fahi
fahi

Reputation: 55

laravel no result from my query

I'm a beginner in Laravel

public function allorders($param1){
    $customerss=Customer::where('mobile_number',$param1)->first();
    $customer_id1=$customerss->id;
    $orderss= Order::where('customer_id',$customer_id1);

    return view('admin.allorders')->with('orderss', $orderss);
}

and i have the view admin.allorders

@foreach ($orderss as $tag)
    <span class="label label-primary">{{  $tag['customer_id']}}</span>
@endforeach

I'm sure the $orderss is having data but it's not shown in the view.

Upvotes: 1

Views: 47

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

You need to add get() to execute the query:

$orderss = Order::where('customer_id', $customer_id1)->get();

Also, you could use relationships instead of this:

$customerss=Customer::where('mobile_number',$param1)->first();       
$customer_id1=$customerss->id;
$orderss= Order::where('customer_id',$customer_id1);

You could do the same with just one query:

$orderss = Order::whereHas('customer', function($q) use($param1) {
    $q->where('mobile_number', $param1);
})->get();

To make it work, define this relationship in the Order model:

public function customer()
{
    return $this->belongsTo(Customer::class);
}

Upvotes: 7

Related Questions