未定义的属性:Illuminate \ Database \ Eloquent \ Collection :: Laravel 5.2 [英] Undefined property: Illuminate\Database\Eloquent\Collection:: Laravel 5.2
问题描述
我试图让物联网显示订单中的物品,但我不断收到此错误
I'm trying to get iot to show The items within an order and i keep getting this error
这些是我的模特
class westcoorder extends Model
{
protected $table = 'westcoorders';
protected $with = 'westcoorderitem';
protected $fillable = ['is_sent', 'is_delivered'];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function westcoorderitem()
{
return $this->hasMany('App\westcoorderitem');
}
}
class westcoorderitem extends Model
{
protected $table = 'westcoorderitems';
protected $fillable = ['westcoorder_id','quantity', 'productName', 'productCode', 'price'];
public function westcoorder()
{
return $this->belongsTo('App\westcoorder');
}
}
这是我的控制器
public function onGoingOrder($orderNumber)
{
$orderNumber = westcoorder::where('id', $orderNumber)->firstOrFail();
$items = westcoorderitem::where('westcoorder_id', $orderNumber)->get();
return view('westco.onGoingOrder', compact('orderNumber', 'items'));
}
这就是我的看法
<div class="panel-heading">Order @if ($orderNumber) {{ $orderNumber->id }} @endif Items</div>
<div class="panel-body">
@if($items)
{{ $items->productName }}
@endif
</div>
这是我的桌子的样子
Schema::create('westcoorders', function (Blueprint $table)
{
$table->increments('id');
$table->tinyInteger('is_sent')->default(0);
$table->tinyInteger('is_delivered')->default(0);
$table->timestamps();
} );
Schema::create('westcoorderitems', function (Blueprint $table)
{
$table->increments('id');
$table->Integer('westcoorder_id'); // fk for westcoOrder.id
$table->string('quantity');
$table->string('productName');
$table->string('productCode');
$table->decimal('price');
$table->timestamps();
} );
这是我得到的错误
Undefined property: Illuminate\Database\Eloquent\Collection::$productName
推荐答案
像您的错误状态一样:
未定义的属性:Illuminate \ Database \ Eloquent \ Collection :: $ productName
Undefined property: Illuminate\Database\Eloquent\Collection::$productName
您正在尝试访问集合中的属性,而不是模型. 首先,您可以像这样使用创建的关系:
You are trying to access a property on a Collection, instead of a Model. First, you can make use of the relationship you created, like so:
$order = App\westcoorder::where('id', $orderNumber)->with('westcoorderitem')->firstOrFail();
这将确保订单商品将包含在结果中,而不是执行另一个查询以获取订单商品.
This will ensure the order items will be included with the result, instead of executing another query to fetch them.
然后您可以将$order
传递到视图:
You can then pass on the $order
to the view:
return view('welcome', compact('orderNumber', 'order'));
(您也可以只删除作为实际订单的orderNumber)
然后,您可以在视图中访问order
并像这样循环遍历items
:
Then you can access the order
in your view and loop through the items
like this:
@foreach($order->westcoorderitem as $item)
{{ $item->productName }}
@endforeach
FK
另一个提示可能是更新表以使用索引来提高性能并使表整洁,就像您在create migration的注释中提到的FK
一样.您可以进行迁移进行更新,例如:
FK
Another tip could be to update your table to use indexes to improve performance and make it neat, like the FK
you mention in the comment of your create migration. You can make a migration to update it, like:
$table->foreign('westcoorder_id')->references('id')->on('westcoorders');
和/或根据您的需要(级联等)对此进行扩展.
And/or expand on this, according to your needs (cascading, etc).
这篇关于未定义的属性:Illuminate \ Database \ Eloquent \ Collection :: Laravel 5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!