skip to Main Content

I am trying with laravel controller to get data with condition.

First, i got all the data "orders" from my data base.

public function getAllInternOrders(Request $request)
    {
        $userData = $request;

         $orders = InternOrders::all();

         $sortedOrders = $this->sortOrdersBasedOnUserRole($orders, $userData);

        return response()->json($sortedOrders);
    }

Then, in a private function I sort the data "orders" and return them.
userData is an object with multiples values.
userName is a string .
userRole is an array with multiple or one value.

    private function sortOrdersBasedOnUserRole($orders, $userData)
{
    $sortedOrders = null;
    $userName = $userData->get('displayName');
    $userRole = $userData->get('userRole');

    if (in_array("1", $userRole)) {
         $sortedOrders  = $orders->where('Referent', $userName);
    } elseif (in_array("2", $userRole)) {
         $sortedOrders = $orders->where('Status', '>', 0);
    } elseif (in_array("3", $userRole)) {
         $sortedOrders = $orders->where('Status', 2)->orWhere('Status', 3);
    }
    return $sortedOrders;
}

My problem :

When I enter in the first condition, everything is ok. I got my data in an array.
When I enter in the second condition, I got no error but my data is inside an object (instead of an array like the first condition).
When I enter in the condition number three, I got an empty array and the error "Method IlluminateDatabaseEloquentCollection::orWhere does not exist."

What I have already try :

  • to add "->get();" in the end for the $sortedOrders.
  • I have seen that the error for "orWhere" is because "orders" is an object instead of an array, but I have also tried to convert orders into an array but still got the same error.
  • I have seen in stack over flow post,that I am returning a response inside a response but i have tried to render response()->json() but still got the error
  • I have tried to convert the response in my front end into an array. It is working for condition 1 and 2 but not for the condition number three who still have the same error.

2

Answers


  1. When you do $orders = InternOrders::all(), $orders is a collection. So, you can’t call orWhere method because collection doesn’t have that method.

    You should call where and orWhere method of Builder. You can consider doing like below:

    public function getAllInternOrders(Request $request)
    {
        $userData = $request;
        $sortedOrders = $this->sortOrdersBasedOnUserRole($userData);
    
        return response()->json($sortedOrders);
    }
    
    private function sortOrdersBasedOnUserRole($userData)
    {
        $userName = $userData->get('displayName');
        $userRole = $userData->get('userRole');
    
        $query = InternOrders::query();
        
        if (in_array("1", $userRole)) {
            $query->where('Referent', $userName);
        } elseif (in_array("2", $userRole)) {
            $query->where('Status', '>', 0);
        } elseif (in_array("3", $userRole)) {
            $query->where('Status', 2)->orWhere('Status', 3);
        }
    
        return $query->get();
    }
    
    Login or Signup to reply.
  2. You need to refactor your code. When you get Order::all() -> you retrieve a collection instead of query, so you are performing where() on collection.
    Try something like this :

    public function getAllInternOrders(Request $request)
    {
        $userData = $request;
    
        $sortedOrders = $this->getOrdersBasedOnUserRole($userData);
    
        return response()->json($sortedOrders);
    }
    
    
    private function getOrdersBasedOnUserRole($userData)
    {
        $userName = $userData->get('displayName');
        $userRole = $userData->get('userRole');
    
        if (in_array("1", $userRole)) {
             $sortedOrders  = InternOrders::where('Referent', $userName);
        } elseif (in_array("2", $userRole)) {
             $sortedOrders = InternOrders::where('Status', '>', 0);
        } elseif (in_array("3", $userRole)) {
             $sortedOrders = InternOrders::where('Status', 2)->orWhere('Status', 3);
        }
        return $sortedOrders->get()->toArray(); //convert to array for json.
    }
    

    Also i advise you to validate request before using variables $userName and $userRole and rename InternOrders into singular name InternOrder.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search