如何使用 laravel 中的模型来获取值?
how to use model in laravel to get value?
我有 3 个 table 订单、订单详情和产品 table 每个订单都是唯一的,每个订单都有多个带有订单 ID 的订单详情,每个订单详情都有 product_id
我在 blade
中使用 foreach
循环
@foreach($order->orderdetail as $odetail)
<tr>
<td><input type="number" value="{{ $odetail->amount }}" ></td>
</tr>
@endforeach
此 value="{{ $odetail->amount }}"
在输入字段中获取值 25
我有另一个 foreach 循环
@foreach($products as $key => $p)
<tr>
<td><input type="number" value="{{ $p->order->orderdetail->unit }}"></td>
</tr>
@endforeach
我想在第二个 foreach 循环中输出上面的循环 value="{{ $odetail->amount }}"
所以我试试这个
value="{{ $p->order->orderdetail->unit }}"
如果 orderdetail 在第一个循环中获得价值那么为什么它在第二个 foreach 循环中没有获得价值有人可以告诉我什么是错误的吗?doeas 模型是如何工作的以及我如何在第二个循环中获得价值?
这是我的订单模型 class 和 orderdetail 函数
class Order extends Model
{
public function orderdetail(){
return $this->hasMany('App\OrderDetail','order_id');
}
}
这是我的订单详情 class 和功能
class OrderDetail extends Model
{
public function order(){
return $this->belongsTo('App\Order');
}
public function product(){
return $this->belongsTo('App\Product');
}
}
这里是产品型号
class Product extends Model
{
public function category(){
return $this->belongsTo('App\Category');
}
}
orderdetai 的数据库图像
enter image description here
看来您只是错过了 Product
模型中的 order
关系
public function orders(){ // by laravel convention you need a plural name for hasMany relation
return $this->hasMany('App\order','product_id');
}
希望能解决您的问题
我有 3 个 table 订单、订单详情和产品 table 每个订单都是唯一的,每个订单都有多个带有订单 ID 的订单详情,每个订单详情都有 product_id
我在 blade
中使用foreach
循环
@foreach($order->orderdetail as $odetail)
<tr>
<td><input type="number" value="{{ $odetail->amount }}" ></td>
</tr>
@endforeach
此 value="{{ $odetail->amount }}"
在输入字段中获取值 25
我有另一个 foreach 循环
@foreach($products as $key => $p)
<tr>
<td><input type="number" value="{{ $p->order->orderdetail->unit }}"></td>
</tr>
@endforeach
我想在第二个 foreach 循环中输出上面的循环 value="{{ $odetail->amount }}"
所以我试试这个
value="{{ $p->order->orderdetail->unit }}"
如果 orderdetail 在第一个循环中获得价值那么为什么它在第二个 foreach 循环中没有获得价值有人可以告诉我什么是错误的吗?doeas 模型是如何工作的以及我如何在第二个循环中获得价值?
这是我的订单模型 class 和 orderdetail 函数
class Order extends Model
{
public function orderdetail(){
return $this->hasMany('App\OrderDetail','order_id');
}
}
这是我的订单详情 class 和功能
class OrderDetail extends Model
{
public function order(){
return $this->belongsTo('App\Order');
}
public function product(){
return $this->belongsTo('App\Product');
}
}
这里是产品型号
class Product extends Model
{
public function category(){
return $this->belongsTo('App\Category');
}
}
orderdetai 的数据库图像 enter image description here
看来您只是错过了 Product
模型中的 order
关系
public function orders(){ // by laravel convention you need a plural name for hasMany relation
return $this->hasMany('App\order','product_id');
}
希望能解决您的问题