Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

me10071990's avatar

What is best way to make every category page with new designed

Is it possible to make new design for every category as I have created category with id ex home id 1 product id 2 contact us id 3 about us id 4

thank you all

0 likes
32 replies
Snapey's avatar

add a template column to your categories

In this, store the name of the view file you want to use. When you return view, insert the template name from the database rather than hard coding the string for the view name.

me10071990's avatar

thank you @snapey , ii just made 4 category in database with the help with id.. and for home my id is 1, product id is 2 about id is 3 contact us id is 4, as i created category.blade.php where it has creating same category page designed but i want to create a new design for every category...can show my all code here

me10071990's avatar

this is category.blade.php

@extends('layouts.frontend')

@section('content')

<div style="background-image:url(app/images/head/about.jpg)" class="wrap-slider my-wrap-slider">
    <div class="container page-container">
        <div class="page-content">
            <div class="page-title">
                <h1 class="tshadow">{{$category->name}}</h1>

            </div><!-- page-title -->
        </div>
    </div><!-- container -->
</div><!-- wrap-slider -->

<div class="container">
    <div class="row medium-padding120">
        <main class="main">

            <div class="row">
                <div class="case-item-wrap">
                    @foreach($category->posts as $post)

                        <div class="col-lg-4  col-md-4 col-sm-6 col-xs-12">
                            <div class="case-item">
                                <div class="case-item__thumb mouseover poster-3d lightbox shadow animation-disabled" data-offset="5">
                                    <img src="{{ $post->featured }}" alt="our case">
                                </div>
                                <a href="{{ route('post.single', ['slug' => $post->slug ]) }}"><h6 class="case-item__title">{{ $post->title }}</h6></a>
                            </div>
                        </div>

                    @endforeach
                </div>
            </div>

        </main>
    </div>
</div>

@endsection

and this is frontend Controller

Snapey's avatar

Where is frontend controller

Copy category.blade.php to category1.blade.php, category2.blade.php, category3.blade.php. category4.blade.php

in the controller

return view('category'.$category->id);

edit each file to suit the category

me10071990's avatar

Thank you once again, I made category1.blade.php, category2.blade.php, category3.blade.php, category4.blade.php, category5.blade.php, and here is example of category1.blade.php

@extends('layouts.frontend')

@section('content')

<div style="background-image:url(app/images/head/about.jpg)" class="wrap-slider my-wrap-slider">
    <div class="container page-container">
        <div class="page-content">
            <div class="page-title">
                <h1 class="tshadow">{{$category->name}}</h1>
                
                Hi type here only for the same category

            </div><!-- page-title -->
        </div>
    </div><!-- container -->
</div><!-- wrap-slider -->

<div class="container">
    <div class="row medium-padding120">
        <main class="main">

            <div class="row">
                <div class="case-item-wrap">
                    @foreach($category->posts as $post)

                        <div class="col-lg-4  col-md-4 col-sm-6 col-xs-12">
                            <div class="case-item">
                                <div class="case-item__thumb mouseover poster-3d lightbox shadow animation-disabled" data-offset="5">
                                    <img src="{{ $post->featured }}" alt="our case">
                                </div>
                                <a href="{{ route('post.single', ['slug' => $post->slug ]) }}"><h6 class="case-item__title">{{ $post->title }}</h6></a>
                            </div>
                        </div>

                    @endforeach
                </div>
            </div>

        </main>
    </div>
</div>

@endsection

and frontend controller is like that

Snapey's avatar

and frontend controller is like that

What does this mean?

Is your problem solved? Please mark it complete.

me10071990's avatar

Still I am doing the same but its nothing happend, because I have different category id and when trying to connect its showing nothing only one desinged shows

Snapey's avatar

Please try and explain the problem

me10071990's avatar

ok @snapey , first I have made 4 category in my admin Home, Contact, About, Porduct and these are showing on menu list in website.

However, I have made a a single template category.blade,php as a designed for every category will have same designed.

And I want to create a different layout designed for each menu individual..

I am pasting all code below...

This is header.blade.php

image
                <div class="btn-menu">
                    <span></span>
                </div><!-- //mobile menu button -->
            </div><!-- /wrap-search -->

            <div class="wrap-nav">
                <nav id="mainnav" class="mainnav">
                    <ul class="menu">

                  @foreach($categories as $category)
                            <li class="text-black-50"><a href="/category/{{$category->id}}">{{$category->name}}</a></li>
                  @endforeach

                    </ul>
                </nav>

            </div><!-- /wrap-nav -->
        </div><!-- /col-md-12 -->
    </div><!-- /row -->
</div><!-- /container -->

this is frontend controller

Maged's avatar

as @snapey said you can add ( template) colum in categories table and in the show method in CategoriesController

$template = 'categories/'. $category->template . '.blade.php';
return view($template, compact('category'));

Or if you don't like adding an extra column to your database table you can just tweak it using categories ids , for instance

if($category->id == 1 )
 $template = "something.blade.php";
elseif

and so on

me10071990's avatar

Thanks @maged , I am doing this as you told still it's nothing happend

public function up() { Schema::create('categories', function (Blueprint $table) { $table->increments('id');

        $table->string('name');
        $table->string('template');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('categories');
}

}

and

my categoriesConroller

Maged's avatar

@ME10071990 - please show me a screen of your Categoriescontroller + names of your view files + categories records ( i want to see what u store in template field for each category)

me10071990's avatar

I have send already pasted all the code please check

Maged's avatar

Post your CategoriesController - show method

me10071990's avatar

thank you ...public function show($id) {

   // $category= new Category;
    //$template = 'categories/'. $category->template . '.blade.php';
    //return view($template, compact('category'));


}
Snapey's avatar

When you show a view, you don't put .blade.php in the view command.... so why have you done it here?

How do you expect to show a category template when you don't choose the category and just load an empty model -$category= new Category;

Is this code really all commented out?

1 like
me10071990's avatar

@Snapey is this not commented out, I was just checking another code for it and also using if elseif condition, please help me to correct so I can do and learn in best way... thank you very much

Snapey's avatar
Snapey
Best Answer
Level 122

@maged still with the blade naming error

public function show(Category $category) {
        $template = 'categories.' . $category->template;
        return view($template, compact('category'));
}

or better

public function show(Category $category) {

        return view( 'categories.' . $category->template, compact('category'));

}
1 like
Maged's avatar

@snapey sorry i just realized that .. you are absolutely right :)

me10071990's avatar

Thank you @snapey -:) I am using this in CategoriesController public function show(Category $category) {

    return view( 'categories.' . $category->template, compact('category'));

} 

so what I have to do next

me10071990's avatar

now in data base it's showing error.."SQLSTATE[HY000]: General error: 1364 Field 'template' doesn't have a default value (SQL: insert into categories (name, updated_at, created_at) values (HOME, 2019-04-25 12:42:37, 2019-04-25 12:42:37)) ◀" however I reset NULL

Maged's avatar

@me10071990 you didn't insert any value in the template field ,this field should include the blade name like category1 ..

make sure you insert a value there using tinker or phpmyadmin for now .

me10071990's avatar

I put from phpmyadmin as a null and its working now however designed is still not going through...

public function show(Category $category) {

return view( 'categories.' . $category->template, compact('category'));

} after this what code I have to make or change.. thank you

Maged's avatar

you also might want to refer for a default template if you didn't insert one .. so in your migration add this

$table->string('template')->default('category1');

i think this would be fine .

me10071990's avatar

Ok thank you , what code I need to change after that

me10071990's avatar

Ok , so I have to make every new blade.php for each category ?

Snapey's avatar

I think you first need to learn php and laravel a little first. This is taking a ridiculous number of messages for a simple request.

You create the category

You create the template for the category

You put the name of the template in the category

You display the category using the template.

How hard can it be?

Next

Please or to participate in this conversation.