skip to Main Content

I want to display my data/product by category in frontend with URL/slug. I watch on YouTube, but I’m stuck when I click URL/link category got an error like this.

ErrorException
Trying to get property ‘slug’ of non-object (View: D:xampphtdocse-catalogresourcesviewsfrontendlayoutsheader.blade.php)

This is my header.blade.php:

<li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="/goldmart" role="button" data-bs-toggle="dropdown" aria-expanded="false">Goldmart</a>
    <ul class="dropdown-menu">
        @foreach ($gmcategories as $gmc )
            <li><a class="dropdown-item" href="{{url('goldmart/'.$gmc->slug)}}">{{$gmc->category_name}}</a></li>
        @endforeach
    </ul>
</li>

This is my web.php:

Route::get('/goldmart', 'GoldmartController@index');
Route::get('/goldmart/{slug}', 'GoldmartController@readCategory');

This is my Controller:

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppGmproducts;
use AppGmcategories;

class GoldmartController extends Controller
{
    public function index()
    {
        $gmproducts   = Gmproducts::orderBy('id', 'desc')->get();
        $gmcategories   = Gmcategories::all();

        return view('frontend.pages.goldmart', compact('gmproducts', 'gmcategories'));
    }

    public function readCategory($slug)
    {
        $category = Gmcategories::where('slug', $slug)->first();
        $gmcategories = Gmcategories::all();
        if($gmcategories)
        {
            $gmproducts = Gmproducts::where('category_id', $gmcategories->id)->get();
            return view('frontend.pages.goldmart', compact('gmproducts', 'category', 'gmcategories'));
        }
        else
        {
            return redirect('/');
        }
        return view('frontend.pages.goldmart');
    }
}

My AppServiceProvider:

namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateSupportFacadesSchema;
use IlluminateSupportFacadesBlade;
use IlluminateSupportFacadesView;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);

        // Membuat penulisan currency
        Blade::directive('currency', function ($expression)
        {
            return "Rp<?php echo number_format($expression,2,',','.'); ?>";
        });

        View::composer('frontend.layouts.header', function ($view) {
            $view->with('gmcategories', Gmcategories::all());
        });
    }
}

Error:

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Change controller from this:

    $gmproducts = Gmproducts::where('category_id', $gmcategories->id)->get();
    

    To this:

    $gmproducts = Gmproducts::where('category_id', $category->id)->get();
    

  2. <li>
    <a class="dropdown-item" href="{{ url('gm/'.($gmc->slug ?? null)) }}">
    {{$gmc->category_name}}
    </a>
    </li>
    
    
    **You can also try this one**
    <li>
    @if($gmc->slug)
    <a class="dropdown-item" href="{{ url('gm/'.($gmc->slug ?? '#')) }}">
    {{$gmc->category_name}}
    </a>
    @else
    slug not found
    @endif
    </li>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search