Using groupby in laravel eloquent - laravel-4

I am using laravel eloquent and doing a group by.
My query as below
$pickups= Pickup::select('pickups.*', DB::raw('count(*) as total'))
->groupBy('address_id')
->havingRaw('total > 0')
->get();
How do I add a function in the model to return all the rows in that group?

Related

How to do search in eloquent for inner tables in laravel

I want to do filter in laravel eloquent for the inner tables, i have 3 tables, discover, category and subcategory, discover have category and subcategory id, if someone try to filter with any text name, i want to filter from all that 3 tables, i am not able to find, how to filter from category and subcategory table, here is my code, can anyone please help me to resolve this issue ?
$discover = Discover::with(['category','subcategory'])->where('status','1')->where('name','like',"%{$searchString}%")->orWhere('description','like',"%{$searchString}%")->orderBy('created_at', 'DESC')->get()->toArray();
You can do this If you want to filter from child table also
https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence
$discover = Discover::where('status', '1')
->where('name', 'LIKE', "%$searchString%")
->orWhereHas('category', function ($query) use ($searchString) {
return $query->where('name','LIKE', "%$searchString%");
})
->orderBy('created_at', 'DESC')
->get();
you can constrain eager loading in this way:
Discover::with([
'category' => function($query){
return $query->where('name','your-category-name');
}
])->whereHas('category', function($query) {
return $query->where('name','your-category-name');
});
the same applies for other relationships. Further info can be found on the laravel docs.
has whereHas Or WhereRelation relation
if you need OrderBY on relation tables
then you need to do join

How i will inner join with condition in eager loading by laravel eloquent

enter code here
$wo=Warehouse_other_receive_detail::with('item_info','item_info.product_sub_group')
->where('item_info.product_sub_group->sub_group_id', 2)
->get();
$wo = Warehouse_other_receive_detail::with(['item_info'=>function( $q){
$q->getQuery()->has('product_sub_group')->where('sub_group_id', 2);
}])
->get();
//but second code is not inner join i want to first one solve
//How i will access in with function model specific column in main model ----solve this ->>
Use whereHas instead of where.You can accomplish what you want with the whereHas method which will perform a query on the relation:
$wo = Warehouse_other_receive_detail::with('item_info')->whereHas('item_info', function ($query){
$query->where('sub_group_id',2);
})->get();

Laravel Eloquent Query Where IN statement

How do I convert this MySQL query to Laravel Eloquent Query:
SELECT * FROM service_package WHERE price IN ('110010', 'Test 02', '11009')
You can use the whereIn() like:
Using DB::table()
$data = \DB::table("service_package")
->whereIn("price", ['110010', 'Test 02', '11009'])
->get();
Using Eloquent
$data = App\ServicePackage::whereIn("price", ['110010', 'Test 02', '11009'])
->get();
Here, make sure you have created model ServicePackage for the table service_package.

How can i write nested query in laravel

SELECT * FROM posts WHERE user_id IN (SELECT following FROM follows WHERE user_id='1'
This is the query i want to wirte in laravel to fetch the data how can i write this in laravel
This should work:
$result = DB::table('posts')
->whereIn('user_id', function ($query) {
$query->select('following')
->from('follows')
->where('user_id', 1);
})->get();

Laravel 5.4 whereDate in Eloquent

How to use whereMonth, whereDate in Eloquent ORM? (I know it can in DB query). Im using 5.4
You can use all the methods from DB query in Eloquent models, Check the following samples you can easily capture it. Both the samples, produce the same result
Using query builder
$users = DB::table('users')
->whereDate('created_at', '2016-12-31')
->get();
Using model
$users = Users::whereDate('created_at', '2016-12-31')
->get();

Resources