Query relationship Eloquent
Any of these should work for you, pick the one you like the most:
Eager-loading.
You can inject the parameter to query function by$comments = News::find(123)->with(['comments' => function ($query) {
$query->where('trashed', '<>', 1);
}])->get();use($param)
method, that allows you to use dynemic query value at runtime.Lazy-loading
$news = News::find(123);
$comments = $news->comments()->where('trashed', '<>', 1)->get();
I couldn't help but notice, though, that what you're probably trying to do is handle soft deleting, and that Laravel has built-in functionality to help you with that: http://laravel.com/docs/eloquent#soft-deleting
Laravel where on relationship object
The correct syntax to do this on your relations is:
Event::whereHas('participants', function ($query) {
return $query->where('IDUser', '=', 1);
})->get();
This will return Events where Participants have a user ID of 1. If the Participant doesn't have a user ID of 1, the Event will NOT be returned.Read more at https://laravel.com/docs/5.8/eloquent-relationships#eager-loading
Sub query with eloquent relationship
Look like by default its not supported Eager-loading with a limit
To solve this problem you can install following library
https://github.com/staudenmeir/eloquent-eager-limit
and use following trait in both the model
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
if you are looking for without library thenRef:Laravel Eloquent limit results for relationship
Ref:Eager-loading with a limit on collection only loads for last element in collection
https://github.com/laravel/framework/issues/18014
Laravel: How to query a Model Relationship only if it exists
Original answer
What i needed was the whereDoesntHave
method.
$surveys = Survey::whereNotNull('installer_id')
->whereDoesntHave('installation.assignments', fn ($query) => $query->where('status', 2))
->get();
How to query Laravel Eloquent belongsTo relationship?
Try this:
State::where('name','like','l%')->get();
For querying relationship:$states= City::whereHas('state', function ($query) {
$query->where('name', 'like', 'l%');
})->get();
Query relationship inside the relationship in Laravel Eloquent
you can use whereYear
public static function getNewsByAuthorByDate($id, $year, $limit = 1){
return User::where('id', $id)->has('news')
->with(['news' => function($q) use(&$limit, &$year) {
$q->whereYear('created_at',$year)->latest()->limit($limit);
}])
->first();
}
and to use limit with eager loading you should use the package eloquent-eager-limit Laravel Eloquent query with relation
I found the issue...
The way you call the relationship method defined in the Model changes the returned result.
In general, if you call:
$model->relationship() //with parenthesis
an instance of the relationship is returned. You are calling the method as a method and, as it is for Eloquent model classes, it "serves as powerful query builder" and "provides powerful method chaining and querying capabilities" (see here).Otherwise if you call:
$model->relationship //without parenthesis
you get directly an instance of the Collection. This way you are calling the method as a property and getting a Laravel Collection thus letting you loop on the collection, get record (model) properties and use all the methods of the Collection class.So, in my case, writing
$rndVersione = Version::inRandomOrder()->first();
$rndFilm = $rndVersione->film()->first(); //film() WITH parenthesis
works, I guess because the first() method is called on the relationship as a method, therefore maintaining the correctness of query building. Query More than one relationship from a model with() in Laravel
Pass array
of relationship to with
method
StoreStock::with(['product.price','product.masterlist']) ->get()
Related Topics
Str_Replace() for Multiple Value Replacement
PHP MySQL Charset Utf8 Problems
PHP Displaying Unread Mail Count
Creating and Invoking an Anonymous Function in a Single Statement
Get Query Back from Pdo Prepared Statement
0' as a String with Empty() in PHP
Get First Element in PHP Stdobject
How to Resize and Convert an Uploaded Image to a Png Using Gd
Ffmpeg Mamp "Dyld: Library Not Loaded" Error
Cannot Unpack Array with String Keys
No Hint Path Defined for [Mail] Laravel 5.4
Pdo Prepared Statements for Insert and on Duplicate Key Update with Named Placeholders
How to Get the Session Id in Laravel
Determining If a File Exists in Laravel 5