I am trying to implement a search so that user can input a book's title and laravel will browse the database and display results that match any character.
My 'book' table contains a 'title' field.
However if I input for example 'f' I get:
Call to undefined method Illuminate\Database\Query\Builder::title()
Search blade:
@extends('layouts.master')
@section('title')
@section('content')
<h1>Search for books</h1>
<form action="" method="POST">
<div>
<input type='text' name='book' placeholder='Enter any character' />
</div>
<input type="submit" name="submitBtn" value="Search">
</form>
@endsection
Details = Result page:
@extends('layouts.master')
@section('title')
@section('content')
<h1>User Details</h1>
<form>
<p>
<ul>
@foreach ($books as $book)
<a href= "">
</a>
</p>
</form>
@endforeach
</ul>
@endsection
Controller:
function search()
{
$books = Book::all();
return view('layouts/search',['books' => $books]);
}
function details() {
$searchTerms = explode(' ', \Request::input('book'));
$books = Book::whereHas('title', function($query) use($searchTerms) {
if(!empty($searchTerms)){
for
each($searchTerms as $book) {
$query->where('book', 'LIKE', '%'. $book .'%');
}
}
})->first();
return view('layouts/details', compact('book'));
}
via Przemek