我是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);
}
我有视图admin.allorders
@foreach ($orderss as $tag)
<span class="label label-primary">{{ $tag['customer_id']}}</span>
@endforeach
我确定$orderss
有数据,但视图中没有显示。
答案 0 :(得分:7)
您需要添加get()
来执行查询:
$orderss = Order::where('customer_id', $customer_id1)->get();
此外,您可以使用关系代替此:
$customerss=Customer::where('mobile_number',$param1)->first();
$customer_id1=$customerss->id;
$orderss= Order::where('customer_id',$customer_id1);
您只需一个查询即可:
$orderss = Order::whereHas('customer', function($q) use($param1) {
$q->where('mobile_number', $param1);
})->get();
要使其有效,请在Order
模型中定义此关系:
public function customer()
{
return $this->belongsTo(Customer::class);
}