我有这两个类:
<?php
class Client extends Eloquent
{
public function orders()
{
return $this->has_many('Order');
}
}
和订单类:
<?php
class Order extends Eloquent
{
public function client()
{
return $this->belongs_to('Client');
}
}
在我的控制器上,我正在使用以下命令加载订单:
$orders = Order::with('client')->order_by('id', 'asc');
它正在检索我的订单数据,事实上,它正在执行急切加载查询:
SELECT * FROM `orders` ORDER BY `id` ASC LIMIT 10 OFFSET 0
SELECT * FROM `clients` WHERE `id` IN ('1')
我可以使用以下方法打印订单数据:
$order->id
但是,当打印客户的数据时,使用:
$order->client->name
它显示此错误:
"Trying to get property of non-object"
这是堆栈跟踪:
#0 /home/instakioski/laravel/laravel.php(42): Laravel\Error::native(8, 'Trying to get p...', '/home/instakios...', 136)
#1 /home/instakioski/laravel/view.php(386) : eval()'d code(136): Laravel\{closure}(8, 'Trying to get p...', '/home/instakios...', 136, Array)
#2 /home/instakioski/laravel/view.php(386): eval()
#3 /home/instakioski/laravel/blade.php(71): Laravel\View->get()
#4 [internal function]: Laravel\{closure}(Object(Laravel\View))
#5 /home/instakioski/laravel/event.php(199): call_user_func_array(Object(Closure), Array)
#6 /home/instakioski/laravel/event.php(138): Laravel\Event::fire('laravel.view.en...', Array, true)
#7 /home/instakioski/laravel/view.php(348): Laravel\Event::until('laravel.view.en...', Array)
#8 /home/instakioski/laravel/view.php(590): Laravel\View->render()
#9 /home/instakioski/laravel/response.php(268): Laravel\View->__toString()
#10 /home/instakioski/laravel/laravel.php(180): Laravel\Response->render()
#11 /home/instakioski/public/index.php(34): require('/home/instakios...')
#12 {main}
我究竟做错了什么?