gpt4 book ai didi

sql - 查询返回 Object(Builder),未定义的属性

转载 作者:行者123 更新时间:2023-11-29 12:17:46 24 4
gpt4 key购买 nike

我有以下代码

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 View 中,我尝试使用以下代码来显示数据,但出现此错误:

未定义的属性: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 正确添加)。现在,当我想显示带有车辆信息的详细信息页面(如上面的代码所示)时,问题就出现了。希望它足够清楚。感谢您的建议。

最佳答案

尝试在 Controller 中添加 ->get()->paginate($YOUR_LIMIT_ONE_PAGE)

public function detailCustomer(Customer $customer)
{
$vehicles = DB::table('vehicles')
->selectRaw('*')
->where('cust_id', '=', $customer->id)->get();
// ->where('cust_id', '=', $customer->id)->paginate($YOUR_LIMIT_ONE_PAGE);
return view('customers.detail', array('customer' => $customer, 'vehicles' => $vehicles));
}

并尝试将您的 foreach 替换为此 forelse

@forelse ($vehicles as $index => $vehicle)
<tr>
<td>{{$index}}</td>
<td>{{($vehicle->spz !== null) ? $vehicle->spz : '-'}}</td>
<td>{{($vehicle->type !== null) ? $vehicle->type : '-'}}</td>
<td>{{($vehicle->brand !== null) ? $vehicle->brand : '-'}}</td>
</tr>
@empty
<td colspan='4'>Data not found</td>
@endforelse

关于sql - 查询返回 Object(Builder),未定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43570209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com