Laravel troubles with WHERE in relationships

BLDD

I'm having trouble when a user is not an admin. The goal is to get only those requests that belong to the user, but when I use the where clause, I get all the requests from the DB. It was supposed to get all the requests only for an admin.

Thank you for the help!

public function index(){
    $status = request('status', -1);
    $paper_size = request('paper_size', -1);

    if (auth()->user()->isAdmin()) {
        $requests = Request::
            where('paper_size', $paper_size)->orWhereRaw($paper_size. ' = -1')->
            where('status', $status)->orWhereRaw($status. ' = -1')->


            orderBy(
                request('orderby') ? request('orderby') : 'created_at',
                request('order') ? request('order') : 'DESC'
            )->paginate(10);

        $departments = Departament::All();


        return view('Requests.index', compact('requests', 'departments'));
    }

    $requests = auth()->user()->requests()->
        where('status', $status)->orWhereRaw($status. ' = -1')->
        where('paper_size', $paper_size)->orWhereRaw($paper_size. ' = -1')->
        orderBy(
            request('orderby') ? request('orderby') : 'created_at',
            request('order') ? request('order') : 'DESC'
        )->paginate(10);

        return view('Requests.index', compact('requests'));
}

UPDATE:

I can already list all user requests, but the status filter does not work.

Ps: the filter "paper_size" is working as expected

SOLVED:

Thanks to the whole community, and especially to @Sandeesh

    public function index(){
    request('status') == -1 || request('status') == null ?
        $statusExists = false : $statusExists = true;
    $status = request('status');

    request('paper_size') == -1 || request('paper_size') == null ?
        $paper_sizeExists = false : $paper_sizeExists = true;
    $paper_size = request('paper_size');

    $is_admin = auth()->user()->isAdmin();

    $requests = Request::when($statusExists, function ($query) use ($status) {
        return $query->where('status', $status);
    })
        ->when($paper_sizeExists, function ($query) use ($paper_size) {
            return $query->where('paper_size', $paper_size);
        })
        ->when(!$is_admin, function ($query) {
            return $query->where('owner_id', auth()->id());
        })
        ->orderBy(request('orderby', 'created_at'), request('order', 'desc'))
        ->paginate(10);

    if (!$is_admin) {
        return view('Requests.index', compact('requests'));
    }

    $departments = Departament::all();

    return view('Requests.index', compact('requests', 'departments'));
}
Sandeesh

Wrap your where and orWhereRaw conditions together for a single column. Or use when instead of the workaround you apply with -1 = -1. I've also refactored the code for you.

public function index()
{
    $status = request('status');
    $paper_size = request('paper_size');

    $is_admin = auth()->user()->isAdmin();

    $requests = Request::when(!is_null($status), function ($query) use ($status) {
            return $query->where('status', $status);
        })
        ->when(!is_null($paper_size), function ($query) use ($paper_size) {
            return $query->where('paper_size', $paper_size);
        })
        ->when(!$is_admin, function ($query) {
            return $query->where('owner_id', auth()->id());
        })
        ->orderBy(request('orderby', 'created_at'), request('order', 'desc'))
        ->paginate(10);

    if (!$is_admin) {
        return view('Requests.index', compact('requests'));
    }

    $departments = Departament::all();

    return view('Requests.index', compact('requests', 'departments'));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Laravel relationships use Where

From Dev

Eloquent / Laravel - Putting a WHERE Clause on a Reference Table With Chained Relationships

From Dev

Relationships and with in Laravel?

From Dev

Laravel Relationships (With)

From Dev

how to handle relationships in laravel where the foreign key field is not required and may sometimes be left out

From Dev

how to handle relationships in laravel where the foreign key field is not required and may sometimes be left out

From Dev

How to count the number of rows coming out of a too many relationships where there are two conditions in Laravel

From Dev

Having troubles with Bootstrap scaffolding in Laravel 7.6

From Dev

Having troubles with Multi-Auth in Laravel 4

From Dev

Contact Form Laravel 4- Troubles with routes

From Dev

Laravel Category Model Relationships

From Dev

Laravel - Database relationships

From Dev

Laravel nested relationships

From Dev

Laravel merge relationships

From Dev

Laravel Recursive Relationships

From Dev

Laravel - three relationships

From Dev

Laravel Eloquent querying relationships

From Dev

Laravel whereHas on multiple relationships

From Dev

Populate laravel object with relationships

From Dev

Laravel Eloquent Relationships

From Dev

Laravel: search through relationships?

From Dev

Laravel 5 Nested Relationships

From Dev

Laravel Auth::user() relationships

From Dev

Loop through relationships Laravel

From Dev

Laravel Defining Relationships

From Dev

Laravel Relationships 2 tables

From Dev

Reverse model relationships in Laravel

From Dev

Laravel merge relationships

From Dev

Laravel not looking at relationships