我正在嘗試將作者和流派存盤到一本書中。我試圖實作這一目標的方法是擁有一個 Author 表、Genre 表和一個 Book 表。我想將作者和流派的外鍵存盤到書籍表中。
我創建了一個表單來存盤這本書所需的資訊。在表單中,有兩個選擇標簽,在其中我回圈遍歷作者和流派陣列。我在 BookController 中使用 store 函式來驗證正在發送的資料。驗證后,我存盤了一本新書。
但是,除了主鍵、標題、簡介和時間戳之外,外鍵并未存盤在資料庫中。
書型
class Book extends Model
{
use HasFactory;
protected $fillable = [
'author_id',
'genre_id',
'title',
'blurb',
];
public function authors()
{
return $this->BelongsTo(Author::class);
}
public function genres()
{
return $this->BelongsTo(Genre::class);
}
}
作者模型
class Author extends Model
{
use HasFactory;
protected $fillable = [
'name',
];
public function book()
{
return $this->hasMany(Book::class);
}
}
流派模型
class Genre extends Model
{
use HasFactory;
protected $fillable = [
'title'
];
public function book()
{
return $this->hasMany(Book::class);
}
}
BookController 創建()
public function create()
{
$authors = Author::all();
$genres = Genre::all();
return view('admin.book.create', ['authors' => $authors, 'genres' => $genres]);
}
BookController store()
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|max:255',
'authors' => 'required|array',
'genres' => 'required|array',
'blurb' => 'required',
]);
Book::create($validatedData);
return redirect()->route('admin.book.create');
}
Books表遷移
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->foreignId('author_id');
$table->foreignId('genre_id');
$table->string('title');
$table->string('blurb');
$table->timestamps();
});
}
create.blade.php(創建書頁)
<div class="flex justify-center">
<div class="flex justify-center border lg:w-1/4">
<div class="flex flex-col justify-center lg:w-2/4 gap-2">
<form action="{{ route('admin.book.store') }}" method="post">
@csrf
<div class="mb-2">
<h1 class="text-xl">Add Book</h1>
</div>
<div>
<label class="" for="title">Title</label>
<input name="title" class="w-10 border border-gray-400 w-full py-1 px-2" type="text">
</div>
<div>
<label class="" for="author">Author</label>
<select name="authors[]">
@foreach($authors as $author)
<option value="{{ $author->id }}">{{ $author->name }}</option>
@endforeach
</select>
</div>
<div>
<label class="" for="genre">Genre</label>
<select name="genres[]">
@foreach($genres as $genre)
<option value="{{ $genre->id }}">{{ $genre->title }}</option>
@endforeach
</select>
</div>
<div>
<label class="" for="blurb">Blurb</label>
<textarea name="blurb" id="" cols="18" rows="10"></textarea>
</div>
<div>
<button class="py-2 px-4 border" type="submit">Save</button>
</div>
</form>
</div>
</div>
</div>
編輯:當我傾倒并死亡時:
array:5 [▼
"_token" => "WHlc2nvslN6l6iY4D8feeIzLEYzetMo3Ek0tLVPn"
"title" => "Harry Potter"
"authors" => array:1 [▼
0 => "1"
]
"genres" => array:1 [▼
0 => "4"
]
"blurb" => "Harry Potter is een zevendelige fantasyserie geschreven door de Britse schrijfster J.K. Rowling. De boeken volgen chronologisch de puberteit en de adolescentie ?"
]
uj5u.com熱心網友回復:
你必須這樣做:
- 首先,在你的刀片中,將authors[] 中的選擇名稱轉換為author_id,將genres[] 中的選擇名稱轉換為genre_id。
- 將驗證中的作者和流派更新為上面的新名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/372191.html
