راجہ سفیان
راجہ سفیان

Reputation: 27

change color of row if data comes from other id in laravel datatable

I am currently showing data of mine and my friend in yajra laravel datatable but I want to change color of row which have data of my friends. this is how I got data of two different users

$user = Auth::user();
$friend_ids = $user->friends()->pluck('friend_id')->toArray();
$posts = PostModel::whereIn('users_id', $friend_ids)
            ->leftJoin('users', 'posts.users_id', '=', 'users.id')
            ->orWhere('users_id',auth()->user()->id)
            ->select('posts.id as id','posts.caption','posts.image')
            ->get();
return Datatables::of($posts )
       ->addIndexColumn()
       ->addColumn('action', function($row){
         $btn = '<a href="#" class="btn btn-primary btn-sm">Edit</a>';
         $btn = $btn. '<a href="#" class="btn btn-success btn-sm">delete</a>';
         return $btn;
       })
       ->rawColumns(['action'])
       ->make(true);

e.g currently my login id is 1 and my friend id is 3 so I want to change color of table row which comes from user id 3, which is of my friend so how should I implement this in my code

Upvotes: 0

Views: 1293

Answers (1)

Farhan Ibn Wahid
Farhan Ibn Wahid

Reputation: 1012

Source:

yajra docs here

Solution:

use setRowClass like below:

->rawColumns(['action'])
->setRowClass(function ($user) {
    if($user-id == 1){
        return 'alert-primary' 
    } else if($user-id == 3){
        return 'alert-info';
    } else {
        //set a default color
        return 'alert-success';
    }
})
->make(true);

Upvotes: 1

Related Questions