How to add slug to href

0 votes

Look at my codes, I want to save the information, then everything falls apart.

I have three tables, houses & categories & category_house.

houses & category_house tables

    public function up()
    {
        Schema::create('houses', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('title');
            $table->string('slug');
            $table->string('lang');
            $table->string('image')->nullable();
            $table->text('sliders');
            $table->text('body');
            $table->timestamps();
        });
        Schema::create('category_house', function (Blueprint $table) {
            $table->bigInteger('category_id')->unsigned();
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->bigInteger('house_id')->unsigned();
            $table->foreign('house_id')->references('id')->on('houses')->onDelete('cascade');
            $table->primary(['category_id' , 'house_id']);
        });
    }

categories table

    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('lang');
            $table->string('href')->nullable();
            $table->integer('parent_id');
            $table->string('icon')->nullable();
            $table->timestamps();
        });
    }

edit.blade.php

    <form action="{{ route('houses.update', $house->id) }}" method="post">
        @csrf
        @method('PUT')
        <input type="hidden" name="sliders">
        <div class="col-md-6">
            <div class="form-group">
                <label for="title">Title</label>
                <input type="text" class="form-control" id="title" name="title" value="{{ old('title') ?? $house->title }}">
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <label for="body">Body</label>
                <textarea class="form-control" id="body" name="body">{{ old('body') ?? $house->body }}</textarea>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="category">Sub Category</label>
                <select name="category" id="category" class="form-control">
                    @foreach($categories as $category)
                        <option value="{{ $category->id }}" {{ $house->categories()->pluck('id')->contains($category->id) ? 'selected' : '' }}>
                            {{ $category->name }}
                        </option>
                    @endforeach
                </select>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="lang">Language</label>
                <select id="lang" name="lang" class="form-control">
                    <option value="fa" {{ $house->lang == 'fa' ? 'selected' : '' }}>Farsi</option>
                    <option value="en" {{ $house->lang == 'en' ? 'selected' : '' }}>English</option>
                </select>
            </div>
        </div>
        //.....
        <div class="form-group">
            <button class="btn btn-primary btn-lg btn-block">
                <i class="fas fa-check ml-2"></i>Save
            </button>
        </div>
    </form>

HouseController.php

     public function update(Request $request, House $house)
    {
        $house->user_id = 1;
        $house->title = $request->title;
        $house->lang = $request->lang;
        $house->body = $request->body;
        if($request->has('image')) {
            $image = $request->file('image');
            $filename = $image->getClientOriginalName();
            $image->move(public_path('images/slideShows'), $filename);
            $house->image = $request->file('image')->getClientOriginalName();
}
        $house->save();
        $house->categories()->sync($request->category);
        $category = Category::findOrFail($request->get('category'))->getParent();
        $category->name = $request->title;
        $category->parent_id = $request->category;
        $category->lang = $request->lang;
        $category->href = $request->slug;
        $category->save();
        return redirect()->route('houses.index');
    }

Category.php

    public function getParent ()
    {
        return $this->hasOne(Category::class, 'id', 'parent_id')->withDefault(['name' => '-']);
    }

For example, I have the following cateories in database.

id`----`name`-----------------`lang`------`href`---------`parent_id`--------`icon`-----------------`created_at`--------`updated_at
8`-----`Main Menu`--------`en`----------`NULL`------`0`---------------------`fas fa-cube`------`NULL`----------------`NULL

The correct method should be stored this way

id`----`name`-----------------`lang`------`href`----------------------`parent_id`--------`icon`-----------------`created_at`--------`updated_at
8`-----`Main Menu`--------`en`----------`NULL`-------------------`0`---------------------`fas fa-cube`------`NULL`----------------`NULL
20`---`Duplex House`----`en`----------`duplex-house`------`8`---------------------`NULL`---------------`NULL`----------------`NULL

But this way it is saved.

id`----`name`-----------------`lang`------`href`----------------------`parent_id`--------`icon`-----------------`created_at`--------`updated_at
8`-----`Duplex House`----`en`---------`NULL`-------------------`8`---------------------`fas fa-cube`------`NULL`----------------`NULL
20`---`Duplex House`----`en`---------`NULL`-------------------`8`---------------------`NULL`---------------`NULL`----------------`NULL

And when I changed this code 

    $category = Category::findOrFail($request->get('category'))->getParent();

I want to add slug to href.

- slug => house
- href => categories

Aug 10, 2020 in PHP by aakash
• 230 points

recategorized Aug 10, 2020 by Niroj 1,131 views

Hello @aakash,

What error you are getting?

I'm getting this error.....How to fix that? Do you have any idea?

You can refer this for better understanding of my code:https://laracasts.com/discuss/channels/laravel/how-to-add-slug-to-href

1 answer to this question.

0 votes

Hello @aakash,

You are not sending the variable to view
Should be:

return view('vendorsDb.sections.sections', [
            'sections' => $sections,
        ]);

Make this change it will work

Hope this works!!

Thank You!!

answered Aug 11, 2020 by Niroj
• 82,840 points
Ahhh...man ...many thanks...finally it worked. I just have to embed your 'vendorsDb.sections.sections' and it worked perfectly.

Related Questions In PHP

0 votes
1 answer

How do I add PHP code/file to HTML(.html) files?

Hello @kartik, You can't run PHP in .html ...READ MORE

answered Oct 22, 2020 in PHP by Niroj
• 82,840 points
665 views
0 votes
1 answer

How to add a line break within echo in PHP?

Hello @kartik, You have to use br when ...READ MORE

answered Oct 27, 2020 in PHP by Niroj
• 82,840 points
3,134 views
0 votes
0 answers

How to add php repositories to debian kali linux for php files?

I added: packages.sury.org/php through the script, I got on ...READ MORE

Jun 1, 2022 in PHP by Kichu
• 19,040 points
758 views
0 votes
0 answers

How to add elements to an empty array in PHP?

I define an array in PHP : $cart ...READ MORE

Jun 3, 2022 in PHP by Kichu
• 19,040 points
934 views
0 votes
0 answers

PDOException SQLSTATE[HY000] [2002] No such file or directory

My (very basic) site appears to have ...READ MORE

Sep 3, 2022 in Database by Kithuzzz
• 38,000 points
2,050 views
0 votes
1 answer

Why is not preferable to use mysql_* functions in PHP?

The reasons are as follows: The MySQL extension: Does ...READ MORE

answered Sep 7, 2018 in Database by DataKing99
• 8,250 points
1,268 views
0 votes
1 answer

How to exclude specific folders every time I run 'eb deploy' using AWS EB CLI

If there is not .ebignore file it ...READ MORE

answered Oct 24, 2018 in AWS by Priyaj
• 58,020 points
2,089 views
0 votes
1 answer

How can I add PHP page to WordPress?

Hii @kartik, First, duplicate post.php or page.php in your theme folder. Rename the ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,840 points
1,423 views
0 votes
1 answer

How to add custom javascript to WordPress Admin?

Hello, Use the admin_enqueue_scripts action and the wp_enqueue_script method to add custom ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,840 points
6,560 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP