Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5.6上的数据格式_Laravel - Fatal编程技术网

Laravel 5.6上的数据格式

Laravel 5.6上的数据格式,laravel,Laravel,我开始学习Laravel框架,我有点怀疑 我有两种模式:客户和机会。 我想在opportunities/show.blade.php上显示客户名称,但我从控制器上的customer表检索到的数据格式有问题 我的OpportunityController上有此显示功能: public function show($id) { $opportunity = Opportunity::find($id); $customerName = Customer::select('name'

我开始学习Laravel框架,我有点怀疑

我有两种模式:客户和机会。 我想在opportunities/show.blade.php上显示客户名称,但我从控制器上的customer表检索到的数据格式有问题

我的OpportunityController上有此显示功能:

public function show($id)
{
    $opportunity = Opportunity::find($id);

    $customerName = Customer::select('name')->where('id','=',$opportunity->customer_id)->first();

    return view('opportunities.show',compact('opportunity','customerName'));
}
我在show.blade.php上调用这个变量,如下所示:

{{ $customerName }}
但它在这种格式上显示客户名称:

Customer: {"name":"John"}
如何显示客户名称,如下所示:

Customer: John

谢谢大家!

模型返回的数据是一个
Std类对象
,因此您可以像这样使用它:

{{ $customerName->name }}  // i.e. variable->column_name
它将打印
John


如果您使用的是
get()
而不是
first()
,那么您必须使用
foreach()
循环来迭代
Std Class对象的
返回数组。因此,必须指定要显示的属性。在您的情况下,您只选择了名称。因此,请这样做:

{{ $customerName->name }}

以下查询从数据库中选择一行,但仍作为对象返回:

Customer::select('name')->其中('id','=',$opportunity->Customer\u id)->first()

因此,查询的结果将是一个对象,该对象转换为
{“name”:“John”}

如果需要名称值,可以在视图中使用
{{{$customerName->name}}
,或者将查询更改为以下内容以仅获取
名称
列:


Customer::select('name')->其中('id','=',$opportunity->Customer\u id)->first()->name

$customerName
在这种情况下被视为对象,因此请尝试
{{{$customerName->name}
获取
name
的值

希望它能帮助你