Php 拉威尔的不动产关系有时不起作用
我有拉威尔5号。bacis电子商店的框架,我尝试使用关系,我有订单,订单项目,产品,客户模型 订单模型与Php 拉威尔的不动产关系有时不起作用,php,laravel,relationship,laravel-5.3,Php,Laravel,Relationship,Laravel 5.3,我有拉威尔5号。bacis电子商店的框架,我尝试使用关系,我有订单,订单项目,产品,客户模型 订单模型与 class Order extends Model { protected $fillable = ['user_id', 'status']; protected $dates = ['created_at']; /** * Get the items for the order. */ public function items()
class Order extends Model
{
protected $fillable = ['user_id', 'status'];
protected $dates = ['created_at'];
/**
* Get the items for the order.
*/
public function items()
{
return $this->hasMany('App\Order_item');
}
public function customer()
{
return $this->hasOne('App\Customer','id');
}
}
订单项目
class Order_item extends Model
{
protected $fillable = ['order_id', 'product_id', 'quantity', 'price'];
public function product()
{
return $this->hasOne('App\Product','id');
}
}
内部控制器
public function detail($id)
{
$order = Order::with('customer','items')->findOrFail($id);
return view('admin.detail', ['order' => $order]);
}
看来我有
<h2>Objednávka č. {{ $order->id }}</h2>
<h3>Zákazník:</h3>
{{ $order->customer->firstname }} {{ $order->customer->lastname }}
<p>{{ $order->customer->email }}</p>
{{ $order->customer->phone }}
<h3>Adresa:</h3>
<p>{{ $order->customer->street }}</p>
<p>{{ $order->customer->city }}</p>
<p>{{ $order->customer->psc }}</p>
<h3>Položky:</h3>
<table class="table table-bordered table-striped">
<thead>
<th>Název</th>
<th>Počet</th>
<th>Cena</th>
</thead>
<tbody>
@foreach($order->items as $item)
<tr>
<th>{{ $item->product->name }}</th>
<th>{{ $item->quantity }}</th>
<th>{{ $item->price }}</th>
</tr>
@endforeach
</tbody>
</table>
Objednávkač。{{$order->id}
扎卡兹尼克:
{{$order->customer->firstname}{{{$order->customer->lastname}
{{$order->customer->email}
{{$order->customer->phone}
肾上腺:
{{$order->customer->street}
{{$order->customer->city}
{{$order->customer->psc}
波罗ž基:
纳泽夫
波切特
塞纳
@foreach($order->items as$item)
{{$item->product->name}
{{$item->quantity}
{{$item->price}
@endforeach
现在,当我为某人测试代码order is function,但对某人no,问题出在{{{$item->product->name}
我的关系还好吗?这对我的网店关系来说是一个更好的解决方案
我将我的完整代码发布到github这样做
$order = DB::table('order')
->join('customer', 'order.customer_id', '=', 'customer.id')
->select('order.*', 'customer.*')
->where('order.id', '=', $id)
->first();
$order_items = Order_item::where('order_id', $id)->get();
return view('admin.detail', ['order' => $order, 'order_items' => $order_items]);
@foreach($order_items as $item)
<tr>
<th>{{ $item->product->name }}</th>
<th>{{ $item->quantity }}</th>
<th>{{ $item->price }}</th>
</tr>
@endforeach
并生成这样的项目
$order = DB::table('order')
->join('customer', 'order.customer_id', '=', 'customer.id')
->select('order.*', 'customer.*')
->where('order.id', '=', $id)
->first();
$order_items = Order_item::where('order_id', $id)->get();
return view('admin.detail', ['order' => $order, 'order_items' => $order_items]);
@foreach($order_items as $item)
<tr>
<th>{{ $item->product->name }}</th>
<th>{{ $item->quantity }}</th>
<th>{{ $item->price }}</th>
</tr>
@endforeach
@foreach($order\u items作为$item)
{{$item->product->name}
{{$item->quantity}
{{$item->price}
@endforeach
你喜欢这样吗
$order = DB::table('order')
->join('customer', 'order.customer_id', '=', 'customer.id')
->select('order.*', 'customer.*')
->where('order.id', '=', $id)
->first();
$order_items = Order_item::where('order_id', $id)->get();
return view('admin.detail', ['order' => $order, 'order_items' => $order_items]);
@foreach($order_items as $item)
<tr>
<th>{{ $item->product->name }}</th>
<th>{{ $item->quantity }}</th>
<th>{{ $item->price }}</th>
</tr>
@endforeach
并生成这样的项目
$order = DB::table('order')
->join('customer', 'order.customer_id', '=', 'customer.id')
->select('order.*', 'customer.*')
->where('order.id', '=', $id)
->first();
$order_items = Order_item::where('order_id', $id)->get();
return view('admin.detail', ['order' => $order, 'order_items' => $order_items]);
@foreach($order_items as $item)
<tr>
<th>{{ $item->product->name }}</th>
<th>{{ $item->quantity }}</th>
<th>{{ $item->price }}</th>
</tr>
@endforeach
@foreach($order\u items作为$item)
{{$item->product->name}
{{$item->quantity}
{{$item->price}
@endforeach
您的订单项目表是一个。与订单和产品的关系应声明为归属关系
- belongstomy用于多对多李>
- hasMany用于一对多模式
- hasOne用于一对一
class order\u产品扩展模型
{
受保护的$fillable=[“订单id”、“产品id”、“数量”、“价格”];
公共功能产品()
{
返回$this->belongtomany('App\Product','order\u Product','order\u id','Product\u id');
}
公共职能令()
{
返回$this->belongtomany('App\Order','Order\u product','product\u id','Order\u id');
}
}
您的订单项目表是一个。与订单和产品的关系应声明为归属关系
- belongstomy用于多对多李>
- hasMany用于一对多模式
- hasOne用于一对一
class order\u产品扩展模型
{
受保护的$fillable=[“订单id”、“产品id”、“数量”、“价格”];
公共功能产品()
{
返回$this->belongtomany('App\Product','order\u Product','order\u id','Product\u id');
}
公共职能令()
{
返回$this->belongtomany('App\Order','Order\u product','product\u id','Order\u id');
}
}
这段代码产生了同样的问题,两个顺序的delai在视图中是正确的,两个没有错误,试图获取非对象$order=order::where('id',$id)->first()的属性;还是一样的问题对不起,查询错了。。使用此$order=DB::table('order')->join('customer','order.customer_id','=','customer.id')->select('order.*','customer.*')->其中('order.id','=',$id)->first();这段代码产生了同样的问题,两个顺序的delai在视图中是正确的,两个没有错误,试图获取非对象$order=order::where('id',$id)->first()的属性;还是一样的问题对不起,查询错了。。使用此$order=DB::table('order')->join('customer','order.customer_id','=','customer.id')->select('order.*','customer.*')->其中('order.id','=',$id)->first();让我们那样比较容易,我们走吧。那样比较容易。