I am very new to laravel and i know there are many treads about it but i dont understand it.. i try it many days now.
I want add an Search form in my Laravel page.
Here my form index script:
<form action="/search" method="POST" role="search">
<div class="input-group">
<input type="text" class="form-control" name="q"
placeholder="Search users"> <span class="input-group-btn">
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</form>
After it i have create an "search.blade.php" file and paste it in "view" folder and route the page in "web.php" ,works fine.
search.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Search Form</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="http://ift.tt/1JetChn">
</head>
<body>
<div class="container">
@if(isset($product))
<h2>Sample Product details</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Product Name</th>
<th>Product Description</th>
</tr>
</thead>
<tbody>
@foreach($product as $dummy)
<tr>
<td></td>
<td></td>
</tr>
@endforeach
</tbody>
</table>
{!! $product->render() !!}@endif
</div>
<div class="container">
@if(isset($name))
<p> The Search results for your query <b> </b> are :</p>
<h2>Sample User details</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Product Name</th>
<th>Product Description</th>
</tr>
</thead>
<tbody>
@foreach($details as $product)
<tr>
<td></td>
<td></td>
</tr>
@endforeach
</tbody>
</table>
@if($details){!! $details->render() !!}@endif
@elseif(isset($message))
<p></p>
@endif
</div>
</body>
</html>
web.php
Route::get('/search', function () {
return view('search');
});
Route::get ( '/', function () {
$dummyDetails = Product::paginate(15);
return view ( 'search' )->withProduct($dummyDetails);
} );
Route::any ( '/search', function () {
$q = Input::get ( 'q' );
if($q != ""){
$product = Product::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'description', 'LIKE', '%' . $q . '%' )->paginate (5)->setPath ( '' );
$pagination = $product->appends ( array (
'q' => Input::get ( 'q' )
) );
if (count ( $product ) > 0)
return view ( 'search' )->withDetails ( $product )->withQuery ( $q );
}
return view ( 'search' )->withMessage ( 'No Details found. Try to search again !' );
} );
Error comes from here:
$product = Product::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'description', 'LIKE', '%' . $q . '%' )->paginate (5)->setPath ( '' );
I have checked my Script. Product.php is in the "APP" folder but maybe works only for "Product" Folder and not "View" Folder?
I am very happy about any Help please! I hope i have post it here all correct with the needed infos.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire