Home > Net >  Attempt to read property "name" on null Laravel 9
Attempt to read property "name" on null Laravel 9

Time:01-14

i am creating a simple inventory system using laravel 9.I want to join the there tables in Laravel those are category and brand table and product.i have written the join queries.but i ran into the problem with Attempt to read property "name" on null what i tried so far i attached code snippet below.

what i checked the error Error has point out of this line {{ $item->category->name }}

Product Model

protected $table = 'products';

protected $primaryKey = 'id';
 
protected $fillable = ['barcode_no', 'productname', 'cat_id', 'brand_id', 'price'];


public function category()
{
    return $this->belongsTo(Category::class);
}

public function brand()
{
    return $this->belongsTo(Brand::class);
}

Category Model

protected $table = 'categories';


    protected $primaryKey = 'id';


    protected $fillable = ['name', 'status'];


    public function products()
    {
        return $this->hasMany(Product::class, 'cat_id', 'id');
    }

Brand Model

protected $table = 'brands';


    protected $primaryKey = 'id';

 
    protected $fillable = ['name', 'status'];

    public function products()
    {
        return $this->hasMany(Product::class, 'brand_id', 'id');
   }

Product view

@foreach($products as $item)
    <tr>
       <td>{{ $loop->iteration }}</td>
      <td>{{ $item->barcode_no }}</td>
       <td>{{ $item->productname }}</td>
       <td>{{ $item->category->name }}</td>
       <td>{{ $item->brand->name }}</td>
    </tr>

ProductController

 public function index(Request $request)
    {
        $keyword = $request->get('search');
        $perPage = 25;

        if (!empty($keyword)) {
            $products = Product::where('barcode_no', 'LIKE', "%$keyword%")
                ->orWhere('productname', 'LIKE', "%$keyword%")
                ->orWhere('cat_id', 'LIKE', "%$keyword%")
                ->orWhere('brand_id', 'LIKE', "%$keyword%")
                ->orWhere('price', 'LIKE', "%$keyword%")
                ->latest()->paginate($perPage);
        } else {
            $products = Product::latest()->paginate($perPage);
        }

        return view('admin.products.index', compact('products'));
    }

CodePudding user response:

The issue with naming convention cat_id in products table so need to change the relation like below by mentioning category id

public function category()
{
    return $this->belongsTo(Category::class,'cat_id');
}

It looks like one of the product might not have an assigned category. So check the null check in the blade template using optional() helper.

<td>{{ optional($item->category)->name }}</td>
<td>{{ optional($item->brand)->name??null }}</td>

and in your controller, you can optimize code little bit using Conditional Clauses

public function index(Request $request)
{
        $keyword = $request->get('search');
        $perPage = 25;

            $products = Product::with(['category','brand'])->when(!empty($keyword),function ($query)use($keyword){
                $query->where('barcode_no', 'LIKE', "%$keyword%")
                    ->orWhere('productname', 'LIKE', "%$keyword%")
                    ->orWhere('cat_id', 'LIKE', "%$keyword%")
                    ->orWhere('brand_id', 'LIKE', "%$keyword%")
                    ->orWhere('price', 'LIKE', "%$keyword%");
            })->latest()->paginate($perPage);


        return view('admin.products.index', compact('products'));
}
  • Related