dijkr / Copia

Laravel-project with CMS
0 stars 0 forks source link

Load category data when there isn't a product yet #18

Closed dijkr closed 1 year ago

dijkr commented 1 year ago

The /categorie/{category} will not load if there isn't a product within that specific category in the database yet. This is because the category-data is fetched by using the product relation with the category.

dijkr commented 1 year ago

The solution was to import the Category-model within the ProductController.

Then the category data can be directly loaded from the model, without the need of a relation with a product.

use App\Models\Product;
use App\Models\Category;
        // Find the category
        $categorySlug = basename($request->getRequestUri());
        // Get the category data
        $categoryData = Category::where('slug', $categorySlug)->first();