Php 在Laravel中显示从一到多的关系
我想在视图中显示我的位置名称,但我不明白我做错了什么 位置模型-Php 在Laravel中显示从一到多的关系,php,laravel,Php,Laravel,我想在视图中显示我的位置名称,但我不明白我做错了什么 位置模型- public function orderz() { return $this->hasMany('App\Models\Order', 'location', 'location_id'); } } 订单模式 public function location() { return $this->belongsTo('App\Models\Order'); } public function sh
public function orderz()
{
return $this->hasMany('App\Models\Order', 'location', 'location_id');
}
}
订单模式
public function location()
{
return $this->belongsTo('App\Models\Order');
}
public function show($id)
{
$locations = Location::find($id)->orderz()->whereDate('created_at', '=', Carbon::today()->toDateString());
$orders = Order::all();
$wallets = Wallet::all();
//$orders = DB::table('orders')->where('location', '=', 'New York')->get();
return view('location')
->with('locations', $locations)
->with('wallets', $wallets)
->with('orders', $orders);
}
LocationsController.php-
public function location()
{
return $this->belongsTo('App\Models\Order');
}
public function show($id)
{
$locations = Location::find($id)->orderz()->whereDate('created_at', '=', Carbon::today()->toDateString());
$orders = Order::all();
$wallets = Wallet::all();
//$orders = DB::table('orders')->where('location', '=', 'New York')->get();
return view('location')
->with('locations', $locations)
->with('wallets', $wallets)
->with('orders', $orders);
}
我这样称呼它:
@foreach ($locations as $location)
@foreach($location->orders as $order)
{{$order->location_id}}
@endforeach
@endforeach
当我从我的代码日期过滤器中删除它的工作,但我显示的订单从所有天
$locations = Location::find($id)->orders()->whereDate('created_at', '=', Carbon::today()->toDateString());
看起来是这样的:
$locations = Location::find($id)->orders;
您可以使用即时加载:
//Controller
$locationWithOrderz = Location::with('orderz')->where('id', $id)->first();
$wallets = Wallet::all();
return view('location')
->with('location', $locationWithOrderz)
->with('wallets', $wallets);
//blade
@foreach($location->orderz as $order)
{{$order->...}}
@endforeach
将控制器代码更改为类似以下内容
如果要按id获取位置,请执行以下操作:
public function show($id)
{
$locationWithOrderz = Location::with('orderz')->where('id', $id)->first();
$orders = Order::all();
$wallets = Wallet::all();
return view('location', compact('locationWithOrderz', 'wallets', 'orders'));
}
更新
检索单个记录时,不能循环遍历$locations。(谢谢@FouedMOUSSI)
要按日期获取位置:
public function index() //Changed function name and argument
{
$locations = Location::with('orderz')->whereDate('created_at', '=', Carbon::today()->toDateString())->get();
$orders = Order::all();
$wallets = Wallet::all();
return view('location', compact('locations', 'wallets', 'orders'));
}
注意:在使用Carbon时,您需要匹配created_中存储的确切日期格式(无分秒),否则在$locations中可能什么也得不到
在这里,您可以循环浏览以下位置:
@foreach ($locations as $location)
@foreach($location->orders as $order)
{{$order->location_id}}
@endforeach
@endforeach
确保在一对多关系中,第二个参数必须是订单模型中的外键。Eloquent将自动确定订单表上正确的外键列。现在你们的关系一定很好
public function orderz()
{
return $this->hasMany('App\Models\Order', 'foreign_key_in_order_model', 'primary_key_in_location_model');
}
您的最终代码应该如下所示
public function orderz()
{
return $this->hasMany('App\Models\Order', 'location_id', 'location');
}
我只是用不同的变量再次调用了我的位置,现在一切都好了
$loc = Location::find($id);
$locations = Location::find($id)->orders()->whereDate('created_at', '=', Carbon::today()->toDateString());
显示我正在使用的位置名称
{{$loc->location_id}}
您将关系命名为
orderz
,但将其称为orders
为什么调用->whereDate('created_at','=',Carbon::today()->toDateString())?在位置模型上,将orderz函数重命名为orders
,以及z
键入,如果这是标准的一对多关系,我认为你们的关系安排错了。您的位置模型orderz()
有许多订单;但是您的订单型号location()
。。。要点菜吗?那个应该是位置,我想?@apokryfos我重新命名了它,但问题不在它们身上。->where('id',$id)必须返回一个result@FouedOP在刀片服务器中的$locations中循环。AFAIK主键(id)必须是唯一的。所以,当您应用->where('id',$id)过滤器时,我很确定您只会得到一行。“对吗?”FouedMOUSSI说得对。我错过了。我会更新我的答案。谢谢。我试过用你的代码,但它也不起作用,而且用你的代码也不能计算位置的总价-“{{$locations->sum('total')}”是的,我修改了我的代码来获取我的ID,因为它们是我的默认主键,但仍然不起作用……我收到了这个消息:“未定义变量:订单(视图:C:\xampp\htdocs\softuni\ticket listing\resources\views\location.blade.php)