I noticed that in web.php
I’m able to access find()
of Listing
model (that I created)
Route::get('listings/{id}',function($id){
return view('listing',[
'listing'=>Listing::find($id)
]); });
When I checked Listing
model I found that it extends IlluminateDatabaseEloquentModel
class. But I couldn’t locate find()
there. On further inspection I discovered that the above mentioned find()
is a part of IlluminateDatabaseEloquentBuilder
class. Question is, how is my Listing
model able to access IlluminateDatabaseEloquentBuilder
or does it happen through IlluminateDatabaseEloquentModel
, if yes, how?
2
Answers
Laravel uses some "magic" ways to do thing like this.
You can see the way it does this by forcing an Exception in a find method like this:
This will show you the path the code took
It will look like this:
if you want to see all query builder methods use the query method.
here is an example if you have a user model and want to find a user by id
for me, my code editor is Php storm when I click the find method it forwards me to the Builder class
you will get all database query builder methods in this class