paranoid
paranoid

Reputation: 7105

Find method does not work in laravel controller

I have controller like this

 $test= test::find($id);  
 return $test;

return null but when I change it to :

 $test= test::where('id',$id)->get();
 return $test;

return all value of $test.

My migration is :

 Schema::create('tests', function (Blueprint $table) {

        $table->increments('id');
        $table->string('user', 20);
 })

This is my model:

 public $fillable = [
    'user'
  ];

Upvotes: 0

Views: 1541

Answers (2)

paranoid
paranoid

Reputation: 7105

I solved it.
I have protected $primaryKey='user' in my model and I change it to protected $primaryKey = 'id';

Upvotes: 0

Gayan Hewa
Gayan Hewa

Reputation: 2397

You need a model class. That corresponds to the migration. And it has to be imported in your controller if it's not in the default paths.

Use the below command and it till generate the template and model. Laravel 5.1+

php artisan make:model User --migration

Upvotes: 2

Related Questions