How can I fetch a single column from a single row in Laravel 4?
Here's what I'm trying:
return DB::selectOne("
SELECT EXISTS(
SELECT *
FROM permissions p
JOIN role_permissions rp ON rp.permission_id=p.id
JOIN user_roles ur ON ur.role_id=rp.role_id
WHERE ur.user_id=? AND p.code=?
)
",[Auth::user()->id,$perm_code]);
selectOne returns this ugly object though:
object(stdClass)#297 (1) {
["EXISTS(
SELECT *
FROM permissions p
JOIN role_permissions rp ON rp.permission_id=p.id
JOIN user_roles ur ON ur.role_id=rp.role_id
WHERE ur.user_id=? AND p.code=?
"]=>
string(1) "0"
}
Is there something better?
Using PDO is ugly too:
$stmt = DB::getPdo()->prepare("
SELECT EXISTS(
SELECT *
FROM permissions p
JOIN role_permissions rp ON rp.permission_id=p.id
JOIN user_roles ur ON ur.role_id=rp.role_id
WHERE ur.user_id=? AND p.code=?
)
");
$stmt->execute([Auth::user()->id,$perm_code]);
return (bool)$stmt->fetchColumn();
Use this:
DB::table('users')->pluck('columname');
From the inline documentation:
/**
* Pluck a single column's value from the first result of a query.
*
* #param string $column
* #return mixed
*/
Is this what you are looking for?
DB::table('users')->select('columnName')->join('whatever',...,...,...)->take(1)->get();
Another example
DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('columnName')
->take(1)
->get();
Raw didn't worked for you?:
return DB::select(DB::raw("
SELECT EXISTS(
SELECT *
FROM permissions p
JOIN role_permissions rp ON rp.permission_id=p.id
JOIN user_roles ur ON ur.role_id=rp.role_id
WHERE ur.user_id=? AND p.code=?
))
",[Auth::user()->id,$perm_code]);
And you can process that object to get what you want:
$result = DB::select(DB::raw("
SELECT EXISTS(
SELECT *
FROM permissions p
JOIN role_permissions rp ON rp.permission_id=p.id
JOIN user_roles ur ON ur.role_id=rp.role_id
WHERE ur.user_id=? AND p.code=?
) as exists)
",[Auth::user()->id,$perm_code]);
$result[0]->exists ? '1' : '0'
This is a test I just did here:
Route::any('test', ['as' => 'test', function()
{
$result = DB::select(DB::raw('select exists(select * from users) as exists;'));
dd($result[0]->exists ? '1' : '0');
}]);
Worked like a charm.
To fetch a single value from the first result, you can use \Illuminate\Database\Query\Builder::value. e.g.
$lastInvoice = DB::table('booking_groups')
->where('company_id','=',$booking->company_id)
->orderBy('invoice_number','desc')
->value('invoice_number');
Related
How to make with laravel a query with with like the example below
Thank you
with s as (select s.id from s)
select a.*
from a
left join s on s.id = a.s_id
...
WITH clauses are not supported by Eloquent. You can either pass in a raw query
$sql = <<<SQL
with s as (select s.id from s)
select a.*
from a
left join s on s.id = a.s_id
...
SQL;
$results = DB::select($sql);
Or use a package that adds this functionality like staudenmeir/laravel-cte
DB::query()
->withExpression('s', fn ($query) => $query->select('s.id')->from('s'))
->select('a.*')
->from('a')
->leftJoin('s', 's.id', 'a.s_id')
...
$users = SELECT users.id,
users.name,
cd.title,
cd.updated_at,
profiles.avatar
FROM `users`
JOIN (SELECT Max(id) max_id,
user_id
FROM book
GROUP BY user_id) c_max
ON ( c_max.user_id = users.id )
JOIN book cd
ON ( cd.id = c_max.user_id )
JOIN profiles
ON ( profiles.user_id = users.id )
ORDER BY cd.updated_at DESC
i am using this to get data in laravel query
$users = DB::select(DB::raw('SELECT users.id, users.name, cd.title, cd.updated_at, profiles.avatar FROM users JOIN ( SELECT MAX(id) max_id, user_id FROM book GROUP BY user_id )
c_max ON (c_max.user_id = users.id) JOIN book cd ON (cd.id = c_max.user_id) JOIN profiles ON (profiles.user_id = users.id) ORDER by cd.updated_at DESC limit 24 offset 0'));
I want to join on a result of other select like this :
SELECT *
FROM TABLE1
JOIN (
SELECT cat_id FROM TABLE2 where brand_id = 2 GROUP BY TABLE2.cat_id) AS b ON TABLE1.id = b.cat_id
is there any way to do this with eloquent?
As it is mentioned here, using DB:raw() will solve your problem.
DB::table('table1')->join(DB::raw("(SELECT
cat_id
FROM table2
WHERE brand_id = 2
GROUP BY table2.cat_id
) as b"),function($join){
$join->on("b.cat_id","=","table1.id");
})->get();
\DB::table('table1')->join('table2' , function($join){
$join->on('table1.id', '=', 'table2.cat_id');
})->select(['table2.cat_id' , 'table1.*'])
->where('table2.brand_id' , '=' , '2')
->groupBy('table2.cat_id');
Depends on whether brand_id is in table1 or table2
You can also use model approach for it.
TABLE1::join('table2' , function($join){
$join->on('table1.id', '=', 'table2.cat_id');
})->select(['table2.cat_id' , 'table1.*'])
->where('table2.brand_id' , '=' , '2')
->groupBy('table2.cat_id');
I am new to laravel 5.2,and i want to know more about laravel query builder,
actually i wants to know that how can i write this SQL Query in laravel
SQL -> select d.*, c.*, ct.*, a.*, b.*, m.* FROM tbl_deal d
INNER JOIN tbl_category c ON d.deal_category_id = c.category_id
INNER JOIN tbl_brand b ON d.deal_brand_id = b.brand_id
INNER JOIN tbl_member m ON m.member_id = d.deal_member_id
INNER JOIN tbl_city ct ON FIND_IN_SET(ct.city_id,d.deal_city)
LEFT JOIN tbl_outlet o ON FIND_IN_SET(o.outlet_id,d.deal_outlets)
LEFT JOIN tbl_area a ON a.area_id = o.outlet_area_id
Please, try the query like this:
$result = DB::table('table_name')
->leftJoin('tbl_country_city', 'table_name.quiz_id', '=', 'tbl_country_city.quiz_id')
->whereRaw('FIND_IN_SET('.$b.',field_name)')
->get();
In Laravel 4.2, I am trying to achieve a query that returns all users, that have all of certain activities. As of now, I have a query that returns all users that have one of many activities:
//$selectedActivities being an array
$userByActivities = User::with('activities')
->whereHas('activities', function($query) use($selectedActivities){
$query->whereIn('id', $selectedActivities);
})->get();
To be more clear: given activities a,b,c. I am looking for all users that have activity a AND b AND c. My query returns all users that have activity a OR b OR c.
Thank you for your help.
EDIT:
The solution offered by lukasgeiter results in following query:
select * from `users` where
(select count(*) from `activities` inner join `activity_user` on `activities`.`id` = `activity_user`.`activity_id` where `activity_user`.`user_id` = `users`.`id` and `id` = '7') >= 1
and (select count(*) from `activities` inner join `activity_user` on `activities`.`id` = `activity_user`.`activity_id` where `activity_user`.`user_id` = `users`.`id` and `id` = '3') >= 1
and (select count(*) from `activities` inner join `activity_user` on `activities`.`id` = `activity_user`.`activity_id` where `activity_user`.`user_id` = `users`.`id` and `id` = '1') >= 1
and (select count(*) from `activities` inner join `activity_user` on `activities`.`id` = `activity_user`.`activity_id` where `activity_user`.`user_id` = `users`.`id` and `id` = '2') >= 1
Whereas the solution offered by Jarek Tkaczyk:
$userByActivities = User::with('activities')
->whereHas('activities', function($query) use($selectedActivities) {
$query->selectRaw('count(distinct id)')->whereIn('id', $selectedActivities);
}, '=', count($selectedActivities))->get();
for a similar request, results in following query:
select * from `users` where (select count(distinct id) from `activities`
inner join `activity_user` on `activities`.`id` = `activity_user`.`activity_id`
where `activity_user`.`user_id` = `users`.`id` and `id` in ('7', '3', '1', '2')) = 4
You'll have to add multiple whereHas for that:
$query = User::with('activities');
foreach($selectedActivities as $activityId){
$query->whereHas('activities', function($q) use ($activityId){
$q->where('id', $activityId);
});
}
$userByActivities = $query->get();
If you are getting Cardinality violation: 1241 Operand should contain 2 column(s) the problem is the nested selectCount adds to the normal select count(*) instead of overriding the existing select, so changing to $query->distinct()->whereIn('id', $selectedActivities); did the trick for me, or changing to $query->select(DB::raw(count(distinct id)))