I have two table 1)clients 2)orders. I trying to make leftjoin but I not getting correct result what i want. Please see image below.
Thanks
ClientController.php
$clients = Client::leftjoin('orders','orders.client_id','=','clients.id')
->select(DB::raw("count(orders.id) as orderCount"),
'clients.name as name','clients.email as email',
'clients.phone as phone','clients.country as country',
'orders.total_amount')
->where('orders.status',0)
->get();
$myevents = DB::table('users')
->join('event_model_user', 'users.id', '=', 'event_model_user.user_id')
->rightJoin('events', 'event_model_user.event_model_id', '=', 'events.id')
->where('users.id','=',$user->id)
->orWhere('events.bookable','=',$user->zipcode)
->get();
this is an example of i used a right join to get the events where no user has done any booking!
in your case this would be something like (not tested)
$myevents = DB::table('clients')
->leftJoin('orders','orders.client_id','=','clients.id')
->where('orders.status',0)
->get();
Related
I have a query that works but I need to get it into the Query Builder:
RIGHT JOIN (tlkpSUDesc RIGHT JOIN tblSU ON tlkpSUDesc.SUDescCode = tblSU.SUDescCode) ON tlkpSUTyp.SUTypCode = tblSU.SUTypCode
I can figure out how to do it. I tired this:
->rightJoin('tlkpSUDesc', function($join) {
$join->on('tblSU', 'tlkpSUDesc.SUDescCode', '=', 'tblSU.SUDescCode');
}
'tlkpSUTyp.SUTypCode', '=', 'tblSU.SUTypCode')
But that didn't work. I also tried
->rightJoin('tlkpSUDesc', 'tlkpSUTyp.SUTypCode', '=', 'tblSU.SUTypCode')
->rightJoin('tblSU', 'tlkpSUDesc.SUDescCode', '=', 'tblSU.SUDescCode')
But that didn't work either.
Thanks!
I agree with #matiaslauriti that there's nothing wrong with reaching for raw expressions. But, since you asked and it can be done with the query builder, try this:
$query = \DB::query();
$query->rightJoin('tlkpSUDesc', function ($query) {
$query->rightJoin('tblSu', 'tlkpSUDesc.SUDescCode', '=', 'tblSU.SUDescCode')->on(
'tlkpSUTyp.SUTypCode', '=', 'tblSU.SUTypCode');
})->toSql();
I'm using a Laravel 8 for website developement, and on the next part of code, I execute a query:
$assig = DB::table('table')
->where('table.column', '=', "oneValue")
->select('anfunctionOnDatabase(2,table.columDesc)')
->get();
but it no works.
How I need call to database function on laravel?
Thanks
Use selectRaw()
like this;
$assig = DB::table('table')
->where('table.column', '=', "oneValue")
->selectRaw('anfunctionOnDatabase(2,table.columDesc)')
->get();
Use : DB::raw :
$assig = DB::table('table')
->where('table.column', '=', "oneValue")
->select(DB::raw("anfunctionOnDatabase(2,table.columDesc)"))
->get();
I have a laravel eloquent db query that I would like to return results based on chained join.
$replies = DB::table('model_replies')
->join('support_tickets', function($join){
$join->on('model_replies.model_id', '=', 'support_tickets.id');
$join->where('model_replies.model', '=', DB::raw('"SupportTicket"'));
})
->join('produce_requests', function($join){
$join->on('model_replies.model_id', '=', 'produce_requests.id');
$join->where('model_replies.model', '=', DB::raw('"ProduceRequest"'));
})
->get();
I followed the example from the docs here.
The problem is when I chain the join statements as suggested here my query gives no results.
A single join works just fine. But I need to be able to chain several of them.
What could I be doing wrong?
Thank you!
If I understood it correctly. You want to get support_tickets for those model_replies where model_replies.model = SupportTicket
DB::table('model_replies')
->leftJoin('support_tickets', function($join){
$join->on('model_replies.model_id', '=', 'support_tickets.id');
$join->on('model_replies.model', '=', DB::raw('"SupportTicket"'));
})
->leftJoin('produce_requests', function($join){
$join->on('model_replies.model_id', '=', 'produce_requests.id');
$join->where('model_replies.model', '=', DB::raw('"ProduceRequest"'));
})
->get();
I would like to test the output of Laravel function in MySQL, for this I need to translate the code to SQL request.
Function :
return DB::table('products')
->leftJoin('product_locations', 'products.id', '=', 'product_locations.productId')
->select(DB::raw('IF(product_locations.id is not null,concat(product_locations.id,"-","M"),concat(products.id,"-","N")) as id'), DB::raw('IF(product_locations.id is not null,1,0) as multilocation'), 'products.productName', 'products.productName as text', 'product_locations.productLocationName', 'product_locations.binLocationName', 'product_locations.unitCost', 'product_locations.amount')
->orderBy('products.productName')
->distinct()
->get();
Use query logging:
DB::connection()->enableQueryLog();
DB::table('products')
->leftJoin('product_locations', 'products.id', '=', 'product_locations.productId')
->select(DB::raw('IF(product_locations.id is not null,concat(product_locations.id,"-","M"),concat(products.id,"-","N")) as id'), DB::raw('IF(product_locations.id is not null,1,0) as multilocation'), 'products.productName', 'products.productName as text', 'product_locations.productLocationName', 'product_locations.binLocationName', 'product_locations.unitCost', 'product_locations.amount')
->orderBy('products.productName')
->distinct()
->get();
dd(DB::getQueryLog())
It is not the way, but still you can get the same result with less effort.
Make a small mistake in you query, for example use remove last character of a table name or a column name. Then it raise an error that shows the query. Then you can easily get your query on the screen.
In your query, use product instead of products. This will cause an error in query and display it.
I am trying to pick out Shops with the specified $item_id.
An Item can have multiple Images, but I only want the first one.
The current code gives me only the first image, but it duplicates the item as many times as there are images.
Here's the code:
$shops = Shop::with(array('items' => function($query) use ($item_id) {
$query->select('items.id', 'items.name', 'items_images.path AS image');
$query->join('items_images', 'items.id', '=', 'items_images.item_id');
$query->where('items.id', '=', $item_id);
}))
->get(array('shops.id', 'shops.shop_name', 'shops.lat', 'shops.lng'));
And the current output:
How can I avoid getting all the duplicates?
I think this will work.. I haven't done much at all with this syntax but I think this is how it would work (assuming main is in the items_images table)
$shops = Shop::with(array('items' => function($query) use ($item_id)
{
$query->select('items.id', 'items.name', 'items_images.path AS image');
$query->join('items_images', function($join)
{
$join->on('items.id', '=', 'items_images.item_id')
->where('items_images.main', '=', 1);
})
$query->where('items.id', '=', $item_id);
}))->get(array('shops.id', 'shops.shop_name', 'shops.lat', 'shops.lng'));
Found the DOCS on it... if you want to include it in the JOIN then it looks like you have to add a where to the JOIN