Laravel Eloquent Query WITH parameters - laravel

Morning,
il would like to use Eloquent to make this request but i have an error.
$ModelVars=Model1::with(['Model2' => function($query,$var){
return $query->where('field1', 'like', '%'.$var.'%');}])->get();
Could somebody help me.
Thks in advance.

Change your code to :
$ModelVars = Model1::with(['Model2' => function($query) use ($var){
return $query->where('field1', 'like', '%'.$var.'%'); }])
->get();
use is not a function, it's part of the Closure syntax It simply makes the specified variables of the outer scope available inside the closure.

Related

Laravel 7 Query with() and using Where()

Hey guys I have a query that looks like this
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table));
I need to filter the transaction based on the iso_id that belons to the current user logged in.
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table))
->where('merchant.iso_id', '=', auth()->user()->isIso());
The iso_id I need to compare to, is inside the merchant table
auth()->user()->isIso() returns the correct iso_id if true or sends false if not
So my first try at this was to use where('merchant.iso_id', '=', auth()->user()->isIso())
But that returns that the column does not exist because for some reason, it's not switching from the transaction model to the merchant one.
I am not sure how to use the stuff inside with() as a selector for my where()
Any help would be appreciated!
Try using whereHas to add the constraint:
$query = Transaction::with(['customer', 'batch'])
->whereHas('merchant', function ($q) {
$q->where('iso_id', auth()->user()->isIso());
})
->select(sprintf('%s.*', (new Transaction)->table))
->get();

Acces to a model relation in an Eloquent Query

I try to make a particular query but i'm not sure how to achieve this , i would like to access to saison table
{{ \App\Licencies::where(['structure_id' => Auth::user()->structure->id])->where(['saison_id->dt_fin' , '<' , \Carbon\Carbon::now()])->count() }}
here the saison() relation in my model , that i can access very well
$licencie->saison->dt_fin < \Carbon\Carbon::now()
someone knows to right query ? thanks a lot in advance
Thanks for the explanation in the comment.
In the Laraval documentation there is the following example:
// Retrieve all posts with at least one comment containing words like foo%
$posts = Post::whereHas('comments', function ($query) {
$query->where('content', 'like', 'foo%');
})->get();
Which can easily be transformed for your purpose:
// Retrieve all licencies where the saison has a dt_fin before now
$licencies = \App\Licencies::whereHas('saison', function ($query) {
$query->where('dt_fin', '<', \Carbon\Carbon::now());
})->get();
If it still returns errors, let us know! (kinda hard to test without carbin and a structure that is equal to yours)

Laravel Eloquent: how to get related records via whereHas() method?

For example, I can check every post with concrete date.
$users = User::whereHas('posts', function($q){
$q->where('created_at', '>=', '2015-01-01 00:00:00');
})->get();
But suppose, what to do, if I want to compare a post model date (created_at) with the date attribute of user model?
For example:
$users = User::whereHas('posts', function($q){
$q->where('created_at', '>=', ** $user->customDate ** ← look this);
})->get();
upd.
I use Eloquent outside of Laravel.
You can use a raw expression on the Eloquent sub query, by using $q->whereRaw.
In your example:
$users = User::whereHas('posts', function($q){
$q->whereRaw("created_at >= users.customDate");
})->get();
Unlike DB::raw, this can be used without Laravel's dependency injection of Illuminate\Database facade.
Assuming that your users table is just called users you can use DB::raw() to reference a value like you would in a normal query:
$users = User::whereHas('posts', function($q){
$q->where('created_at', '>=', DB::raw('users.customDate'));
})->get();
Don't forget to either import the DB facade or just change it to \DB::raw(...).
If you're using this outside of Laravel and don't have facades you can cut out the middleman and do:
$q->where('created_at', '>=', new Illuminate\Database\Query\Expression(('users.customDate'));
Hope this helps!

How to start a query in Laravel with Eloquent?

I'm trying to write the following code:
foreach($relations as $relation){
$query->orWhereHas($relation, function ($query) use ($filter) {
$query->where('name', 'like', '%'.$filter.'%');
});
}
but I don't know how to initialize the $query variable, I'd prefer not to write the first orWhereHas before the foreach starts.
Anyone knows how to achieve this?
Thanks!
Eloquent models has query() method. Then, you can do this
$query = App\User::query();

Paginate based on conditional with relationship

I'm trying to paginate based on a conditional on a relationship. Thought this would work but it does not..
Product::with(['manufacturer' => function($query){
$query->where('name', '=', 'Maker');
}])->paginate(10)->toArray();
for some reason It only works on the first model. I can tell because its the only one loading the manufacturer data.
Anyone have any idea how to do this?
thanks!
Laravel offers convenient way to query relationships (docs)
I think, this is what you want
$products = Product::whereHas('manufacturer', function($q)
{
$q->where('name', '=', 'Maker');
})->get();
You may add pagination and other things as you wish.

Resources