Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select single column from a row in Laravel?

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();
like image 303
mpen Avatar asked May 10 '14 21:05

mpen


2 Answers

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
 */
like image 148
Kurt Beheydt Avatar answered Oct 19 '22 03:10

Kurt Beheydt


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.

like image 45
Antonio Carlos Ribeiro Avatar answered Oct 19 '22 05:10

Antonio Carlos Ribeiro