【发布时间】:2017-09-20 01:54:34
【问题描述】:
我有以下代码
public function detailCustomer(Customer $customer)
{
$vehicles = DB::table('vehicles')
->selectRaw('*')
->where('cust_id', '=', $customer->id);
return view('customers.detail', array('customer' => $customer, 'vehicles' => $vehicles));
}
其中表 vehicles 包含:
spz
cust_id <FK> //this is foreign key to customer->id
type
brand
在customers.detail 视图中,我尝试使用以下代码显示数据,但出现此错误:
Undefined property: Illuminate\Database\PostgresConnection::$spz
代码:
@if (count($vehicles) > 0)
<?php $i = 1; ?>
@foreach ($vehicles as $vehicle)
<?php $i++; ?>
<td>{{$vehicle->spz}}</td>
<td>{{$vehicle->type}}</td>
<td>{{$vehicle->brand}}</td>
@endforeach
@endif
我已经阅读了this topic,但似乎这不是我的问题,因为我使用foreach 来遍历对象,但似乎我没有将数据库中的对象放入我的$vehicles 变量中,因为在错误页面中,它还显示类似这样的内容:
'customer' => object(Customer), 'vehicles' => object(Builder)
是什么让我认为customer 正确获取了它的对象,但vehicles 获取了Builder??真的不知道那里出了什么问题。有任何想法吗?
只是为了描述我在我从事的项目中所做的事情,我有一个客户详细信息页面,我在其中单击一个按钮以将车辆添加到他的详细信息页面(个人资料页面),然后我将客户 id 作为函数的参数发送我将车辆添加到数据库中,该数据库有效(车辆与客户id 正确添加)。现在,当我想显示带有车辆信息的详细信息页面时,就会出现这个问题,如上面的代码所示。
希望它足够清楚。感谢您的建议。
【问题讨论】:
-
你的查询最后不需要
->get()吗?
标签: sql postgresql laravel query-builder