I have difficulty to convert raw SQL query to eloquent model with relations.
This is the raw SQL query I have constructed but I cannot read the count inside the whereHas function of the eloquent.
Raw SQL query:
select *
from `subscriptions` where (`user_id` = 1) and
exists (select count(subscription_infos.id) as rec from `subscription_infos` where `subscriptions`.`id` = `subscription_infos`.`subscription_id` and `status` = 0 HAVING rec > 1)
Attempt Laravel Eloquent Model
$chk = Subscription::where(["user_id" => $userId])
->with(['info' => function($q){
return $q->where("status",0);
}])
->whereHas('info', function($q){
return $q->where("status",0);
})
->has('info','>',5)
->get();
I have setup a relation called "info" in the Subscription Model
public function info(){
return $this->hasMany(SubscriptionInfo::class,"subscription_id","id");
}
I really appreciate for those contributor that help me.
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.
I have a function to get a pass a language number to get language categories record for API purpose. I use a database query statement to select categories table and join the category language table to get category id, parent_id and name (specified language). When execute return error and select the underlying SQL converted the language value to string (e.g. languages_id = 1). I google a lot and no ideas what's wrong. Can anyone advise how to resolve. Thanks a lot.
I tried to copy the underlying SQL to MySQL Workbench and remove the languages_id = 1 --> languages_id = 1 can working properly. I guess the 1 caused error.
Code Sample:
private function getCategories($language) {
$categories = DB::table('categories')
->select(DB::raw('categories.id, categories.parent_id, categories_translation.name'))
->join('categories_translation', function($join) use ($language) {
$join->on('categories_translation.categories_id', '=', 'categories.id');
$join->on('categories_translation.languages_id', '=', $language);
})
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id]
])
->get();
return $categories;
}
Error return the converted SQL:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'on
clause' (SQL: select categories.id, categories.parent_id,
categories_translation.name from categories inner join
categories_translation on categories_translation.categories_id =
categories.id and categories_translation.languages_id = 1
where (parent_id = 0 and categories.id = 1))"
You are trying to join using a comparison to an scalar value, instead of a column. I think you actually want to put that comparison as a "where" condition, rather than a "join on"
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id],
['categories_translation.languages_id', '=', $language]
])
there is another thing i just discover with your code. when joining table, you are suppose to be joining 'categories_translation.languages_id' with another table id field. in your case, it is not so. you are not joining 'categories_translation.languages_id' with any table field. so ideally, what you are going to do is this
private function getCategories($language) {
$categories = DB::table('categories')
->select(DB::raw('categories.id, categories.parent_id, categories_translation.name'))
->join('categories_translation', function($join) use ($language) {
$join->on('categories_translation.categories_id', '=', 'categories.id');
})
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id]
['categories_translation.languages_id', '=', $language]
])
->get();
return $categories;
}
hope this helps
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();
SELECT itemid,COUNT(itemid),itemname FROM `items`
WHERE DATE(`save_at`) ='2015-12-16'
GROUP BY itemid
Anyone know how to write this sql in laravel 5 using query builder ???
this is the code i used but it didn't work for me
$query = DB::table('items')
->select(itemid, DB::raw('COUNT(itemid) as noitems')),itemname)
->where( DATE(`save_at`),'=', 2015-12-16)
->group_by('itemid')
->get();
Check the below code.
$query = DB::table('items')
->select(itemid, DB::raw('COUNT(itemid) as noitems')),itemname)
->where( DB::raw('DATE(`save_at`)'),'=', '2015-12-16')
->groupBy('itemid')
->get();