user6817600
user6817600

Reputation:

Method [where] does not exist

I have encountered a problem while working in Laravel project.

Method [where] does not exist. i know the 'Where' clause is causing it .

I have studied it but not solution is found. finally i had to ask here.

$data = Track::where(
                [
                    ['generated_by', '=', Input::get('user_id')],
                    ['id', '=',Input::get('track_id') ],
                ])->get();


            $finalResult = array('code' => 100,
                'msg' => 'Your Account is found.',
                'data' => $data
                );

My controller name is Track . i have also made a Model named Track . the corresponding table name is tracks as laravel requires it to be plural of your model.

Can anybody please help me in this situation ?

Upvotes: 1

Views: 238

Answers (2)

user6817600
user6817600

Reputation:

Problem Solved. The problem was caused because i had same names of controller(Track) and my model (Track) .

I had to refer to model by useing \App\ in front of my model So my code would become

$data = \App\Track::where(
                [
                    ['genrated_by', '=', Input::get('user_id')],
                    ['id', '=',Input::get('track_id') ],
                ])->get();


            $finalResult = array('code' => 100,
                'msg' => 'Your Account is found.',
                'data' => $data
                );

Upvotes: 1

user1669496
user1669496

Reputation: 33098

Your problem is you have both a controller and model named Track. And when you use Track::where() it assumes the where method is being called in your controller.

Unfortunately you can't just import your model as Track because then there would be a naming conflict.

You will need to alias your Track model.

In your import statements (use), add the following...

use App\Track as TrackModel;

This assumes your models are in the App namespace. And modify your code...

TrackModel::where()...

That should get it working. However I highly advise you to use proper naming conventions. You will likely run into many more pitfalls going down this path that can easily be avoided. Controllers should end in Controller.

Upvotes: 0

Related Questions