在laravel 5.4中发现错误,试图获取非对象的属性

时间:2017-04-26 22:08:31

标签: php laravel-5.4

在管理区域,我正在尝试在列表页面中显示产品。每种产品都有材料的种类,颜色,尺寸和类型 我的表结构

categories
id   | category_name  
1    | Cat1  
2    | Cat2 

colors
id   |  color_name  
1    |  Color1   
2    |  Color2

materials
id   | material_type  
1    | material1  
2    | material2 

 sizes
id   | size_name  
1    | size1  
2    | size2 

我的产品表

products
id  name  description category_id  color_id   material_id   size_id
1   pro1  product 1        1          1           1            1
2   pro2  product 2        2          2           2            2

类别模型

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Product;


class Category extends Model
{
    protected $table = 'categories';

    public function products(){
      return $this->hasOne(Product::class);
    }
}

颜色,尺寸和材质模型具有相同的结构

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Product;

class Color extends Model
{
    protected $table = 'colors';
    public function products(){
      return $this->hasMany(Product::class);
    }
}

在产品控制器中,我有以下索引功能来显示产品

public function index()
    {
      $products = Product::with('categories', 'colors' , 'sizes', 
      'materials', 'fantasias')->get();
      return view('backend.product.product-library', compact('products'));
    }

我希望输出为

products
name  description  category   color    material   size 
pro1  product 1      cat1    color1    mateial1   size1
pro2  product 2      cat2    color2    material2  size2

我的观点是

  @foreach( $products as $item )
      <tbody>
        <tr>
          <td><h3>{{ $item->product_name }}</h3></td>
          <td><p>{{ $item->product_description }}</p></td>
          <td><p>{{ $item->categories->category_name }}</p></td>
          <td><p>{{ $item->colors->color_name }}</p></td>
          <td><p>{{ $item->materials->material_type }}</p></td>
          <td><p>{{ $item->sizes->size_name }}</p></td>
          <td rowspan="2">
            <a class="btn transparent" href="{{ route('products.edit', ['product'=>$item->id]) }}">Edit</a>
            <form action="{{ route('products.destroy', ['product'=>$item->id]) }}" method="POST" enctype="multipart/form-data">
              <input name="_method" type="hidden" value="DELETE">
              <button type="submit" class="btn transparent">Delete</button>
              <input type="hidden" name="_token" value="{{Session::token()}}">
            </form>
          </td>
        </tr>
      </tbody>
    @endforeach

出现以下错误

ErrorException in b5a959a09cc5b7e6abf71778d058119094681a2d.php line 28:
Trying to get property of non-object (View: /var/www/html/pet/resources/views/backend/product/product-library.blade.php)

非常感谢您的帮助,

完整错误是

ErrorException in b5a959a09cc5b7e6abf71778d058119094681a2d.php line 28:
Trying to get property of non-object (View: /var/www/html/pet/resources/views/backend/product/product-library.blade.php)
in b5a959a09cc5b7e6abf71778d058119094681a2d.php line 28
at CompilerEngine->handleViewException(object(ErrorException), 1) in PhpEngine.php line 44
at PhpEngine->evaluatePath('/var/www/html/pet/storage/framework/views/b5a959a09cc5b7e6abf71778d058119094681a2d.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'products' => object(Collection))) in CompilerEngine.php line 59
at CompilerEngine->get('/var/www/html/pet/resources/views/backend/product/product-library.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'products' => object(Collection))) in View.php line 137
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 38
at Response->setContent(object(View)) in Response.php line 225
at Response->__construct(object(View)) in Router.php line 602
at Router->prepareResponse(object(Request), object(View)) in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 51
at Debugbar->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('/var/www/html/pet/public/index.php') in server.php line 21
1/2
ErrorException in b5a959a09cc5b7e6abf71778d058119094681a2d.php line 28:
Trying to get property of non-object
in b5a959a09cc5b7e6abf71778d058119094681a2d.php line 28
at HandleExceptions->handleError(8, 'Trying to get property of non-object', '/var/www/html/pet/storage/framework/views/b5a959a09cc5b7e6abf71778d058119094681a2d.php', 28, array('__path' => '/var/www/html/pet/storage/framework/views/b5a959a09cc5b7e6abf71778d058119094681a2d.php', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'products' => object(Collection)), 'obLevel' => 1, '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'products' => object(Collection), '__currentLoopData' => object(Collection), 'item' => object(Product), 'loop' => object(stdClass))) in b5a959a09cc5b7e6abf71778d058119094681a2d.php line 28
at include('/var/www/html/pet/storage/framework/views/b5a959a09cc5b7e6abf71778d058119094681a2d.php') in PhpEngine.php line 42
at PhpEngine->evaluatePath('/var/www/html/pet/storage/framework/views/b5a959a09cc5b7e6abf71778d058119094681a2d.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'products' => object(Collection))) in CompilerEngine.php line 59
at CompilerEngine->get('/var/www/html/pet/resources/views/backend/product/product-library.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'products' => object(Collection))) in View.php line 137
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 38
at Response->setContent(object(View)) in Response.php line 225
at Response->__construct(object(View)) in Router.php line 602
at Router->prepareResponse(object(Request), object(View)) in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 51
at Debugbar->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('/var/www/html/pet/public/index.php') in server.php line 21

这是我在产品控制器中的var_dump($ products)时得到的结果

Collection {#305 ▼
  #items: array:7 [▼
    0 => Product {#313 ▶}
    1 => Product {#314 ▶}
    2 => Product {#315 ▶}
    3 => Product {#316 ▶}
    4 => Product {#317 ▶}
    5 => Product {#318 ▶}
    6 => Product {#319 ▼
      #guarded: array:1 [▶]
      #table: "products"
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:13 [▶]
      #original: array:13 [▶]
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: array:5 [▼
        "categories" => Category {#303 ▶}
        "colors" => Color {#304 ▶}
        "sizes" => Size {#321 ▶}
        "materials" => Material {#307 ▶}
        "fantasias" => Fantasia {#124 ▶}
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
    }
  ]
}

2 个答案:

答案 0 :(得分:1)

您可以尝试替换这些行:

<td><p>{{ $item->categories->category_name }}</p></td>
<td><p>{{ $item->colors->color_name }}</p></td>
<td><p>{{ $item->materials->material_type }}</p></td>
<td><p>{{ $item->sizes->size_name }}</p></td>

与这些行:

<td><p>{{ isset($item->categories) ? $item->categories->category_name: '-'}}</p></td>
<td><p>{{ isset($item->colors) ? $item->colors->color_name : '-'}}</p></td>
<td><p>{{ isset($item->materials) ? $item->materials->material_type : '-'}}</p></td>
<td><p>{{ isset($item->sizes) ? $item->sizes->size_name : '-'}}</p></td>

我认为你明白我的观点。在访问对象的任何属性之前,只需检查该对象是否存在。

答案 1 :(得分:0)

我认为您的问题出在视图中

<img src="/images/product-feature/{!! $item->product_preview !!}

尝试

var USER_DAYS = JSON.parse('${jsonArrayString}')