Why is this not working?
Article::with('category')->find($ids)
I got a Array to String Conversion Exception.
But if i split the query into 2 parts like this:
$articles = Article::with('category')
and
$articles = $articles->find($ids)
I didn't get any exception and the result is right.
As we know, the find () method in Laravel can be used by any user along with an array of primary keys, and it will return a set of matching records from the database. For example, $student = Students::all (); With the help of the above data, we can get the details of all the students.
with() function is used to eager load in Laravel. Unless of using 2 or more separate queries to fetch data from the database , we can use it with() method after the first command. It provides a better user experience as we do not have to wait for a longer period of time in fetching data from the database.
BelongsTo is a inverse of HasOne. We can define the inverse of a hasOne relationship using the belongsTo method. Take simple example with User and Phone models. I'm giving hasOne relation from User to Phone. class User extends Model { /** * Get the phone record associated with the user.
The first() method will return only one record, while the get() method will return an array of records that you can loop over. Also, the find() method can be used with an array of primary keys, which will return a collection of matching records.
Just for the posterity... other way you can do this is:
Article::with('category')->whereIn('id', $ids)->get();
This should be faster because it's leaving the query to the database manager
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With