doubi5127 2017-04-26 22:08
浏览 76
已采纳

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

In the admin area, I am trying to display the products in a listing page. Each product has category, color, size and type of the material My tables structure

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 

My product table

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

Category Model

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);
    }
}

color, size and material Models have the same structure

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);
    }
}

In the product controller, I have the following index function to display the products

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

I would like the output to be

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

My View is

  @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

The following error appears

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)

Thank you so much for the help,

The full Error is

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

This what I get when I var_dump($products) in the product controller

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条回答 默认 最新

  • dououde4065 2017-05-02 18:11
    关注

    Can you try by replacing these lines:

    <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>
    

    with thse lines:

    <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>
    

    I think you got my point. Before accessing any property of an object, just check if that object is exist or not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方