title: Laravel query relationship based on name not id
body: <p>Hi I am new laravel and struggling a bit on understanding how to query relationships. I am trying to make a basic restful api in laravel and have 3 models</p>
  
  <pre><code>class Book extends Model
  {
  
      public function author()
      {
          return $this-&gt;belongsTo(Author::class);
      }
  
      public function categories()
      {
          return $this-&gt;belongsToMany('App\Category', 'category_book')
          -&gt;withTimestamps();
      }
  }
  
  class Author extends Model
  {
      public function books(){
          return $this-&gt;hasMany(Book::class);
      }
  }
  
  class Category extends Model
  {
      public function books()
      {
          return $this-&gt;belongsToMany('App\Book', 'category_book')
             -&gt;withTimestamps();
      }
  }
  </code></pre>
  
  <p>Table migrations: </p>
  
  <pre><code>Schema::create('books', function (Blueprint $table) {
          $table-&gt;engine = "InnoDB";
          $table-&gt;increments('id');
          $table-&gt;string('ISBN', 32);
          $table-&gt;string('title');
          $table-&gt;integer('author_id')-&gt;unsigned();
          $table-&gt;float('price')-&gt;default(0);
          $table-&gt;timestamps();
      });
  
  Schema::create('authors', function (Blueprint $table) {
          $table-&gt;engine = "InnoDB";
          $table-&gt;bigIncrements('id');
          $table-&gt;string('name');
          $table-&gt;string('surname');
          $table-&gt;timestamps();
      });
  
  Schema::create('categories', function (Blueprint $table) {
          $table-&gt;engine = "InnoDB";
          $table-&gt;bigIncrements('id');
          $table-&gt;string('name');
          $table-&gt;timestamps();
      }); 
  
  Schema::create('category_book', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;integer('category_id')-&gt;unsigned();
          //$table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories')-&gt;onDelete('cascade');
          $table-&gt;integer('book_id')-&gt;unsigned();
          //$table-&gt;foreign('book_id')-&gt;references('id')-&gt;on('books')-&gt;onDelete('cascade');
          $table-&gt;timestamps();
      });   
  </code></pre>
  
  <p>books is the main table and author has a one to many relationship with books. Category has a many to many relationship with books as a book can be in more than one category. </p>
  
  <p>The books table has an author_id field to link it to the authors table. There is also a pivot table called category_books that contains category_id and book_id to link books to categories </p>
  
  <p>But how do I query books so that it returns only books based on the authors name ?</p>
  
  <p>I would also like to do the same thing based on the category name?</p>
  
  <p>I my books controller i have the following but not sure how to do it correctly</p>
  
  <pre><code>public function index(request $request, Author $author, Category $category)
  {
  
      $author = $request-&gt;author;
  
      $books = Book::find()-&gt;author()-&gt;where('name', $author);
  
      $books = Book::with(['categories'])-&gt;where('name', $category);
  
      return response()-&gt;json($books, 200);
  }
  </code></pre>