skip to Main Content
Route::get('catalog/{catname}', [MainController::class, 'getCatalog'])->name('catalog.page');
Route::get('catalog/{catname}/{alias}', [MainController::class, 'getItemsForList'])->name('catalog.view.item');

When I click on the link /catalog/table/table-black-and-white, I get an error

Attempt to read property "alias" on null

During development, I connected the database via belongsToMany


AppModelsCatalog

public function items() : BelongsToMany
{
return $this->belongsToMany(Item::class);
}

AppModelsItem
public function catalogs() : BelongsToMany
{
return $this->belongstoMany(Catalog::class);
}

The controller has two entries for outputting information

public function getCatalog($alias)
    {
        $cats = Catalog::where('alias', $alias)->first();
        $cats->items;
        return view('template.sait.page.catalog', compact('cats'))->with('alias', $alias);
    }

    public function getItemsForList($alias)
    {
        $data = Item::where('alias', $alias)->with('catalogs')->first();
        return view('template.sait.page.catalog.view', compact('data'))->with('alias', $alias);
    }

And in the view I display it like this:

@foreach($cats->items as $item)
.....
.....
<a href="{{route(''catalog.view.item'', $item->alias)}}">env</a>

As far as I understand, there is an error in my controller, and apparently I also do not fully indicate the route in the view, since sometimes I still get the error of missing “alias” field

2

Answers


  1. In both getCatalog and getItemsForList methods, you are using first() which will return null if no matching record is found. You need to check if $cats or $data is null before trying to access their properties or relationships.

    On the hand there are 2 route parameters in the getItemsForList and you are passing just one.

    public function getCatalog($catname)
    {
        $cats = Catalog::where('alias', $catname)->first();
    
        if (!$cats) {
            abort(404, 'Catalog not found');
        }
        // other code
    }
    
    public function getItemsForList($catname, $alias)
    {
        $data = Item::where('alias', $alias)->with('catalogs')->first();
    
        if (!$data) {
            abort(404, 'Item not found');
        }
    
        // other code
    }
    

    Also correct you a tag link

    <a href="{{ route('catalog.view.item', ['catname' => $cats->alias, 'alias' => $item->alias]) }}">env</a>
    
    Login or Signup to reply.
  2. The query didn’t find a matching Catalog with the provided alias in the getCatalog method, this indicates that the $cats variable is null when trying to access the alias property.

    Check if $cats is not null before accessing its properties.

    public function getCatalog($alias)
    {
        $cats = Catalog::where('alias', $alias)->first();
    
        if (!$cats) {
            abort(404);
        }
    
        $cats->load('items'); // I eager loaded the 'items' relationship here
    
        return view('template.sait.page.catalog', compact('cats'))->with('alias', $alias);
    }
    

    Update the getItemsForList method

    public function getItemsForList($alias)
    {
        $item = Item::where('alias', $alias)->with('catalogs')->first();
    
        if (!$item) {
            abort(404);
        }
    
        return view('template.sait.page.catalog.view', compact('item'))->with('alias', $alias);
    }
    

    Modify variable names accordingly in the view

    @foreach($item->catalogs as $catalog)
        <a href="{{ route('catalog.view.item', ['catname' => $catalog->alias, 'alias' => $item->alias]) }}">env</a>
    @endforeach
    

    NB: The important things to note are you check if the $cats variable is not null, and if it is, you can choose to either abort with a 404 error or handle it differently.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search