I made my custom search for my small car project and I have two problems now. Let me first show my code:
This is my SearchController :
<?php
namespace App\Http\Controllers;
use App\Car;
use DB;
use Illuminate\Http\Request;
class SearchController extends Controller
{
// ovo je search controller
public function index(){
$cars = Car::all();
return view('searchcreate')->with('cars', $cars);
}
public function blogs(Request $request){
$from = $request->input('from');
$to = $request->input('to');
$search = $request->input('fuel');
$cars = DB::table('cars')
->where('brand', '=', $request->input('brand'))
->where('model', '=', $request->input('model'))
->where('fuel', '=', $request->input('fuel'))
->where('gear', '=', $request->input('gear'))
->whereBetween('price', [$from, $to])
->get();
return view('search')->with('cars', $cars);
}
}
And this is my search page:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Search</div>
<div class="card-body">
{!! Form::open(['action' => 'SearchController@blogs', 'method' => 'GET']) !!}
<div class="column">
<label for="name">Brand:</label></br>
<select class="form-control" name="brand" id="input" onchange="random_function()">
<option value="" selected>Select your option</option>
<option value="Volkswagen">Volkswagen</option>
<option value="BMW">BMW</option>
<option value="Audi">Audi</option>
</select>
</div>
<br>
<div class="column">
<label for="model">Model:</label></br>
<select class="form-control" id="output" name="model">
</select>
</div>
<br>
<div class="column">
<label for="fuel">Fuel:</label></br>
<select class="form-control" name="fuel">
<option value="" disabled selected>Select your option</option>
<option value="diesel">Diesel</option>
<option value="gasoline">Gasoline</option>
</select>
</div>
<br>
<div class="column">
<label for="gear">Gear:</label></br>
<input name="gear[]" type="checkbox" placeholder="" value="ABS">ABS
<input name="gear[]" type="checkbox" placeholder="" value="CL">Child lock
<input name="gear[]" type="checkbox" placeholder="" value="navigation">Navigation
<input name="gear[]" type="checkbox" placeholder="" value="isofix">Isofix
</div>
<br>
<div class="column">
<label for="from">Price from:</label></br>
<input name="from" type="text" placeholder="" class="form-control" value="0">
</div>
<br>
<div class="column">
<label for="to">Price to:</label></br>
<input name="to" type="text" placeholder="" class="form-control" value="150000">
</div>
<br>
<button type="submit" class="btn btn-primary">
Search
</button>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
@endsection
So my two problems are:
First problem is that all my search inputs are required, I need if user type just brand or just fuel to show him all cars with that specific brand or fuel.
Second problem is my gear input, it is an array that users select when creating car post. And right now if user want to search for some car with for example just ABS while searching user needs to check exactly gear that car post have. I need my search to find any car post with only checked gear, so no need to check exactly gear.
What is solution to these two problems?
EDITED:
My cars migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCarsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('brand');
$table->string('model');
$table->string('fuel');
$table->string('gear');
$table->string('price');
$table->integer('user_id');
$table->dateTime('refresh');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cars');
}
}
My CarsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Car;
use DB;
use Carbon\Carbon;
class CarsController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth', ['except' => ['index', 'show']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$cars = Car::orderBy('refresh', 'desc')->get();
return view('cars.index')->with('cars', $cars);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('cars.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'brand' => 'required',
'model' => 'required',
'fuel' => 'required',
'gear' => 'required',
'price' => 'required'
]);
$car = new Car;
$car->brand = $request->input('brand');
$car->model = $request->input('model');
$car->fuel = $request->input('fuel');
$car->gear = implode(',', $request->input('gear'));
$car->price = $request->input('price');
$car->user_id = auth()->user()->id;
$car->refresh = Carbon::now();
$car->save();
return redirect('/cars');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$car = Car::find($id);
return view('cars.show')->with('car', $car);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$car = Car::find($id);
return view('cars.edit')->with('car', $car);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'brand' => 'required',
'model' => 'required',
'fuel' => 'required',
'gear' => 'required',
'price' => 'required'
]);
$car = Car::find($id);
$car->brand = $request->input('brand');
$car->model = $request->input('model');
$car->fuel = $request->input('fuel');
$car->gear = implode(',', $request->input('gear'));
$car->price = $request->input('price');
$car->user_id = auth()->user()->id;
$car->save();
return redirect('/cars');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$car = Car::find($id);
$car->delete();
return redirect('/cars');
}
And my create.blade.php for cars table:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create car post
</div>
<div class="card-body">
{!! Form::open(['action' => 'CarsController@store', 'method' => 'POST']) !!}
<div class="column">
<label for="brand">Brand:</label></br>
<select class="form-control" id="input" name="brand" onchange="random_function()">
<option value="" disabled selected>Select your option</option>
<option value="Volkswagen">Volkswagen</option>
<option value="BMW">BMW</option>
<option value="Audi">Audi</option>
</select>
</div>
<br>
<div class="column">
<label for="model">Model:</label></br>
<select class="form-control" id="output" name="model">
</select>
</div>
<br>
<div class="column">
<label for="fuel">Fuel:</label></br>
<select class="form-control" name="fuel">
<option value="diesel">Diesel</option>
<option value="gasoline">Gasoline</option>
</select>
</div>
<br>
<div class="column">
<label for="gear">Gear:</label></br>
<input name="gear[]" type="checkbox" placeholder="" value="ABS">ABS
<input name="gear[]" type="checkbox" placeholder="" value="CL">Child lock
<input name="gear[]" type="checkbox" placeholder="" value="navigation">Navigation
<input name="gear[]" type="checkbox" placeholder="" value="isofix">Isofix
</div>
<br>
<div class="column">
<label for="price">Price:</label></br>
<input name="price" type="text" placeholder="" class="form-control">
</div>
<br><br>
<button type="submit" class="btn btn-primary">
Submit
</button>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
@endsection
store functionthat is withimplode