Laravel Query relationships - php

I have query with this relationships
$query = ExtraDay::with([
'credit.client'
]);
Now i am going to make searchfield filter for 'name' field which is in CLIENT model. ExtraDay model is connected to Client model via Credit model. Below there is a way how to make search for one level relationship like with(['client']);
$query->where('name', 'like', "%$q%")->whereHas('client', function
($organization) use ($q) {
$organization->where('name', 'like', "%$q%")
->orWhere('short_name', 'like', "%$q%");
But i have with(['credit.client']); How to search field 'name' in this situation?

whereHas works with nested relations, so use the same dot notation credit.client. For example:
ExtraDay::whereHas('credit.client', function ($client) use ($q) {
$client->where('name', 'like', "%$q%");
})->get();

Related

How to search from multiple tables in laravel?

How can I search from multiple tables using laravel eloquent. below is my query.
$data= User::where('id',$search_str)
->orWhere('name', 'LIKE', "%{$search_str}%")
->with('usersecond:id,name,email')->get();
If I search for something I get results from the user table. how can I get results from usersecond table in this query?
use whereHas() function
$data = User::where('id', $search_str)
->orWhere('name', 'LIKE', "%{$search_str}%")
->with('usersecond:id,name,email')
->orWhereHas('usersecond', function ($q) use ($search_str) {
$q->where('name', 'LIKE', "%{$search_str}%");
})->get();
ref link https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-existence

Multiple like in a laravel mongo db package not working

I have a project which is creating in laravel and mongodb. I am using a package for connecting to mongodb "https://github.com/jenssegers/laravel-mongodb". I have a table listing. In this table listing there is a text box for search data. For that I used like feature. I have followed the same in documentation but not working. When I search invoice_number data is getting empty. But when I search beds data is getting perfectly. Please correct me.
$bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
->where('is_delete', 0)
->where('invoice_number', 'like', "%{$search}%")
->orWhere('beds', 'like', "%{$search}%")
->skip($start)
->take($limit)
->orderBy($order, $dir)
->get();
The WHERE statements in your query translate to this:
WHERE [cond_1] AND [cond_2] OR [cond_3]
I doubt this is the condition you need. I'd say you want this:
WHERE [cond_1] AND ([cond_2] OR [cond_3]) -- notice the brackets.
To achieve this, you need a closure in your Builder query. Try this:
$bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
->where('is_delete', 0)
->where(function($query) use ($search) { /* That's the closure */
$query->where('invoice_number', 'like', "%{$search}%")
->orWhere('beds', 'like', "%{$search}%");
})
->skip($start)
->take($limit)
->orderBy($order, $dir)
->get();
And an example from the docs.

laravel 5.2 search in related entity

Suppose I have a Model called 'Commodity' which has category_id belongs to a category.
In Commodity.php I defined this relationship:
public function category(){
return $this->belongsTo('App\Category');
}
Now, I want to find the commodities which their category name include 'glass'
I tried this:
$items = Commodity::orderBy('id', 'desc')->category()->where('title', 'LIKE', '%glass%');
But it populates this error:
Call to undefined method Illuminate\Database\Query\Builder::category()
Please read official documentation that covers querying relations.
Sometimes you may wish to eager load a relationship, but also specify additional query constraints for the eager loading query. Here's an example:
$users = App\User::with(['posts' => function ($query) {
$query->where('title', 'like', '%first%');
}])->get();
So, regarding your question I'd suggest to try next:
$commodities = App\Commodity::with(['category' => function ($query) {
$query->where('title', 'like', '%glass%');
}])->orderBy('id', 'desc')->get();

Eager Load Constraints Filter issue in Laravel

I am unable to filter the contents of groups table with respect to username in users table using Eager Load Constraints
public function username()
{
return $this->belongsTo('User','fk_users_id')->select(['id','username']);
}
I have tried using the code below but it filters only the users data not the groups data
$groups = Groups::with(array('username' => function($query) use ($keyword)
{
$query->where('username', 'like', '%'.$keyword.'%');
}))
->where('status',1)->paginate($paginateValue);
any help is welcome...
Think it should be something like this:
Groups::with('User')->whereHas('User', function($q) use ($key){
$q->where('username', 'like', '%'.$key.'%');
})->where('status', 1)->paginate($pagVal);

Laravel - ORM - call to undefined method

app/models/Model.php:
<?php
class Model extends Eloquent {
public function maker()
{
return $this->belongsTo('Maker', 'maker_id', 'id');
}
}
?>
I want to perform a search. The user enters the name of the model and the search should return the maker of that model (each model belongs to a certain maker). The following piece of code doesn't work:
$result = Model::where('title', 'LIKE', '%test%')->maker()->paginate(10);
It gives me the following error:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::maker()
Any ideas?
I've solved my own problem.
$result = Model::where('models.title', 'LIKE', $query)
->leftJoin('makers', 'models.maker_id', '=', 'makers.id')
->orWhere('makers.title', 'LIKE', $query)
->groupBy('makers.title')
->paginate(10);
According to your solution:
// $search is the keyword to find
Model::with(['maker' => function ($q) use ($search) {
$q->where('title', 'like', "%{$search}%"); // where on makers table
}])->where('title', 'like', "%{$search}%") // where on models table
->paginate(10);
This will return Models that match where clause, with related Makers, but only those who match the where clause too.
But what you need is probably this (according to your question):
Model::with('maker')->where('title', 'like', "%{$search}%") // where on models table
->paginate(10);
This on the other hand returns the Models that match the where clase, each with related Maker
Note: namespace that Model class so it won't collide with Illuminate\Database\Eloquent\Model (which it extends aliased as Eloquent)

Categories