Home > OS >  Display data by category with url slug
Display data by category with url slug

Time:09-29

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:\xampp\htdocs\e-catalog\resources\views\frontend\layouts\header.blade.php)

This is my header.blade.php:

<li >
    <a  href="/goldmart" role="button" data-bs-toggle="dropdown" aria-expanded="false">Goldmart</a>
    <ul >
        @foreach ($gmcategories as $gmc )
            <li><a  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 App\Http\Controllers;

use Illuminate\Http\Request;
use App\Gmproducts;
use App\Gmcategories;

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 App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\View;

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

CodePudding user response:

<li>
<a  href="{{ url('gm/'.($gmc->slug ?? null)) }}">
{{$gmc->category_name}}
</a>
</li>


**You can also try this one**
<li>
@if($gmc->slug)
<a  href="{{ url('gm/'.($gmc->slug ?? '#')) }}">
{{$gmc->category_name}}
</a>
@else
slug not found
@endif
</li>

CodePudding user response:

The problem here is that you need to pass all the categories for the header and the wanted category to view it like this

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

you could also fix it in a more efficient way by using View Composer from your AppServiceProvider add these lines this will pass the categories for the header every time it used

use App\Gmcategories;


public function boot()
{
   View::composer('header', function ($view) {
     $view->with('gmcategories', Gmcategories::all());
   });
}
  • Related