I need all columns from invoice,invoice_details table but I want to get only one column (column name: amount) from my payment table. What will be my query for it? here is my controller.
$all_invoice = DB::table('invoice')
->join('invoice_details', 'invoice.invoice_id', '=', 'invoice_details.invoice_id')
->join('payment', 'invoice.invoice_id', '=', 'payment.invoice_id')
->where('invoice.client_id', 7)
->orderBy('invoice.invoice_id', 'DESC')
->get();
This very simple you need to add select clause in query like this.
$all_invoice = DB::table('invoice')->select(['invoice.*', 'payment.amount'])
->join('invoice_details','invoice.invoice_id','=','invoice_details.invoice_id')
->join('payment','invoice.invoice_id','=','payment.invoice_id')->where('invoice.client_id',7)->orderBy('invoice.invoice_id','DESC')->get();
If have still query the see the laravel docs in select option in query.
Just using select
$all_invoice = DB::table('invoice')
->join('invoice_details', 'invoice.invoice_id','=','invoice_details.invoice_id')
->join('payment', 'invoice.invoice_id', '=', 'payment.invoice_id')
->where('invoice.client_id', 7)
->select('payment.amount')
->orderBy('invoice.invoice_id', 'DESC')
->get();
Related
I am new in Laravel and facing an issue with the DB query builder.
I want to fetch all the records from users table and want to count related wishlists in wishlists table. Here is my query.
select `users`.*, count(wishlists.id) as wishlists_count from `users` left join `wishlists` on `users`.`id` = `wishlists`.`uid` where `users`.`status` = 1 group by `wishlists`.`id`
I want to do it with laravel query builder. Here is what I am trying.
$leads = DB::table('users')
->selectRaw('users.*', 'count(wishlists.id) as wishlists_count')
->leftJoin('wishlists', 'users.id', '=', 'wishlists.uid')
->where('users.status',1)
->groupBy('wishlists.id')
->get();
It is showing this error.
Argument 2 passed to Illuminate\Database\Query\Builder::selectRaw()
must be of the type array, string given.
Any help will be greatly appreciated.
Thanks in Advance.
This should work. From the documentation, the second argument to selectRaw() is an optional array of bindings.
However, you can achieve what you are looking for with a combination of select() and DB::raw()
$leads = DB::table('users')
->select('users.*', DB::raw('count(wishlists.id) as wishlists_count'))
->leftJoin('wishlists', 'users.id', '=', 'wishlists.uid')
->where('users.status',1)
->groupBy('wishlists.uid')
->get();
Edit: The above answer is wrong. I would consider the following alternatives instead.
Either you could achieve it with a Sub-Query with something like the following:
$wishlist = Wishlist::select(DB::raw('count(wishlists.id)'))
->whereColumn('uid', 'users.id')
->getQuery();
$users = User::select('users.*')
->selectSub($wishlist, 'wishlists_count')
->get();
Or with a much easier way (assuming you have a wishlists relationship set up)
Users::withCount('wishlists')->get();
foreach($users as $user) {
echo $user->wishlists_count;
}
I'm using Laravel Query Builder and my join statement working perfectly.
User table columns:
name|email|phone|gender
School_Abouts table columns:
courses|boards|contact|location|teachers
Currently I do the select query as below:
$school=User::join('school_abouts', 'users.id', '=', 'school_abouts.school_id')
->where('users.id',$id)
->select('users.name',
'users.email',
'users.phone',
'school_abouts.courses',
'school_abouts.boards',
'school_abouts.contact',
'school_abouts.location',
'school_abouts.teachers')
->first();
To select the columns from school_about table I have to write table name multiple times. But is there any way to pass an array of columns instead? I tried this but failed:
->select('users.name',
'users.email',
'users.phone',
'school_abouts'.[courses,boards,location,contact,teachers],
)
You can safely remove table name from columns as there is no column name common in both tables but else, as I see, you are trying to get almost all columns from both tables which could be simplified using *:
$school = User::join('school_abouts', 'users.id', '=', 'school_abouts.school_id')
->where('users.id', $id)
->select('users.*', 'school_abouts.*')
->first();
However, if you want to get some columns and their names could make an ambiguity then prefixing column names with table name is a must. To make it shorter you could use aliasing:
$school = User::join('school_abouts AS sa', 'users.id', '=', 'sa.school_id')
->where('users.id', $id)
->select('users.name',
'sa.courses',
'sa.boards',
'sa.contact',
'sa.location')
->first();
I have to run a query to find out the total orders for each quantity as
SELECT userorders.`delivered` as delivered_status, items.`name` as itemname,
sizes.`size` as sizename,SUM(userorders.`order_qty`) as order_quantity,
sizes.`qty` as stockremaining FROM userorders inner join sizes on
userorders.`size`=sizes.`id` inner join items on sizes.`item_id`=items.`id`
WHERE 1 group by userorders.`delivered`, userorders.`size`;
Roughly, its laravel equivalent is
$userorders = DB::table('userorders')
->join('sizes', 'userorders.size', '=', 'sizes.id')
->join('items', 'sizes.item_id', '=', 'items.id')
->join('categories', 'items.category_id', '=', 'categories.id')
->select('SUM(userorders.order_qty) as order_quantity','userorders.delivered as delivered_status', 'items.name as itemname','categories.name as categoryname', 'sizes.size as sizename','sizes.qty as stockremaining')
->groupBy('userorders.delivered', 'userorders.size')
->get();
But, it seems laravel doesnt support aggregate parameters.
I found out that for getting just the aggregate quantity we could use it as
$userorders = DB::table('userorders')
->join('sizes', 'userorders.size', '=', 'sizes.id')
->join('items', 'sizes.item_id', '=', 'items.id')
->join('categories', 'items.category_id', '=', 'categories.id')
->SUM('userorders.order_qty');
But it seems not to be working in my case for getting the sum as well as other parameters too.
Could someone suggest me the way how to make it work??
Use DB::raw:
$userorders = DB::table('userorders')
->select('userorders.delivered', 'userorders.size',
DB::raw('SUM(userorders.order_qty) as total_qty'))
->join('sizes', 'userorders.size', '=', 'sizes.id')
->join('items', 'sizes.item_id', '=', 'items.id')
->join('categories', 'items.category_id', '=', 'categories.id')
->groupBy('userorders.delivered', 'userorders.size');
Note carefully that I only select three things, the two columns which appear in the GROUP BY clause and the aggregate of the order quantity. Selecting any other non aggregate columns would technically be an invalid query (though MySQL might tolerate it).
To use aggregates with eloquent or the query builder you need to use raw methods. Roughly something like:
->selectRaw('SUM(userorders.order_qty) as order_quantity','userorders.delivered as delivered_status', 'items.name as itemname','categories.name as categoryname', 'sizes.size as sizename','sizes.qty as stockremaining')
See the docs: https://laravel.com/docs/5.5/queries#raw-expressions
I have a question about Laravel queries, so I have a upload Model and database table which stores all my images. Then there is an Activity Model and database table which stores all my activities. For each activity I want a image.
So my Activity model has a 'uploads_id' column. I wrote the query like this:
$activity_images = DB::table('uploads')
->join('activities', 'uploads.id', '=', 'activities.upload_id')
->where('uploads.id', '=', 'activities.upload_id')
->get();
It cannot find the right image what am I doing wrong?
You are "joining" twice in your join and where clause. You connect uploads.id and activities.upload_id one time in the join and again in the where clause.
If you want to query for a special upload.id, your query should look like this:
$activity_images = DB::table('uploads')
->join('activities', 'uploads.id', '=', 'activities.upload_id')
->where('uploads.id', '=', '<yourUploadID>')
->get();
If you want all images, you can delete the where statement.
$activity_images = DB::table('uploads')
->join('activities', 'uploads.id', '=', 'activities.upload_id')
->get();
If you use join an inner join will be used.
You could also use leftJoin if you want all entries from the table uploads.
$activity_images = DB::table('uploads')
->leftJoin('activities', 'uploads.id', '=', 'activities.upload_id')
->get();
Does it solve your issue?
Here is my query using fluent query builder.
$query = DB::table('category_issue')
->select('issues.*')
->where('category_id', '=', 1)
->join('issues', 'category_issue.issue_id', '=', 'issues.id')
->left_join('issue_subscriptions', 'issues.id', '=', 'issue_subscriptions.issue_id')
->group_by('issues.id')
->order_by(DB::raw('COUNT(issue_subscriptions.issue_id)'), 'desc')
->get();
As you can see, I am ordering by a count from the joined table. This is working fine. However, I want this count returned with my selections.
Here is the my raw sequel query that works fine.
Select issues.*, COUNT(issue_subscriptions.issue_id) AS followers
FROM category_issue JOIN Issues ON category_issue.issue_id = issues.id
LEFT JOIN issue_subscriptions ON issues.id = issue_subscriptions.issue_id
WHERE category_issue.category_id = 1
GROUP BY issues.id
ORDER BY followers DESC
How would I go about this select using Laravel's fluent query builder? I am aware I can use a raw sql query but I would like to avoid that if possible.
You can use an array in the select() to define more columns and you can use the DB::raw() there with aliasing it to followers. Should look like this:
$query = DB::table('category_issue')
->select(array('issues.*', DB::raw('COUNT(issue_subscriptions.issue_id) as followers')))
->where('category_id', '=', 1)
->join('issues', 'category_issue.issue_id', '=', 'issues.id')
->left_join('issue_subscriptions', 'issues.id', '=', 'issue_subscriptions.issue_id')
->group_by('issues.id')
->order_by('followers', 'desc')
->get();
$count = DB::table('category_issue')->count();
will give you the number of items.
For more detailed information check Fluent Query Builder section in beautiful Laravel Documentation.