I have this in my view:
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12">
<form class="search-form search-form-basic" action="/candidates/index" method="post">
{{ csrf_field() }}
<div class="form-row">
<div class="col-md-4 form-group">
<label for="search_email">First name:</label>
<input type="text" name="search_first_name" id="search_first_name" class="form-control" #if(isset(Session::get('inputs')['search_first_name'])) value="{{ Session::get('inputs')['search_first_name'] }}" #endif>
</div>
<div class="col-md-4 form-group">
<label for="search_last_name">Last name:</label>
<input type="text" name="search_last_name" id="search_last_name" class="form-control" #if(isset(Session::get('inputs')['search_last_name'])) value="{{ Session::get('inputs')['search_last_name'] }}" #endif>
</div>
<div class="col-md-4 form-group">
<label for="search_round_number">Round Number:</label>
<input type="text" name="search_round_number" id="search_round_number" class="form-control" placeholder="" #if(isset(Session::get('inputs')['search_round_number'])) value="{{ Session::get('inputs')['search_round_number'] }}" #endif>
</div>
<div class="col-md-4 form-group">
<label for="search_location">location:</label>
<input type="text" name="search_location" id="search_location" class="form-control"
#if(isset(Session::get('inputs')['search_location'])) value="{{ Session::get('inputs')['search_location'] }}" #endif>
</div>
<select name="options" class="col-md-4 form-group">
<option value="">--- Select From ---</option>
<option value="birth_date">Birth Date</option>
<option value="CV_grade_date">CV Grade Date</option>
<option value="source">Source</option>
<option value="recommendation">Recommended</option>
<option value="created_at">Applied</option>
</select>
<div class="col-md-4 form-group">
<label for="search_from_date">From:</label>
<input type="date" name="search_from_date" id="search_from_date" class="form-control"
#if(isset(Session::get('inputs')['search_from_date'])) value="{{ Session::get('inputs')['search_from_date'] }}" #endif>
</div>
<div class="col-md-4 form-group">
<label for="search_to_date">To:</label>
<input type="date" name="search_to_date" id="search_to_date" class="form-control"
#if(isset(Session::get('inputs')['search_to_date'])) value="{{ Session::get('inputs')['search_to_date'] }}" #endif>
</div>
</div>
<div class="form-row">
<div class="col-md-12 col-lg-12">
<button type="submit" class="btn btn-custom"><i class="fa fa-search" aria-hidden="true"></i>Search</button>
<i class="fa fa-times-circle" aria-hidden="true"></i>Clear
</div>
</div>
</form>
</div>
My controller:
public function index(Request $request) {
if($request->isMethod('post')){
$search_first_name = $request->search_first_name;
$search_last_name = $request->search_last_name;
$search_email = $request->search_email;
$search_round_number = $request->search_round_number;
$search_location = $request->search_location;
$search_from_date = $request->search_from_date;
$search_to_date = $request->search_to_date;
$options = Input::get('options');
$recOrSource = null;
if($options == 'recommendation' || $options == 'source') {
$recOrSource = Input::get('options');
$options == null;
}
$candidate = DB::table('candidates')
->when($search_first_name, function ($query) use ($search_first_name) {
return $query->where('first_name', 'like', '%' . $search_first_name . '%');
})
->when($search_last_name, function ($query) use ($search_last_name) {
return $query->where('last_name', 'like', '%' . $search_last_name . '%');
})
->when($search_email, function ($query) use ($search_email) {
return $query->where('email', $search_email);
})
->when($search_round_number, function ($query) use ($search_round_number) {
return $query->where('round_number', $search_round_number);
})
->when($search_location, function ($query) use ($search_location) {
return $query->where('location', $search_location);
})
->when($recOrSource, function ($query) use ($recOrSource,$search_from_date,$search_to_date) {
return $query->where(!empty($recOrSource))->whereBetween('created_at', array($search_from_date, $search_to_date));
})
->when($options, function ($query) use ($options,$search_from_date,$search_to_date ) {
return $query->whereBetween($options, array($search_from_date, $search_to_date));
})
->orderBy('first_name', 'asc')
->orderBy('last_name', 'asc')
->get();
Session::flash('inputs', [
'search_first_name' => $search_first_name,
'search_last_name' => $search_last_name,
'search_email' => $search_email,
'search_round_number' => $search_round_number,
'search_from_date' => $search_from_date,
'search_to_date' => $search_to_date,
'search_location' => $search_location
]);
}else{
Session::forget('inputs');
$candidate = Candidate::orderBy('first_name', 'asc')
->orderBy('last_name', 'asc')
->get();
}
return view('/candidates/index', [
'candidate' => $candidate
]);
When I select "source" as one of the options, and enter from-to dates, I'm receiving this error:
Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select * from candidates where 1 is null and created_at between 2015-8-8 and 2019-1-1 and source between 2015-8-8 and 2019-1-1 order by first_name asc, last_name asc)
However, dd($recOrSource) is returning correct value; I don't see where this "1" is coming from, and why this part is run
->when($options, function ($query) use ($options,$search_from_date,$search_to_date ) {
return $query->whereBetween($options, array($search_from_date, $search_to_date));
})
when I have
if($options == 'recommendation' || $options == 'source') {
$recOrSource = Input::get('options');
$options == null;
}
Related
Whenever I visit the add-meeting URL, I receive an error message stating that $meeting is undefined. However, when I attempt to edit a meeting, everything works as expected. I am using the same blade file for both creating and updating meetings. Can you explain why this error is occurring?
Here is MeetingController:
public function add_meeting()
{
$customers = Customer::all();
$projects = Project::all();
return view('admin.meeting.add-meeting', get_defined_vars());
}
public function store(Request $request)
{
$this->validate($request, [
'meeting_scedule' => 'required',
'meeting_user_id' => 'required',
'project_id' => 'required',
'agenda' => 'required',
]);
if ($request->id) {
$input['meeting_scedule'] = $request->meeting_scedule;
$input['meeting_user_id'] = $request->meeting_user_id;
$input['project_id'] = $request->project_id;
$input['agenda'] = $request->agenda;
$meeting = Meeting::where('id', $request->id)->update($input);
return back()->with('success', 'Updated Successfully!');
} else {
$new['meeting_scedule'] = $request->meeting_scedule;
$new['meeting_user_id'] = $request->meeting_user_id;
$new['project_id'] = $request->project_id;
$new['agenda'] = $request->agenda;
$meeting = new Meeting();
$meeting->persist($new);
return back()->with('success', 'Meeting Created Successfully!');
}
}
public function edit_meeting($id)
{
$customers = Customer::all();
$projects = Project::all();
$meeting = Meeting::find($id);
return view('admin.meeting.add-meeting', get_defined_vars());
}
Here is add-meeting.blade.php file:
<form action="{{url('admin/update-meeting')}}" method="POST" id="add-rel-form"
enctype="multipart/form-data">
#csrf
#if(isset($meeting))
<input class="hidden" type="hidden" name="id" value="{{$meeting->id ?? ''}}">
#endif
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<label> Meeting Schedule</label>
<div class="form-group">
<input type="datetime-local" value="{{$meeting->meeting_scedule ?? ''}}" required
class="form-control" name="meeting_scedule" id="meeting_scedule">
</div>
</div>
<div class="form-group col-md-6 mb-0">
<label> User</label>
<select id="customer-select" class='form-control' required name="meeting_user_id">
<option value="" id="">--Select User--</option>
#foreach($customers as $customer)
<option value="{{$customer->id}}"{{ $customer->id == $meeting->meeting_user_id ? 'selected' : '' }}>{{$customer->first_name." ".$customer->last_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<label> Project</label>
<select id="project-select" class='form-control' required name="project_id" disabled>
<option value="" id="">--Select Project--</option>
#foreach($projects as $project)
<option value="{{$project->id}}"{{$project->id == $meeting->project_id ? 'selected' : ''}}>{{$project->project_type}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-6 mb-0">
<label> Agenda</label>
<div class="form-group">
<textarea type="text" value="" required
class="form-control" name="agenda" id="agenda">{{$meeting->agenda ?? ''}}</textarea>
</div>
</div>
</div>
Since there is no $meeting variable when creating a new meeting, the error occurs.
You can use null coalescing operator (??) or ternary operator (?:) to check if $meeting is defined before attempting to access its properties :
#if(isset($meeting))
<input class="hidden" type="hidden" name="id" value="{{$meeting->id}}">
#endif
...
<select id="customer-select" class='form-control' required name="meeting_user_id">
<option value="" id="">--Select User--</option>
#foreach($customers as $customer)
<option value="{{$customer->id}}" {{ isset($meeting) && $customer->id == $meeting->meeting_user_id ? 'selected' : '' }}>{{$customer->first_name." ".$customer->last_name}}</option>
#endforeach
</select>
...
<select id="project-select" class='form-control' required name="project_id" disabled>
<option value="" id="">--Select Project--</option>
#foreach($projects as $project)
<option value="{{$project->id}}"{{ isset($meeting) && $project->id == $meeting->project_id ? 'selected' : ''}}>{{$project->project_type}}</option>
#endforeach
</select>
...
<textarea type="text" value="" required class="form-control" name="agenda" id="agenda">{{ isset($meeting) ? $meeting->agenda : '' }}</textarea>
I'm working with Laravel 8 and at users.blade.php where all of the users of the website appears, I wanted to add a search form for searching the names, mobile number & other information related to a user:
<form method="GET" action="">
<div>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label for="name">Name or Last Name</label>
<input type="text" class="form-control" name="name"
value="{{ request()->query('name') }}">
</div>
<div class="col-md-3">
<label for="mnumber">Mobile Number</label>
<input type="text" class="form-control" name="order"
value="{{ request()->query('mnumber') }}">
</div>
<div class="col-md-3">
<label for="ucode">User Code</label>
<input type="text" class="form-control" name="product"
value="{{ request()->query('ucode') }}">
</div>
<div class="col-md-3">
<label for="ncode">National Number</label>
<input type="text" class="form-control" name="order"
value="{{ request()->query('ncode') }}">
</div>
</div>
</div>
</div>
</form>
Then at the Controller I tried this:
public function index()
{
$users = User::query();
if($keyword = request('name')) {
$users->where('name' , 'LIKE' , "%{$keyword}%");
}
$users = $users->latest()->paginate(20);
return view('admin.users.all', compact('users'));
}
But now the problem is when I fill the name field with a user name that already exists in the DB, it does not show that custom user because the if($keyword = request('name')) { condition does not run & request('name') is null!
In other words, when I submit the data using this url:
http://localhost:8000/admin/users?name=ENTERED_NAME&mnumber=&ucode=&ncode=
The result does not appear but when I submit it like this:
http://localhost:8000/admin/users?name=ENTERED_NAME
it shows result correctly!
So how can I properly search for the name field properly while the other fields are in the form?
public function index(Request $request)
{
$users = User::query();
if(isset($request->input('name'))) {
$keyword = $request->input('name');
$users->where('name' , 'LIKE' , "%{$keyword}%");
}
$users = $users->latest()->paginate(20);
return view('admin.users.all', compact('users'));
}
I use when() instead of if.. else.. you could try the query below and check if it works. I use similar types of query to search user.
$user = User::query()
->when(request()->has('name'), function ($query) {
$query->where('name', 'like', '%' . request('name') . '%');
})
->when(request()->has('mnumber'), function ($query) {
$query->where('mnumber', 'like', '%' . request('mnumber') . '%');
})
->when(request()->has('ucode'), function ($query) {
$query->where('ucode', 'like', '%' . request('ucode') . '%');
})
->when(request()->has('ncode'), function ($query) {
$query->where('ncode', 'like', '%' . request('ncode') . '%');
})
->paginate(20);
I think this might work without any modification to the view.
HTML Form Code
<form method="GET" action="">
<div>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label for="name">Name or Last Name</label>
<input type="text" class="form-control" name="name" value="{{ request()->query('name') }}">
</div>
<div class="col-md-3">
<label for="mnumber">Mobile Number</label>
<input type="text" class="form-control" name="mnumber" value="{{ request()->query('mnumber') }}">
</div>
<div class="col-md-3">
<label for="ucode">User Code</label>
<input type="text" class="form-control" name="ucode" value="{{ request()->query('ucode') }}">
</div>
<div class="col-md-3">
<label for="ncode">National Number</label>
<input type="text" class="form-control" name="ncode" value="{{ request()->query('ncode') }}">
</div>
</div>
</div>
</div>
</form>
Controller Code
Here in your code I suspect issue that in if condition you are not checking double equal you just place single equal. so I resolved it and place.
public function index()
{
$users = User::query();
if($keyword == request('name')) {
$users->where('name' , 'LIKE' , "%{$keyword}%");
}
$users = $users->latest()->paginate(20);
return view('admin.users.all', compact('users'));
}
Modified Controller Code
Here I write code with some Laravel standard and full code for other keywords also.
public function index()
{
$users = User::query();
if($request->has('name')) {
$keyword = $request->input('name');
$users->where('name' , 'LIKE' , "%{$keyword}%");
}
if($request->has('mnumber')) {
$keyword = $request->input('mnumber');
}
if($request->has('ucode')) {
$keyword = $request->input('ucode');
}
if($request->has('ncode')) {
$keyword = $request->input('ncode');
}
$users = $users->latest()->paginate(20);
return view('admin.users.all', compact('users'));
}
I have a search bar with 3 types of input and I want to search into my database if a data exist with those criteria.
Here is my html code for my search bar :
<form method="GET" action="{{ route('admin.liste.article.accueil.article') }}" class="mt-4 background_form">
<div class="form-row justify-content-center">
<div class="form-group col-md-4">
<label for="inputCity">Rechercher par ...</label>
<input class="form-control" name="rechercher" type="text" aria-label="Search" value="{{ request()->query('recherche') }}">
</div>
<div class="form-group col-md-2">
<label for="inputState">Type article</label>
<select id="inputState" class="form-control" name="type_article">
<option></option>
#foreach ($type_articles as $type_article)
<option value="{{ $type_article->type_article_id }}">{{ $type_article->libelle }}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-3">
<label for="datePublication">Date de publicaton</label>
<input class="form-control" type="text" placeholder="Exemple : 2021-02-29" name="date" value="{{ request()->query('date') }}">
</div>
<div class=" col-md-2 align-self-center mt-3">
<button type="submit" class="form-control btn-sm btn-custom mb-2">Trouver</button>
</div>
</div>
</form>
Into my controller :
public function accueil_article(Request $request)
{
$rechercher_par_accueil = $request->query('rechercher');
$type_article_rechercher_accueil = $request->query('type_article');
$date_publication_recherche_accueil = $request->query('date');
if($rechercher_par_accueil || $type_article_rechercher_accueil || $date_publication_recherche_accueil){
$article_rechercher = Article::where('titre', 'like', "%{$rechercher_par_accueil}%")
->when(request()->query('type_article_recherche'), function($query) {
$query->where('type_article_id', 'like', "%{$type_article_rechercher_accueil}%");
})
->when(request()->query('date_publication_recherche'), function($query) {
$query->where('created_at', 'like', "%{$date_publication_recherche_accueil}%");
})
->simplePaginate(5);
} else {
$article_rechercher = Article::orderBy('created_at','desc')->paginate(5);
}
return view('admin/article/admin_liste_article', [
'articles' => $article_rechercher,
'type_articles' => Type_article::all(),
'themes' => Theme::all()
]);
}
And at the bottom of my view, I've :
{!! $articles->appends(['rechercher' => request()->query('rechercher'), 'type_article' => request()->query('type_article'), 'date' => request()->query('date')])->links() !!}
Cordially
Try:
$query->whereIn('type_article_id', [$type_article_rechercher_accueil]);
I am trying to display items from database based on selected filters in my search form. I am not getting anything when I put some filters and submit form, and there should be some results that correspond to the filters. My url changes accordingly but with no results. I have three tables with many to many relationship that works.
properties (id, city, price, quadarature)
category_property (id, category_id, property_id)
categories (id, category, priority)
Any help is appreciated. Here is my code.
startpage.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-md-center">
<div class="col-md-8" id="forma">
<form id="searchForm" method="GET" action="/">
<div class="row">
<div class="col-md-12 mb-3">
<h5 class="text-center">City</h5>
<input name="city" list="result" id="input" class="form-control" value="{{ old('city') }}">
<datalist id="result"></datalist>
</div>
</div>
<div class="row">
<div class="col-md-3 mb-6">
<label>Price</label>
<input type="number" id="min_price" name="min_price" class="form-control" placeholder="Min Price" value="{{ old('min_price') }}">
<input type="number" id="max_price" name="max_price" class="form-control" placeholder="Max Price" value="{{ old('max_price') }}">
</div>
<div class="col-md-3 mb-6">
<label>Quadrature</label>
<input type="number" name="min_quadrature" class="form-control" placeholder="Min quadrature" value="{{ old('min_quadrature') }}">
<input type="number" name="max_quadrature" class="form-control" placeholder="Max quadrature" value="{{ old('max_quadrature') }}">
</div>
<div class="col-md-2 mb-6">
<h5>Payment Method</h4>
<div class="d-block my-3">
<div class="custom-control custom-radio">
<input id="ponuda" name="propertyBidAsk" value="ponuda" type="radio" class="custom-control-input" #if( old('propertyBidAsk')=='ponuda')checked #endif>
<label class="custom-control-label" for="ponuda">Ponuda</label>
</div>
<div class="custom-control custom-radio">
<input id="potraznja" name="propertyBidAsk" value="potraznja" type="radio" class="custom-control-input" #if( old('propertyBidAsk')=='potraznja')checked #endif>
<label class="custom-control-label" for="potraznja">Potraznja</label>
</div>
</div>
</div>
<div class="col-md-2 mb-6">
<h5>Property payment</h4>
<div class="d-block my-3">
<div class="custom-control custom-radio">
<input id="kupovina" name="propertyPayment" value="kupovina" type="radio" class="custom-control-input" #if( old('propertyPayment')=='Kupovina')checked #endif>
<label class="custom-control-label" for="kupovina">Kupovina</label>
</div>
<div class="custom-control custom-radio">
<input id="izdavanje" name="propertyPayment" value="izdavanje" type="radio" class="custom-control-input" #if( old('propertyPayment')=='Izdavanje')checked #endif>
<label class="custom-control-label" for="izdavanje">Izdavanje</label>
</div>
</div>
</div>
<div class="col-md-2 mb-6">
<h5>Property type</h4>
<div class="d-block my-3 ">
<div class="custom-control custom-radio">
<input id="kuca" name="propertyType" value="kuca" type="radio" class="custom-control-input" #if( old('propertyPayment')=='kuca')checked #endif>
<label class="custom-control-label" for="kuca">Kuca</label>
</div>
<div class="custom-control custom-radio">
<input id="stan" name="propertyType" value="stan" type="radio" class="custom-control-input" #if( old('propertyPayment')=='stan')checked #endif>
<label class="custom-control-label" for="stan">Stan</label>
</div>
<div class="custom-control custom-radio">
<input id="apartman" name="propertyType" value="apartman" type="radio" class="custom-control-input" #if( old('propertyPayment')=='apartman')checked #endif>
<label class="custom-control-label" for="apartman">Apartman</label>
</div>
<div class="custom-control custom-radio">
<input id="soba" name="propertyType" value="soba" type="radio" class="custom-control-input" #if( old('propertyPayment')=='soba')checked #endif>
<label class="custom-control-label" for="soba">Soba</label>
</div>
<div class="custom-control custom-radio">
<input id="lokal" name="propertyType" value="lokal" type="radio" class="custom-control-input" #if( old('propertyPayment')=='lokal')checked #endif>
<label class="custom-control-label" for="lokal">Lokal</label>
</div>
<div class="custom-control custom-radio">
<input id="plac" name="propertyType" value="plac" type="radio" class="custom-control-input" #if( old('propertyPayment')=='plac')checked #endif>
<label class="custom-control-label" for="plac">Plac</label>
</div>
<div class="custom-control custom-radio">
<input id="garaza" name="propertyType" value="garaza" type="radio" class="custom-control-input" #if( old('propertyPayment')=='garaza')checked #endif>
<label class="custom-control-label" for="garaza">Garaza</label>
</div>
</div>
</div>
</div>
<div class="row justify-content-md-center">
<div class="col-md-2 mb-6">
<button class="btn btn-primary btn-lg btn-block" type="submit">Search</button>
</div>
</div>
<hr class="mb-4">
</form>
</div>
</div>
</div>
<div class="row justify-content-md-center">
<div>
#if(isset($results))
<table class="table">
<thead>
<th>Property Bid Ask</th>
<th>Property Payment</th>
<th>Property Type</th>
<th>City</th>
<th>Price</th>
<th>Quadrature</th>
</thead>
<tbody>
#foreach ($results as $result)
<tr>
<td>{{ $result->category[0]->category }}</td>
<td>{{ $result->category[1]->category }}</td>
<td>{{ $result->category[2]->category }}</td>
<td>{{ $result->city }}</td>
<td>{{ $result->price }}</td>
<td>{{ $result->quadrature }}</td>
</tr>
#endforeach
</tbody>
</table>
#endif
</div>
</div>
</div>
#endsection
CategoryController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Property;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
class CategoryController extends Controller
{
public function index()
{
$properties = Property::where('active', '1')
->orderBy('page_views', 'desc')
->with('user.photo', 'photos', 'category')
->paginate(10);
return view('startpage', compact('properties'));
}
public function search(Request $request, $propertyBidAsk, $propertyPayment, $propertyType, $city, $price, $quadrature, Property $property)
{
$category = $property->category;
$query = Property::query();
if ($request->filled('propertyBidAsk')) {
$propertyBidAsk = $request->get('propertyBidAsk');
}
if ($propertyBidAsk) {
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . $request->propertyBidAsk . '%');
});
}
if ($request->filled('propertyPayment')) {
$propertyPayment = $request->get('propertyPayment');
}
if ($propertyPayment) {
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . $request->propertyPayment . '%');
});
}
if ($request->filled('propertyType')) {
$propertyType = $request->get('propertyType');
}
if ($propertyType) {
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . $request->propertyType . '%');
});
}
if ($request->filled('city')) {
$city = $request->get('city');
}
if ($city) {
$query->where('city', 'LIKE', '%' . $request->city . '%');
}
// PRICE
$array = explode('-', $price);
foreach ($array as $a) {
$s = [];
$s = explode('=', $a);
if ($s[0] === 'min_price') {
$s[1] ? $min_price = intval($s[1]) : $min_price = null;
} else {
$s[1] ? $max_price = intval($s[1]) : $max_price = null;
}
}
if ($request->filled('min_price')) {
$min_price = $request->get('min_price');
}
if ($request->filled('max_price')) {
$max_price = $request->get('max_price');
}
if ($min_price && $max_price) {
$query->whereBetween('price', [$min_price, $max_price]);
}
elseif ($min_price) {
$query->where('price', '>=', $min_price);
}
elseif ($max_price) {
$query->where('price', '<=', $max_price);
}
// QUADRATURE
$array1 = explode('-', $quadrature);
foreach ($array1 as $a1) {
$s1 = [];
$s1 = explode('=', $a1);
if ($s1[0] === 'min_quadrature') {
$s1[1] ? $min_quadrature = intval($s1[1]) : $min_quadrature = null;
} else {
$s1[1] ? $max_quadrature = intval($s1[1]) : $max_quadrature = null;
}
}
if ($request->filled('min_quadrature')) {
$min_quadrature = $request->get('min_quadrature');
}
if ($request->filled('max_quadrature')) {
$max_quadrature = $request->get('max_quadrature');
}
if ($min_quadrature && $max_quadrature) {
$query->whereBetween('quadrature', [$min_quadrature, $max_quadrature]);
}
elseif ($min_quadrature) {
$query->where('price', '>=', $min_quadrature);
}
elseif ($max_quadrature) {
$query->where('quadrature', '<=', $max_quadrature);
}
$results = $query->get();
return view('startpage', compact('category', 'results'));
}
}
web.php
Route::get('/', 'CategoryController#index')->name('startpage');
Route::get('/{propertyBidAsk}/{propertyPayment}/{propertyType}/
{city}/{price}/{quadrature}', 'CategoryController#search')->name('search.param');
I have search form to list properties/ads through certain criteria (city, price, quadrature, property type, etc). I am having trouble when I try to put some values in form to fetch those items that correspond in the database, I get empty table with no results. I have three tables.
properties (id, city, price, quadrature, property_type)
categories (id, category, priority)
category_property (id, property_id, category_id)
This relationship is many-to-many and it works, I checked that. And this categories table it has default values. In category row it has offer, demand, buy, rent, house, flat, where offer and demand have priority 0, buy and rent priority 1, house and flat priority 2. In my url when I click on submit button it should get something like this
project/search/offer/buy/house/Madrid/min_price=10000-max_price=15000/min_quadrature=20-max_quadrature=30
where offer, buy, house are those values from category row. I am stuck from there how to solve this, so I could display appropriate results. Any help is greatly appreciated. Here is my code:
search.blade.php
<div class="row justify-content-md-center">
<div class="col-md-8 order-md-1">
<div>
#if(isset($results))
<table class="table">
<thead>
<th>City</th>
<th>Price</th>
<th>Quadrature</th>
</thead>
<tbody>
#foreach ($results as $result)
<tr>
<th>{{ $result->city }}</th>
<td>{{ $result->price }}</td>
<td>{{ $result->quadrature }}</td>
</tr>
#endforeach
#else
#php
{{echo "not set";}}
#endphp
</tbody>
</table>
#endif
</div>
<form id="searchForm" method="GET" action="/search">
<div class="row">
<div class="col-md-5 mb-3">
<label>City</label>
<input name="city" list="result" id="input" class="form-control">
<datalist id="result"></datalist>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-6">
<label>Price</label>
<input type="number" name="min_price" class="form-control" placeholder="Min Price">
<input type="number" name="max_price" class="form-control" placeholder="Max Price">
</div>
<div class="col-md-6 mb-6">
<label>Quadrature</label>
<input type="number" name="min_quadrature" class="form-control" placeholder="Min quadrature">
<input type="number" name="max_quadrature" class="form-control" placeholder="Max quadrature">
</div>
</div>
<hr class="mb-4">
<div class="row">
<div class="col-md-4 mb-6">
<h5>Payment</h4>
<div class="d-block my-3">
<div class="custom-control custom-radio">
<input id="offer" name="propertyBidAsk" value="offer" type="radio" class="custom-control-input">
<label class="custom-control-label" for="offer">Offer</label>
</div>
<div class="custom-control custom-radio">
<input id="demand" name="propertyBidAsk" value="demand" type="radio" class="custom-control-input">
<label class="custom-control-label" for="demand">Demand</label>
</div>
</div>
</div>
<div class="col-md-3 mb-6">
<h5>Property payment</h4>
<div class="d-block my-3">
<div class="custom-control custom-radio">
<input id="buy" name="propertyPayment" value="buy" type="radio" class="custom-control-input">
<label class="custom-control-label" for="buy">Buy</label>
</div>
<div class="custom-control custom-radio">
<input id="rent" name="propertyPayment" value="rent" type="radio" class="custom-control-input">
<label class="custom-control-label" for="rent">rent</label>
</div>
</div>
</div>
<div class="col-md-5 mb-6">
<h5>Property type</h4>
<div class="d-block my-3 ">
<div class="custom-control custom-radio">
<input id="house" name="propertyType" value="house" type="radio" class="custom-control-input">
<label class="custom-control-label" for="house">House</label>
</div>
<div class="custom-control custom-radio">
<input id="flat" name="propertyType" value="flat" type="radio" class="custom-control-input">
<label class="custom-control-label" for="flat">Flat</label>
</div>
<hr class="mb-4">
<button class="btn btn-primary btn-lg btn-block">Search</button>
</form>
<script>
var onSubmitFunc = function(e){
e.preventDefault();
e.stopPropagation();
if( e.stopImmediatePropagation ){
e.stopImmediatePropagation();
}
var propertyBidAsk = this["propertyBidAsk"].value.trim();
var propertyPayment = this["propertyPayment"].value.trim();
var propertyType = this["propertyType"].value.trim();
var city = this['city'].value.trim();
var min_price = this["min_price"].value.trim();
var max_price = this["max_price"].value.trim();
var price = min_price + "_" + max_price;
var min_quadrature = this["min_quadrature"].value.trim();
var max_quadrature = this["max_quadrature"].value.trim();
var quadrature = min_quadrature + "_" + max_quadrature;
url = propertyBidAsk.length === 0 ? '' : ( '/' + encodeURIComponent(propertyBidAsk) );
url += propertyPayment.length === 0 ? '' : ( '/' + encodeURIComponent(propertyPayment) );
url += propertyType.length === 0 ? '' : ( '/' + encodeURIComponent(propertyType) );
url += city.length === 0 ? '' : ( '/' + encodeURIComponent(city) );
url += min_price.length === 0 ? '' : ( '/min_price' + "=" + encodeURIComponent(min_price) );
url += max_price.length === 0 ? '' : ( '-max_price' + "=" + encodeURIComponent(max_price) );
url += min_quadrature.length === 0 ? '' : ( '/min_quadrature' + "=" + encodeURIComponent(min_quadrature) );
url += max_quadrature.length === 0 ? '' : ( '-max_quadrature' + "=" + encodeURIComponent(max_quadrature) );
window.location.href = this.action + url;
}
document.addEventListener( 'DOMContentLoaded', function(){
var srch = document.getElementById("searchForm");
srch.addEventListener('submit', onSubmitFunc, false);
}, false );
</script>
</div>
</div>
CategoryController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Property;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
class CategoryController extends Controller
{
public function index()
{
$data = \DB::table('properties');
return view('categories.search', compact('data'));
}
public function search($propertyBidAsk, $propertyType, $propertyPayment, $city, $price, $quadrature, Request $request, Property $property)
{
$category = $property->category;
$results = null;
if (!empty($request->city)) {
$results = Property::all()->where('city', 'LIKE', "%" . $request->city . "%");
}
if (!empty($request->min_price) && !empty($request->max_price )) {
$results = Property::all()->where('price', '>=', $request->min_price)->where('price', '<=', $request->max_price);
}
if (!empty($request->min_quadrature) && !empty($request->max_quadrature )) {
$results = Property::all()->where('quadrature', '>=', $request->min_quadrature)->where('price', '<=', $request->max_quadrature);
}
return view('categories.search', compact('category', 'results'));
}
}
web.php
<?php
//search
Route::get('/search', 'CategoryController#index');
Route::get('/search/{propertyBidAsk}/{propertyPayment}/{propertyType}/
{city}/{price}/{quadrature}', 'CategoryController#search');
Please replace your search action in CategoryController with the following code:
public function search($propertyBidAsk, $propertyType, $propertyPayment, $city, $price, $quadrature, Request $request, Property $property)
{
$category = $property->category;
$property_obj = Property::query();
if (!empty($request->city)) {
$property_obj->where('city', 'LIKE', "%" . $request->city . "%");
}
if (!empty($request->min_price) && !empty($request->max_price)) {
$property_obj->where('price', '>=', $request->min_price)->where('price', '<=', $request->max_price);
}
if (!empty($request->min_quadrature) && !empty($request->max_quadrature)) {
$property_obj->where('quadrature', '>=', $request->min_quadrature)->where('price', '<=', $request->max_quadrature);
}
$results = $property_obj->get();
return view('categories.search', compact('category', 'results'));
}