简体   繁体   中英

How to compare row data in Laravel 5.2

I need to check my table data using AND operator in Laravel and view this like

    if(Permission::where('status', '=', '1')->first()) AND (Permission::where('project_id', '=', '$id')->first())return view('collaborators.show')->withProject($project)->withTasks($tasks)->withFiles($files)->withComments($comments)->withCollaborators($collaborators);
else
    return('hi');

but I'm getting the following error

syntax error, unexpected 'else' (T_ELSE)

You were missing parethesis after if and before return keywords.

Here's code with proper syntax for you:

if ((Permission::where('status', '=', '1')->first()) AND (Permission::where('project_id', '=', '$id')->first())) {
    return view('collaborators.show')->withProject($project)->withTasks($tasks)->withFiles($files)->withComments($comments)->withCollaborators($collaborators);
} else {
    return 'hi';
}

I'm not sure but perhaps this is what you wanted to achieve

if (Permission::where('status', 1)->where('project_id', $id)->exists()) {
    return view('collaborators.show')
        ->withProject($project)
        ->withTasks($tasks)
        ->withFiles($files)
        ->withComments($comments)
        ->withCollaborators($collaborators);
} else {
    return 'hi';
}

The below code is formatted properly. It should fix your error

if(Permission::where('status', '=', '1')->first() && Permission::where('project_id', '=', '$id')->first()){
    return view('collaborators.show')
        ->withProject($project)
        ->withTasks($tasks)
        ->withFiles($files)
        ->withComments($comments)
        ->withCollaborators($collaborators);
}else{
    return('hi');
}

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