Convert SQL to Laravel ORM RELATONS - php

It is working but I should do with Relaion
SELECT * FROM product JOIN ware_houses ON product.id=ware_houses.product_id WHERE ware_houses.variant_id=1;
Like this...
Product::whereHas('ware_houses')->where('ware_houses.product_id',1)->get();
But it is not working because whereHas returns a collection, What I can do ??? Please give advice.

You need to use whereHas() to actually filter:
Product::whereHas('ware_houses', function($q) {
$q->where('ware_houses.product_id',1);
})->get();

This is working thank you !!!
$products=Product::whereHas('wareHouse',function(Builder $query) use ($id){
$query->where('ware_houses.variant_id',$id);
})->get();

Related

How Can We Use Or With Wherebetween Clause in eloquent laravel

$checking = DB::table('bookings')->whereBetween('checkindate', [$checkindate, $checkoutdate])->first();
------My first query--------
$checking_2 = DB::table('bookings')->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)->first();
--------My Second query-------
$checking_3 = DB::table('bookings')->whereBetween('checkoutdate',[$checkindate,$checkoutdate])->first();
------ My third query-------------
I want to build these queries into a single query but unable to do it can anybody help it with me.
I want to run each query and store the result in a single array .
Main question how to use OR after wherebetween query because if I simply use ->Where it doesn't give me desire result
or can anywrite this code in a single query?
Try
$camps = DB::table('bookings')->where(function ($q)
{
$q->whereBetween('checkindate', [$checkindate, $checkoutdate])
->orWhere(function($q1) {
$q1->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)
})
->orwhereBetween('checkoutdate', [$checkindate, $checkoutdate])
})->first();
Try this code.
$checking = DB::table('bookings')
->whereBetween('checkindate', [$checkindate, $checkoutdate])
->orWhereBetween('checkoutdate',[$checkindate,$checkoutdate])
->orWhere(function($query){
$query->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)
})
->first();
You can use the following query to get the result.
->where(function($query) use($checkindate, $checkoutdate) {
$query->whereBetween('checkindate', [$checkindate, $checkoutdate])
->orWhere(function($q) use($checkindate, $checkoutdate) {
$q->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)
})
->orWhereBetween('checkoutdate',[$checkindate,$checkoutdate])
})->first();
use this one
$checking_2 = DB::table('bookings')->where('checkindate','>=' ,$checkindate)->where('checkoutdate','<=',$checkoutdate)->get();

Laravel retreive data from database order by creation date

I am trying to retrieve data orderby creation date desc and get the first one here is my code
$localnews = Articles::whereHas('sous_categories',
function ($query) {
$query->where('id', '15')->order_by('created_at', 'desc');
})->get()->first();
You must have got error when using order_by as its not the syntax in Laravel.
$localnews = Articles::whereHas('sous_categories',
function ($query) {
$query->where('id', '15')->orderBy('created_at', 'desc');
})->first();
Please have a look at official documentation to help you with.
And when using first() you don't need to use get().
get() we use to fetch multidimensional associative array.
first() we use to fetch one record which matches first.
Here is concise difference between all functions you will use then after. link.
It should be like this,
$localnews=Articles::whereHas('sous_categories', function($query) {
$query->where('id', '15')->orderBy('created_at', 'desc');
})->firstOrFail();

Laravel wherehas ignored when using with

I have a Query where I want to see all Alert (relation) items for a User for a specific type.
I'm using the following query
$users = User::with('alerts')->whereHas('alerts', function($q) use ($type) {
$q->where('type', $type);
})->get();
The issue is it's ignoring my where subquery and returning the alerts for all types, not the type I'm passing into the whereHas.
Thank you!
Solution
whereHas limits the results of User, not the results of alerts
I had to use a subquery on the 'with' statement.
$users = User::whereHas('email_alerts')->with(['email_alerts' => function($q) use ($email_type) {
$q->where('email_type', $email_type);
}, 'company'])->get();

Laravel OrderBy Nested Collection

I'm using a Roles package (similar to entrust). I'm trying to sort my User::all() query on roles.id or roles.name
The following is all working
User::with('roles');
This returns a Collection, with a Roles relation that also is a collection.. Like this:
I'm trying to get all users, but ordered by their role ID.
I tried the following without success
maybe because 'roles' returns a collection? And not the first role?
return App\User::with(['roles' => function($query) {
$query->orderBy('roles.id', 'asc');
}])->get();
And this
return App\User::with('roles')->orderBy('roles.id','DESC')->get();
None of them are working. I'm stuck! Can someone point me in the right direction please?
You can take the help of joins like this:
App\User::join('roles', 'users.role_id', '=', 'roles.id')
->orderBy('roles.id', 'desc')
->get();
Hope this helps!
You can make accessor which contains role id or name that you want to sort by.
Assume that the accessor name is roleCode. Then App\User::all()->sortBy('roleCode') will work.
Here's the dirty trick using collections. There might be a better way to achieve this(using Paginator class, I guess). This solution is definitely a disaster for huge tables.
$roles = Role::with('users')->orderBy('id', 'DESC')->get();
$sortedByRoleId = collect();
$roles->each(function ($role) use($sorted) {
$sortedByRoleId->push($role->users);
});
$sortedByRoleId = $sortedByRoleId->flatten()->keyBy('id');
You can sort your relations by using the query builder:
notice the difference with your own example: I don't set roles.id but just id
$users = App\User::with(['roles' => function ($query) {
$query->orderBy('id', 'desc');
}])->get();
See the Official Laravel Docs on Constraining Eager Loading
f you want to order the result based on nested relation column, you must use a chain of joins:
$values = User::query()->leftJoin('model_has_roles', function ($join)
{
$join>on('model_has_roles.model_id', '=', 'users.id')
->where('model_has_roles.model_type', '=', 'app\Models\User');})
->leftJoin('roles', 'roles.id', '=', 'model_has_roles.role_id')
->orderBy('roles.id')->get();
please note that if you want to order by multiple columns you could add 'orderBy' clause as much as you want:
->orderBy('roles.name', 'DESC')->orderby('teams.roles', 'ASC') //... ext
check my answer here:
https://stackoverflow.com/a/61194625/10573560

Laravel - Using a nested relationship with the query builder

I am working on an API but its starting to get a bit slow now that the data is increasing. I am moving some of the queries so that they use the DB query builder.
I have my last one which has a nested query:
$artists = Artist::with('performances', 'performances.stage')->get();
I have got so far:
$artists = \DB::table('artists')
->leftJoin('performances', 'artists.id', '=', 'performances.artist_id')
->get();
But now need to do the second relationship which in the Performance model is:
public function stage()
{
return $this->hasOne('App\Models\Stage', 'id', 'stage_id');
}
Any help on how I do this?
yes you can use eloquent relationship with query builder like this
$artists = Artist::join('performances', 'artists.id', '=', 'performances.artist_id')
->all();
foreach($artists as $artist){
$data = $artist->stage()->first();
}
It is very well covered in the official documentation, please, refer to this section of Documentation
I think that you want to achieve something like this:
$posts = Post::whereHas('comments', function ($query) {
$query->where('content', 'like', 'foo%');
})->get();
And also, please, read carefully this section

Categories