0

I have this simple sql query

$orders=DB::table('carts')
                - 
 ->join('prod_suppliers','carts.prod_supplier_id','=','prod_suppliers.id')
 ->join('tickets','carts.ticket_id','=','tickets.id') 
 ->distinct()                                
  ->select(
     'suppliers.id as supplier_id',
     'suppliers.supplier',
     'suppliers.house_no_street_no',
     'barangays.brgy_name',                        
     'suppliers.city_id',
     'tickets.id',
     'tickets.ticket_no',                                                                                            

    )                        
  ->get();  

dd($orders); 

i want my result to be an object but im getting result as an array

Collection {#525 ▼
  #items: array:2 [▶]
}

what is the best way i can do to solve this problem?

1 Answer 1

1

It is a collection of Order objects. The dd() shows array, but those are objects inside that array. If you loop on the $orders variable, you will have ability to use object notation to get the fields you need.

For example:

foreach($objects as $object){
    echo ($object->id); // or whatever field you need from the $order object
}

This is actually where Laravel really shines though - it has a fantastic model system. Using a model to pull the data through a simple Eloquent query, along with any relationships you need, might make your like far easier.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.