Select single column from a row in Laravel?
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.