24

I have 2 models: Profile and Student. The profile contains a primary key, id. Student model doesn't have any primary key, but one foreign key, profile_id, which links to profile.id. I have a function within my Student controller that activates the Student, setting the status from 0 to 1, when called. I am trying to use save() for the update of the value, but unable to do it, prompting me a message as such:

Column not found: 1054 Unknown column 'id' in 'where clause'

Can please advise? Thank you very much.

Student Model:

class Student extends Eloquent {

    protected $primary_key = null;
    public $incrementing = false;

    protected $fillable = array('username', 'password', 'status', 'profile_id');

    public function profile() {
        return $this->belongsTo('Profile');
    }
}

Profile Model:

class Profile extends Eloquent {

    protected $fillable = array('firstname', 'lastname', 'email', 'phone');

    public function student()
    {
        return $this->hasOne('Student', 'profile_id', 'id');
    }
}

StudentController:

public function activate($id) {
        $student = Student::where('profile_id', '=', $id)->first();
        $student->status = 1;
        $student->save();
        return Redirect::to('students');
}
jl.
  • 2,129
  • 12
  • 46
  • 61

3 Answers3

56

You might want to consider adding an id field. It is pretty important to Laravel and packages (for aggregation, specifically)

but if you must...

Student Model: (camel case primaryKey property)

class Student extends Eloquent {

    protected $primaryKey = null;
    public $incrementing = false;


    protected $fillable = array('username', 'password', 'status', 'profile_id');

    public function profile() {
        return $this->belongsTo('Profile');
    }
}
c-griffin
  • 2,760
  • 1
  • 17
  • 25
  • I think you are right about the "primaryKey", but upon trying to change it in the code, I got a loading issue upon calling the function instead. I am not sure is it really the case that I definitely require an id field to work, even though it doesn't serve any purpose in my case, but just to satisfy the requirement for needing a primary key. – jl. Aug 21 '14 at 18:39
  • 1
    What is the loading issue you are experiencing after changing the property name? No doubt, you can use this table with Laravel and no primary key, but it may become increasingly difficult when trying to integrate other packages: `Cruddy` for example. Adding a `primary key` may save you some headache down the road and i can't think of any case where it would be costly. – c-griffin Aug 21 '14 at 18:54
  • Thanks, the page just doesn't load, and firefox states connection reset. I dig into the laravel.log, doesn't show any error. But I am going ahead and work with adding the Id, which may become handy next time. – jl. Aug 22 '14 at 03:35
  • @jl. did you start your webserver? – developerbmw Jul 29 '15 at 03:00
  • 1
    Note, that official documentation doesn't mention about such case and in some reasons it can lead to unexpected errors, hard to locate and debug, so **I strongly recommend everyone don't do this!** – Bushikot Aug 31 '16 at 07:52
  • 1
    I can confirm that setting the `$primaryKey` to `null` just makes Laravel blow up and give a white screen of death with absolutely no logging anywhere, any time an update is attempted. This will work for inserts, but not updates. – Skeets Oct 12 '17 at 20:57
  • 2
    Confirmed, stuff super easy, when working with db directly, is super-duper complicated in Laravel. Laravel NEEDS primary key. Without it entire framework collapses silly and is totally useless. So, design your DB with primary keys, or use some other framework, as Laravel is useless to you. – Jeffz Feb 20 '18 at 01:49
  • Worth noting, if you're not using autoIncrement and instead are using UUID or something similar, make sure you sent `$incrementing = false` in the model – SamMakesCode Nov 25 '19 at 14:26
6

I had the same problem and couldn't use a primary key field for this table.

Since Eloquent cannot update tables like this, I solved it simply deciding updating the row in a different way:

NameOfMyTable::where('entity_type', 1)->update(['last_import' => Carbon::now()]);
ivoroto
  • 725
  • 10
  • 11
-2

If anyone has the same issue, I used Nishchit Dhanani answer and it worked pretty well:

Write this line into your Model:

public $primaryKey = '_id';

Where _id is your manual primary key.

For my case I had a user table and part of my users are students. So the primary key in my students table was "user_id" and I added this line in my Student model:

public $primaryKey = 'user_id';

I was able to use the save() method when updating my student object.

In your case you would just need to add this to your student model:

public $primaryKey = 'profile_id';
Tony
  • 9,280
  • 3
  • 46
  • 72
mushood badulla
  • 987
  • 1
  • 10
  • 17