use*_*846 1 php laravel eloquent laravel-5
你好,我是 Laravel 新人,在理解如何创建关系方面有点挣扎。我正在尝试在 laravel 中制作一个基本的 Restful api 并有 3 个模型
class Book extends Model {
public function author()
{
return $this->belongsTo(Author::class);
}
public function categories()
{
return $this->belongsToMany('App\Category', 'category_book')
->withTimestamps();
}
}
class Author extends Model
{
public function books(){
return $this->hasMany(Book::class);
}
}
class Category extends Model
{
public function books()
{
return $this->belongsToMany('App\Book', 'category_book')
->withTimestamps();
}
}
Run Code Online (Sandbox Code Playgroud)
表迁移:
Schema::create('books', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->string('ISBN', 32);
$table->string('title');
$table->integer('author_id')->unsigned();
$table->float('price')->default(0);
$table->timestamps();
});
Schema::create('authors', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->bigIncrements('id');
$table->string('name');
$table->string('surname');
$table->timestamps();
});
Schema::create('categories', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
Schema::create('category_book', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('category_id')->unsigned();
//$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->integer('book_id')->unsigned();
//$table->foreign('book_id')->references('id')->on('books')->onDelete('cascade');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
books 是主表,作者与 books 具有一对多关系。类别与书籍具有多对多的关系,因为一本书可以属于多个类别。
books 表有一个author_id 字段将其链接到作者表。还有一个名为category_books 的数据透视表,其中包含category_id 和book_id,用于将书籍链接到类别。
假设我想创建一个新的图书记录,如果作者存在,则将该书与该作者关联,但如果不存在,我想创建一个新的作者记录,然后将该书与该作者关联?
我也希望能够对类别做同样的事情
我的图书控制器中有以下内容:
public function store(Request $request)
{
$book = new book;
$book->title = $request->title;
$book->ISBN = $request->ISBN;
$book->price = $request->price;
$book->categories()->associate($request->category);
$book->save();
return response()->json($book, 201);
}
Run Code Online (Sandbox Code Playgroud)
首先,在行中
$book->categories()->associate($request->category);
Run Code Online (Sandbox Code Playgroud)
associate()当您想要更新关系时使用该方法belongsTo。$book->categories() 是多对多关系 ( belongsToMany),因此您应该使用attach()它。
其次,如果你想关联一个可能存在或不存在的作者,你可以使用该firstOrCreate方法。
$author = Author::firstOrCreate([
'name' => $request->author_name,
'surname' => $request->author_surname
]);
$book->author()->associate($author);
Run Code Online (Sandbox Code Playgroud)
您可以对类别或书籍执行相同的操作。
$category = Category::firstOrCreate([
'name' => $request->category_name
]);
$book->categories()->attach($category);
Run Code Online (Sandbox Code Playgroud)
$book = Book::firstOrCreate([
'ISBN' => $request->book_isbn,
'title' => $request->book_title,
'price' => $request->book_price
]);
$category->books()->attach($book);
Run Code Online (Sandbox Code Playgroud)
的使用firstOrCreate()记录在这里
https://laravel.com/docs/5.8/eloquent#other-creation-methods
此页面有更多关于 Eloquent 关系方法的信息
https://laravel.com/docs/5.8/eloquent-relationships