简体   繁体   中英

How to set condition in Query Builder?

I'm trying to set a condition in my function where I need set my column role_id equal to 1 if it's true it will return me a view. First I retrieve all the column in my users table $result = DB::table('users')->get(); after that I set a if condition.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Requests;
use DB;

class DocumentController extends Controller
{
    public function getDocuments()
    {
        $result = DB::table('users')->get();


        if ('role_id' == 1)
        {
            return view ('document.create');
        }
    }
}

I tried to set my role_id == 1 but it says.

Use of undefined constant role_id - assumed 'role_id'

try this...

// if you want to fetch only user with role_id = 1
    $users = DB::table('users')->where('role_id', '=', 1)->get();

in your case you can use condition like

if( $users->role_id == 1 )
{

}

如果要检查当前用户,只需使用Auth::user()

 if(\Auth::user()->role_id == 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM