Посмотри мои коды, я хочу сохранить информацию, потом все разваливается.
У меня три таблицы, houses
& categories
& category_house
.
домов & category_house таблицы
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']);
});
}
таблица категорий
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');
}
Категория. php
public function getParent ()
{
return $this->hasOne(Category::class, 'id', 'parent_id')->withDefault(['name' => '-']);
}
Например, у меня в базе данных есть следующие категории:
id
---- name
------ ----------- lang
------ href
--------- parent_id
-------- icon
---- ------------- created_at
-------- updated_at
8
----- Main Menu
-------- en
- --------- NULL
------ 0
--------------------- fas fa-cube
----- - NULL
---------------- NULL
Правильный метод должен быть сохранен здесь
id
---- name
----------------- lang
------ href
------------------ ---- parent_id
-------- icon
----------------- created_at
-------- updated_at
8
----- Main Menu
-------- en
---------- NULL
---------------- --- 0
--------------------- fas fa-cube
------ NULL
----------- ----- NULL
* 1 061 * --- Duplex House
---- en
---------- duplex-house
------ 8
------------- -------- NULL
--------------- NULL
---------------- NULL
Но так сохраняется.
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
И когда я изменил этот код
$category = Category::findOrFail($request->get('category'))->getParent();
Я получаю эту ошибку
обновить