Laravel searches for "LIKE" query in two tables

I'm currently trying to customize a search bar that filters the results of two tables, a book and a category .

I have an installation relationship for both models, where:

Book.php (Model) (table: id, b_name, b_author, cat_id)

public function bookCategory()
{
  return $this->belongsTo('Category', 'cat_id', 'id');
}

Category.php (Model) (table: id, cat_name)

public function book()
{
  return $this->hasMany('Book', 'cat_id', 'id');
}

BookController.php

public function getFilterBooks($input)
{
  $books = Book::with('bookCategory')->where('**cat_name at category table**. 'LIKE', '%' . $input . '%'')->get();

  return Response::json($books);
}

But obviously this will not work. The reason I do this is because I want to allow users to use the same search bar to filter different columns (that I know how to do this in one table, but not in two or more).

+4
1

.

Book::whereHas('bookCategory', function($q) use ($input)
{
    $q->where('cat_name', 'like', '%'.$input.'%');

})->get();

http://laravel.com/docs/4.2/eloquent#querying-relations

EDIT:

Book::with('bookCategory')->whereHas('bookCategory', function($q) use ($input)
    {
        $q->where('cat_name', 'like', '%'.$input.'%');

    })->get();

cat_name .

+8

All Articles