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