I am new in Laravel.
How to convert the following query to Laravel where clause.
where category='1' and (shipping_from='1' OR shipping_to='2')
You should use nested parameter groupings: https://laravel.com/docs/5.6/queries#parameter-grouping. Eg:
->where('category','1')
->where(function ($query) {
$query->where('shipping_from', '1')
->orWhere('shipping_to', '2');
})
->where('category', 1)
->where(function ($query){
$query->where('shipping_from', 1)
->orWhere('shipping_to', 2);
})
Refer to: Parameter Grouping
Please check the documentation of parameter-grouping in Laravel 5.7
$products = Product::where('category', 1)
->where(function ($query){
$query->where('shipping_from', 1)
->orWhere('shipping_to', 2);
})->get();
Initially laravel thinks AND condition in all where conditions. until unless you specify or.
You can also try like this:-
use App\Model;
$data = Model::where('category','1')
->where(function ($query) {
$query->where('shipping_from', '1')
->orWhere('shipping_to', '2');
})->get();
Related
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
i have a problem when doing "orderBy" name product. here are my codes
$resume = Transaction::with(['product' => function ($q) {
$q->orderBy('name_product','ASC');
}])
->where('status', 'keluar')
->where('status', 'masuk')
->get();
but my code its not working... here is the output
result
Use the collection to sort instead. It allows for sorting based on a nested property.
$resume = Transaction::with('product')
->where('status', 'keluar')
->where('status', 'masuk')
->get()
->sortBy('product.name_product')
->values();
Because you are applying condition to product, not in transaction.
I have another suggestion use whereHas.
Transaction::whereHas('product', function($query) {
$query->->orderBy('name_product','ASC');
})->where('status', 'keluar')
->where('status', 'masuk')
->get();
In Laravel eloquent query using multiple columns for whereNotIn clause I need to hardcoded one of the DB::raw column for the value should be coming from a variable (loop variable). What is the best way to implement this?
This is my query and I need to change the hardcoded 1 in DB::raw('(1,user_profile.user_id')
$otherProfiles = Userprofile::where('user_id', '!=', $profile->user_id)
->where(function ($query) use ($userInterests) {
foreach ($userInterests as $interest) {
$query->orWhere('interest', 'like', "%$interest%");
};
})
->whereNotIn(DB::raw('(1, user_profile.user_id)'), function ($query) {
$query->select('sender_id', 'receiver_id')
->from('email_reports');
})
->inRandomOrder()
->get();
Manage to fix it by just simple concatenation
the code added were DB::raw('('. $profile->user_id . ', user_profile.user_id)')
create a variable as $id for your dynamic value..
$otherProfiles = Userprofile::where('user_id', '!=', $profile->user_id)
->where(function ($query) use ($userInterests) {
foreach ($userInterests as $interest) {
$query->orWhere('interest', 'like', "%$interest%");
};
})
->whereNotIn('user_id', $id), function ($query) {
$query->select('sender_id', 'receiver_id')
->from('email_reports');
})
->inRandomOrder()
->get();
I'm working in Laravel 5 and am using the Orwhere clausule, my code to consult is this:
$estudiantes= Usuario::
where('rol', '=', 'ESTUDIANTE')
->whereHas('perfil', function($query) use ($data){
if($data!="")
$query->Where('doc_identidad', '=' ,$data)
->orWhere('nombres','like','%'.$data.'%')
->orWhere('apellidos','like','%'.$data.'%');
dd($query);
})->get();
So, I want to filter every Usuario with rol ESTUDIANTE that works very fine, then I want to filter which of these have doc_identidad or nombres or apellidos like you see, I have a dd($query) to see the tree of the query, and I noticed that the first where uses and instead of or. This is the tree:
So, I have the next:
where1 and where2 or where3 or where4
and I want:
where1 and (where2 or where3 or where4)
so, how should I do it? thanks!
where1(a=1) and (where2(b=1) or where3(c=1) or where4(d=1))
Model::where(function ($query) {
$query->where('a', '=', 1);
})->where(function ($query) {
$query->where('b', '=', 1)
->orWhere('c', '=', 1)
->orWhere('d', '=', 1);
});
I know that sql AND can be achieved by consecutive where clauses. and OR with where(condition,'OR').
I want to choose all the messages from message table where sentTo = editor_id AND sentFrom= currentUser_id OR where sentTo = currentUser_id AND sentFrom = editor_id.
I have this query which I am trying to get the result through,
$this->data['messages'] = Message::where(function ($query) use($uId) {
$query->where('sentTo', '=', $this->data['currentUser']->id)
->orWhere('sentFrom', '=', $uId);
})
->where(function ($query) use($uId){
$query->where('sentTo', '=', $uId)
->orWhere('sentFrom', '=', $this->data['currentUser']->id);
})
->get();
How can I do this? Please advice.
I'm a bit confuse as you didn't provide parentheses to know which operator precedence you want.
Assuming you want WHERE (editor_id=$editorId AND sentFrom=$currentUserId) OR (sentTo=$currentUserId AND sentFrom=$editorId) then latest version of Laravel allows you to do:
Message::where(['editor_id' => $editorId, 'sentFrom' => $currentUserId])
->orWhere(['sentTo' => $currentUserId, 'sentFrom' => $editorId])->get();
No need to use closures.
Try this
$this->data['messages'] = Message::where(function ($query) use($uId) {
$query->where('sentTo', '=', $this->data['currentUser']->id)
->where('sentFrom', '=', $uId);
})-> orWhere(function ($query) use($uId){
$query->where('sentTo', '=', $uId)
->Where('sentFrom', '=', $this->data['currentUser']->id);
})->get();