search function look for parameter's value in related table - php

I have 2 tables, merchants and transaction. merchants has (mid,name,age) and transaction has (id,mid,order_no,). I have displayed the data like (orderid,name) with eagerloading. now i have a search function on top where , i can search for all the orders by the merchant's name. how do i do that? im' trying something like this
public function search($search){
$query = Transaction::with('themerchant')
->join('merchants','transactions.mid', '=', 'merchants.mid' )
->select('merchants.name', '=' , '%'.$search.'%')->paginate(10);
return $query;
}

Oh you are using eagerLoading
Do this
public function search($search){
return Transaction::with(['themerchant' => function($query){
$query->where('name', 'like', '%'.$search.'%');
}])->paginate(10);;
}
If you want an exact merchant match change the where like this
...
->where('name', '=' , $search)
...
If you want to use Join
public function search($search){
return Transaction::join('merchants', 'merchants.mid', '=', 'transactions.mid')
->where('merchants.name', 'like', '%'.$search.'%')
->get();
}

Related

Can not use where clause after using with clause in Laravel query

I am writing a query using with and then where clause in laravel.
$users = User::query()->with('roles')->where('name', '!=', 'customer')->get();
return $users;
But where clause is not working here. Customers are not excluded. I am providing the snap shot of the query result.
I think you need whereHas() :
use Illuminate\Database\Eloquent\Builder;
$users = User::query()
->with('roles')
->whereHas('roles', function (Builder $query) {
$query->where('name', '!=', 'customer');
})
->get();
return $users;
I suppose you trying to filter relation data in base query.
Try smth like that:
$users = User::query()->with([
'roles' => function ($q) {
$q->where('name', '!=', 'customer');
}])
->get();
return $users;
Doc

Laravel with() and search using LIKE not working

So I have 2 tables, 1 table for storing debt(id, amount, category_id) and 1 table for storing debt categories(id, name). I am trying to pull the data for each month from the debt table, but I also have a search which seems to not work, I guess I am missing something.
I have the following:
$debt = $this->debtModel
->select(DB::raw('MONTH(created_at) as month'), DB::raw('SUM(amount) as amount'), 'category_id')
->where('user_id', Auth::user()->id)
->whereYear('created_at', $year)
->with(['category' => function ($query) use ($filter) {
$query->where('name', 'like', "%$filter%");
}])
->orderBy('month', 'asc')
->groupBy('month')
->groupBy('category_id')
->get();
Debt Model:
public function category()
{
return $this->hasOne('App\Models\DebtCategory', 'id', 'category_id');
}
This works fine, with the exception of search, If I try to filter by a category name it still returns everything.
try with
->whereHas('category' , function ($query) use ($filter) {
$query->where('name', 'like', "%$filter%");
})
instead of
->with(['category' => function ($query) use ($filter) {
$query->where('name', 'like', "%$filter%");
}])
with() will just loads the relationship not filtering result.

How to get first entry from the record in Laravel relation?

I got this query, it fetch results from my database, based on relationships.
The query works fine, but it grabs the first entry from the database/table and not the first value of the array.
How can I fix that?
$motor = Motor::query()
->where([
['effect_id', '=', $request->effect_id],
['poles', '=', $request->poles],
['voltage_id', '=', $request->voltage_id],
['active', '=', 1],
])
->whereHas('MotorSize', function (Builder $query) {
$query->whereIn('mounting', ['B5', 'B14', 'B34', 'B35']);
})
->firstOrFail();
I have tried different things, such as:
Adding the orderBy to the Motor model and created a "priority" field in my database, giving each a number between 1 and 100:
(i actually thought this would work, but it still grabs the first entry from the table)
public function MotorSize()
{
return $this->belongsTo('App\MotorSize')->orderBy('priority', 'ASC');
}
I also tried something like this, but it's a mess and never got it to work:
$query->where(function ($query) {
$query->where('mounting', 'like', 'B5');
})
->orWhere(function($query) {
$query->where('mounting', 'like', 'B14');
})
->orWhere(function($query) {
$query->where('mounting', 'like', 'B34');
})
->orWhere(function($query) {
$query->where('mounting', 'like', 'B35');
});
Any suggestions will be welcome :)

How to return query from variable?

I try to search first_name, last_name and city but city is now always set whether the users want to set it with city or not. so I make query like this if request has city then filter it
$users = \App\User::join('profiles', 'users.id', '=', 'profiles.member_id')
->where(function($query) {
$query->where('first_name', 'like', '%z%')
->orWhere('last_name', 'like', '%z%');
})
->paginate(10);
if ($request->has('city')) {
$users->where('location', 'like', '%bandung%');
}
but where location like its like never used even request has city. i've try var_dump $users and count it and end up with no city query
Do little change in sequence of your lines of code:
$users = \App\User::join('profiles', 'users.id', '=', 'profiles.member_id')
->where(function($query) {
$query->where('first_name', 'like', '%z%')
->orWhere('last_name', 'like', '%z%');
});
if ($request->has('city')) {
$users->where('location', 'like', '%bandung%');
}
$users_data = $users->paginate(10);
The variable $users_data will be having proper result.

How to make a multiple where clause with Laravel Elequent?

$data = Retour::where('status','=',3)->where('rma','LIKE', '%' .$searchquery.'%')->OrWhere('naam','LIKE', '%' .$searchquery.'%')->OrWhere('framenummer','LIKE', '%' .$searchquery.'%')->get();
What's wrong with this query?
It ignores the where status = 3..
and returns all the reconrds even where the status != 3..
Thanks in advance.
You should group orWhere() here with closure:
$data = Retour::where('status', 3)
->where(function($q) use($searchquery) {
$q->where('rma', 'like', '%'.$searchquery.'%')
->orWhere('naam', 'like', '%'.$searchquery.'%')
->orWhere('framenummer', 'like', '%'.$searchquery.'%');
})->get();
Use scopes in such cases. See https://laravel.com/docs/5.3/eloquent#local-scopes
public function scopeSearch($query, $searchQuery)
{
return $query->where('status',3)
->where('rma','LIKE', "%$searchQuery%")
->orWhere('naam','LIKE', "%$searchQuery%")
->orWhere('framenummer','LIKE', "%$searchQuery%");
}
and one more thing
its not OrWhere it is orWhere

Categories