我无法从刀片模板 Laravel 4 中的对象中获取价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26278199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:15:37  来源:igfitidea点击:

I cannot get value from an object in blade template Laravel 4

phplaravellaravel-4eloquentblade

提问by user2957058

I have a loop foreach in a blade template where i print data from a specific model, the problem is I am not able of getting the value of "$pedido->proveedor()->first()->name" in the code giveing me this error "ErrorException (E_UNKNOWN) Trying to get property of non-object (View: C:..":

我在刀片模板中有一个循环 foreach,我在其中打印来自特定模型的数据,问题是我无法在代码中获取“$pedido->proveedor()->first()->name”的值给我这个错误“ErrorException (E_UNKNOWN) Trying to get property of non-object (View: C:...”:

@foreach($pedidos as $pedido)  
    <tr>
        <td>
            {{ $pedido->id }}
        </td>
        <td>
           {{ $pedido->proveedor()->first()->name }}
        </td>
        <td>
          {{ date('d/m/Y', $pedido->fecha) }}
        </td>

          <td>
            <a onclick="return confirm('deseas borar este registro?')" class="btn btn-danger btn-xs fullButton">Borrar</a>
         </td>
     </tr>
@endforeach

The weird thing here is when I code this "$pedido->proveedor()->first()" in side the loop of the template I get an object like this:

这里奇怪的事情是当我在模板的循环旁边编码这个“$pedido->proveedor()->first()”时,我得到一个像这样的对象:

      {"name":"nombre","domicilio":"domicilio","cp":"46006","poblacion":"poblacion","ciudad":"ciudad","pais":"pais"} 

but coding this "$pedido->proveedor()->first()->name" I get the error:

但是编码这个“$pedido->proveedor()->first()->name”我得到错误:

the data is sent from a controller:

数据从控制器发送:

public function listPedidos()
{   

    $pedidos = Pedido::all();
//  this next pice of code shows me i can get the name as spected but only from php
//  foreach($pedidos as $pedido){
//     ddd($pedido->proveedor()->first()->name);exit;
//  }
    return View::make('pedidos/pedidos-list')->with('pedidos', $pedidos);

}

Another weirder thing is that I have the same code with different model and it is working.

另一件奇怪的事情是我有不同模型的相同代码并且它正在工作。

Thanks in advance for any help. ;)

在此先感谢您的帮助。;)

回答by Marcin Nabia?ek

You should use:

你应该使用:

{{ $pedido->proveedor->name }}

回答by user2957058

Problem solved:

问题解决了:

the previos answer was ok. I can use:

之前的回答没问题。我可以用:

{{ $pedido->proveedor()->first()->name }}

or

或者

{{ $pedido->proveedor->name }}

but because in one of the way of the loop there was not content to refer to i.e($pedido->proveedor didn't exixt) I use this:

但是因为在循环的一种方式中没有内容引用 ie($pedido->proveedor did not exixt) 我使用这个:

{{ isset($pedido->Proveedor->name )?$pedido->Proveedor->name :''; }}