Laravel 5:“试图获取非对象的属性"从2个链接的模型中获取数据时 [英] Laravel 5: "Trying to get property of non-object" when getting data from 2 linked models
问题描述
我正在尝试创建一个简单的Laravel 5应用进行测试,但我仍在努力通过模型关系显示所需的数据. 当我通过手工艺修补匠测试模型关系时,一切工作正常. 在我的索引视图中,一切都很好...直到我使用第二个链接模型,这给了我该主题中的错误.
I'm trying to create a simple Laravel 5 app for testing purposes and I'm still struggling with displaying the desired data through models relationships. Everything works fine when I test models relationships through artisan tinker. In my index view, everything is fine... until I use a second linked model, which gives me the error in the subject.
这是我的表格(迁移):
Here are my tables (migrations):
Schema::create('orders', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
$table->string('status');
});
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
$table->integer('order_id')->unsigned();
$table->timestamps();
$table->string('name');
$table->string('sn');
});
Schema::create('services', function(Blueprint $table)
{
$table->increments('id');
$table->integer('order_id')->unsigned();
$table->timestamps();
$table->string('name');
$table->string('type');
});
这是我的3个型号:
//文件:App \ Order.php
//File: App\Order.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model {
public function products()
{
return $this->hasMany('App\Product');
}
public function services()
{
return $this->hasMany('App\Service');
}
}
//文件:App \ Product.php
//File: App\Product.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model {
public function order()
{
return $this->belongsTo('App\Order');
}
}
//文件:App \ Service.php
//File: App\Service.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Service extends Model {
public function order()
{
return $this->belongsTo('App\Order');
}
}
///我的简单控制器OrdersController.php
//My simple controller OrdersController.php
public function index()
{
$orders = Order::all();
return view('orders.index', compact('orders'));
}
最后我的观点是"/resources/views/orders/index.blade.php"
And finally my view "/resources/views/orders/index.blade.php"
<ul>
@foreach( $orders as $order )
<li>
Order# {{ $order->id }} - //this line is OK
{{$order->products->first()->name}} - //this line is OK
{{$order->services->first()->name}} - //as soon as I add this line, I get the error!
</li>
@endforeach
</ul>
=>我做错什么了吗?是否不可能从同一视图中的2个外部"模型中检索数据? 非常感谢您的帮助!
=> Am I doing something wrong? Isn't it possible to retrieve data from 2 "external" models within the same view? Thanks a lot for your help!
推荐答案
每个订单是否至少分配了一项服务?我的猜测不是,在这种情况下,您应该将其包装在if语句中:
Does every order have at least one service assigned? My guess is not, in which case you should wrap it in an if statement:
@foreach( $dossiers as $dossier )
<li>
Order# {{ $order->id }}
@if($product = $order->products->first())
{{ $product->name }}
@endif
@if($service = $order->services->first())
{{ $service->name }}
@endif
</li>
@endforeach
这篇关于Laravel 5:“试图获取非对象的属性"从2个链接的模型中获取数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!