I am working on a project which requires me to get all the list of all information from a table --Just like in a blog, i used the all()
method to do this but when i try to get the method i declared in my Model i get an error, saying
the collection instance does not exists
But when i use The Model::find($id)->relationship()->name;
it works fine. Is there any way to load all relationship with the all()
function in laravel.
Thanks for your help..
When you perform Model::find($id)->relationship();
you are actually accesing to the Dynamic relationships Properties
You need to convert it into a collection using Model::find($id)->relationship()->get();
Then you can perform any collection method to get the result you want. After doing this you can access to its attributes like this:
$model_varible = Model::find($id)->relationship()->get();
$model_variable = $model_variable->find($id)->name;
Let me know if this works for you.
You should use relationship without brackets to access the model:
Model::find($id)->relationship->name;
And use "with()" to populate the relationships:
Model::where('published', 1)->with('relationship')