Mirza Chilman
Mirza Chilman

Reputation: 429

show/hide record based on field Laravel 5

I have table users, which contain users that can log in into my web, so i have something like this, u can see that admin have privileges 1 while qwert have privileges 2, i want to display anything that have privileges 2, how can i do something like that?

enter image description here

in my web it's look something like this, u can see that admin been showed in that table, i don't want admin record to be shown, i just want to display record that have privileges 2 enter image description here

VIEW

@foreach($users as $user) 
        <tr class="odd gradeX">
            <td>{{$user->cv_name}}</td>
            <td>{{$user->cv_position}}</td>
            <td>{{$user->cv_country}}</td>
            <td>{{$user->email}}</td>
            <td>{{$user->cn_phone}}</td>
            <td>{{$user->cv_address}}</td>
            <td>
                <a class="btn btn-success" title="edit" data-id={{$user->id}} data-action="user-edit"><i class="glyphicon glyphicon-pencil"></i></a>
                <a class="btn btn-danger" title="delete" data-id={{$user->id}} data-action="user-delete"><i class="glyphicon glyphicon-trash"></i></a>
            </td>
        </tr>
@endforeach

CONTROLLER

public function index(){
    return view('user')
    ->with('title','User')
    ->with('users',User::all());
}

public function getAll(){
    $data = User::all();
    $response = array(
            'content' => $data,
            'status' => 'success',
        );
    return Response::json($response);
} 

how to do that? certain if else case maybe?

Upvotes: 0

Views: 1726

Answers (1)

KmasterYC
KmasterYC

Reputation: 2354

Another good practice is using scope like this

- In User model

public function scopeMember($query){
    return $query->where('privileges','<>',1);
}

- In Controller. So easily to get only member

Change

->with('users',User::all());

To

->with('users',User::member()->get());

Upvotes: 3

Related Questions