You can use eloquent relationship
https://laravel.com/docs/5.2/eloquent-relationships#defining-relationships
Here you have tutorial
https://scotch.io/tutorials/a-guide-to-using-eloquent-orm-in-laravel#querying-relationships
This is commonly used in e-commerce sites, like serarching products based on some category. Well, what I want to retrieve is, for example, all products that belongs to books, if I select books. I think I have to insert some if statements in Controller in order for each submit action, but not sure.
Here is what I have up to now...
View
{!! Form::open(['method' => 'GET', 'route'=>'products', 'class' => 'navbar-form']) !!}
{{ Form::select('category_id', [
'' => 'select category,
'1' => 'books',
'2' => 'clothes',
'3' => 'eletronics'],
null, ['class' => 'form-control'])
}}
{{ Form::submit('submit, ['class' => 'submit btn']) }}
{!! Form::close() !!}
ProductsController
pubic function search()
{
$products = Product::all();
$products->category_id = Input::get('category_id');
return view('products.index', compact('products'));
}
Route
Route::get('products', ['as' => 'products', 'uses' => 'ProductsController@products']);
Appreciate your help.
Please or to participate in this conversation.