Laravel基于名称而非ID的查询关系

时间:2019-04-30 13:18:27

标签: php laravel laravel-5 eloquent

嗨,我是新来的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();
    }
}

表迁移:

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();
    });   

books是主要表格,作者与书籍具有一对多的关系。类别与书籍具有多对多的关系,因为一本书可以属于多个类别。

books表具有一个author_id字段,可将其链接到authors表。还有一个名为category_books的数据透视表,其中包含category_id和book_id以将图书链接到类别

但是如何查询书籍,使其仅返回基于作者姓名的书籍?

我也想根据类别名称做同样的事情吗?

我的图书控制者有以下问题,但不确定如何正确执行

public function index(request $request, Author $author, Category $category)
{

    $author = $request->author;

    $books = Book::find()->author()->where('name', $author);

    $books = Book::with(['categories'])->where('name', $category);

    return response()->json($books, 200);
}

2 个答案:

答案 0 :(得分:0)

作者:

$books = App\Book::whereHas('author', function ($query) use ($authorName) {
    $query->where('name', $authorName);
})->get();

按类别:

$books = App\Book::whereHas('categories', function ($query) use ($categoryName) {
    $query->where('name', $categoryName);
})->get();

whereHas允许您执行对指定关系的查询

一起:

$books = App\Book::whereHas('author', function ($query) use ($authorName) {
        $query->where('name', $authorName);
    })->whereHas('categories', function ($query) use ($categoryName) {
        $query->where('name', $categoryName);
    })->get();

答案 1 :(得分:0)

要同时过滤作者和图书,可以同时使用gbalduzzi进行的两个查询:

$books = App\Book::whereHas('author', function ($query) use ($authorName) {
    $query->where('name', $authorName);
})->whereHas('categories', function ($query) use ($categoryName) {
    $query->where('name', $categoryName);
})->get();