Laravel 错误:“试图获取非对象的属性”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42516994/
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
Laravel Error: "Trying to get property of non-object"
提问by Feralheart
I have a table what populates from database:
我有一个从数据库中填充的表:
@extends('layouts.master')
@section('main')
@parent
<table border=1 align=center;>
<tr><td rowspan=10><img src="media/productimg/question.jpg" width=250px></td>
<th>Márka</th><td colspan=3>{{ $productdetails->brand }}</td></tr>
<tr><th>Beszállító</th><td colspan=3>{{ $productdetails->supplier }}</td></tr>
<tr><th>{{ $productdetails->type }}</th>
<th colspan=3>{{ $productdetails->name }}</th></tr>
<tr><th>Nettó beszerzési ár</th><td>{{ $productdetails->wholeprice }} Ft</td>
<th rowspan=2>ár</th><td rowspan=2>{{ $productdetails->price }} Ft</td></tr>
<tr><th>Bruttó beszerzési ár</th><td>{{ $productdetails->wholeprice }} Ft</td>
<tr><th>Vonalkód</th><td>{{ $productdetails->barcode }}</td><th>Raktáron</th><td>{{ $productdetails->count }} {{ $productdetails->dimension }}</td></tr>
<tr><th>Elhelyezkedés</th><td>{{ $productdetails->whereis }} {{ $productdetails->whereis2 }}</td><th>Küsz?b</th><td>{{ $productdetails->threshold }} {{ $productdetails->dimension }}</td></tr>
<tr><th>Utolsó rendelés</th><td> NA </td>
<th>Utolsó vásárlás</th><td> NA </td></tr>
<tr><td colspan=5><a href="stock_productimageupload.php"><button class="button">Kép szerkesztése</button></a>
<a href="stock_productupdate.php"><button class="button">Termék szerkesztése</button></a>
<a href="stock_productcountupdate.php"><button class="button">Mennyiség szabályozása</button></a></td></tr>
</table>
@endsection
And I got this error:
我收到了这个错误:
ErrorException in 20d205ed160f36c734b8252e44ac81bfa0977988.php line 6: Trying to get property of non-object
ErrorException in 20d205ed160f36c734b8252e44ac81bfa0977988.php 第 6 行:试图获取非对象的属性
If I replace the table with
如果我用
<?php print_r($productdetails);?>
I got the the array with the good values.
我得到了具有良好价值的数组。
What going wrong?
怎么了?
回答by Robert
You are saying that the array will show as it should be.
您是说数组将按原样显示。
You could try for example:{{ $productdetails['type'] }}
instead of {{ $productdetails->type }}
您可以尝试例如:{{ $productdetails['type'] }}
而不是{{ $productdetails->type }}
But please post the result of the print_r($productdetails)
function so that we can see whats wrong with the code.
但是请发布print_r($productdetails)
函数的结果,以便我们可以看到代码有什么问题。
回答by Feralheart
The solution was: {{ $productdetails[0]->type }}
解决方案是: {{ $productdetails[0]->type }}
Thanks for @rahul_m for his answer:
感谢@rahul_m 的回答:
Once check the structure of array it is having 0th index first,
You should get it like,
{{$product[0]->brand}}
OR
$product = DB::table("inventory")->where("barcode", $id)->first(); //
and get data as echo $product->brand;
Give it a try, it should work.
一旦检查数组的结构,它首先具有第 0 个索引,
你应该明白,
{{$product[0]->brand}}
或者
$product = DB::table("inventory")->where("barcode", $id)->first(); //
and get data as echo $product->brand;
试一试,它应该工作。