Problem with Laravel scope in search form - php

I have search form that searches ads/properties through certain filters. I have problem when I use 'like' function my search fields for property id and user id don't work properly. For example when I put 1 in property id and submit form it returns all properties that have number 1 in their id (for example 1, 11, 21, 111). And when I change my code to be without 'like' function and just 'where' function it returns correct results, but then other filters don't work. Any help is appreciated. Here is my code.
Property.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Property extends Model
{
protected $guarded = ['id'];
public function scopeId($query, $id)
{
return $query->where('id', $id);
}
public function scopeUserId($query, $user_id)
{
return $query->where('user_id', 'like', '%'.$user_id.'%');
}
public function scopeTitle($query, $title)
{
return $query->where('title', 'like', '%'.$title.'%');
}
public function scopeDescription($query, $description)
{
return $query->where('description', 'like', '%'.$description.'%');
}
public function scopeActive($query, $active)
{
return $query->where('active', 'like', '%'.$active.'%');
}
public function scopeViews($query, $views)
{
return $query->where('page_views', 'like', '%'.$views.'%');
}
}
PropertyController.php
class PropertyController extends Controller
{
public function show(Request $request)
{
$properties = Property::id($request->id)
->userId($request->user_id)
->title($request->title)
->active($request->active)
->views($request->views)
->description($request->description)
->paginate(10);
return view('admin.properties.propertylist', compact('properties','request'));
}
}
propertylist.blade.php
<form action="{{route('admin.property')}}" method="get">
<label for="id">Id</label>
<input type="number" name="id" id="id" value="#if(!empty($request->id)){{$request->id}}#endif">
<label for="user_id">User id</label>
<input type="number" name="user_id" id="user_id" value="#if(!empty($request->user_id)){{$request->user_id}}#endif">
<label for="title">Title</label>
<input type="text" name="title" id="title" value="#if(!empty($request->title)){{$request->title}}#endif">
<label for="description">Description</label>
<input type="text" name="description" id="description" value="#if(!empty($request->description)){{$request->description}}#endif">
<label for="active">Status</label>
<select name="active"id="active">
<option value='' #if(empty($request->active))selected #endif>All</option>
<option value="A" #if(!empty($request->active) && $request->active == 'A')selected #endif>Approved</option>
<option value="Q" #if(!empty($request->active) && $request->active == 'Q')selected #endif>Queued</option>
<option value="S" #if(!empty($request->active) && $request->active == 'S')selected #endif>Suspicious</option>
<option value="D" #if(!empty($request->active) && $request->active == 'D')selected #endif>Denied</option>
</select>
<label for="views">Views</label>
<input type="text" name="views" id="views" value="#if(!empty($request->views)){{$request->views}}#endif">
<button class="btn btn-default" type="submit"> Submit </button>
</form>

Related

Search Functionality in Laravel not working

I am trying to search through my posts but clearly something is wrong since i am getting no results whether the query contains data present in my posts or not.
here is my blade file and search function
<h6 class="sidebar-title">Search</h6>
<form class="input-group" action="{{route('welcome-page')}}" method="GET">
<input type="search" class="form-control" name="search" placeholder="Search">
<div class="input-group-addon">
<span class="input-group-text"><i class="ti-search"></i></span>
</div>
</form>
search function
class WelcomeController extends Controller
{
public function index(){
$search = request()->query('search');
if ($search){
$posts = Post::where('title', 'LIKE', '%' .$search. '%')->paginate(1);
}else
{
$posts = Post::paginate(2);
}
return view('welcome')->with('categories', Category::all())
->with('tags', Tag::all())
->with('posts', Post::paginate(2));
}
I was trying to search the get the correct results

laravel filter return all the records

im trying to search using three inputs, in the first scenario when i search using all the inputs it return the result, in the second scenrio when i search by using only one field it return all the records what im trying to do is when i search with one field it return a specsific result,
public function multiSearch(Request $request){
$search = $request->input('search');
$search_1 = $request->input('search_1');
$search_2 = $request->input('search_2');
$cars = Car::query()
->where('price', 'LIKE', "%{$search}%")
->orWhere('model', 'LIKE', "%{$search_1}%")
->orWhere('transmission', 'LIKE', "%{$search_2}%")
->get();
return view('car.car-multisearch', ['cars'=>$cars]);
}
this is my blade code
<form type="get" action="{{url('/search-car')}}">
#csrf
<div class="row">
<div class="col-md-4">
<p>
<input type="text" name="search" placeholder="Price" />
</p>
</div>
<div class="col-md-4">
<p>
<input type="text" name="search_1" placeholder="Model Year" />
</p>
</div>
<div class="col-md-4">
<p>
<select name="search_2">
<option data-display="Transmission">Transmission</option>
<option value="Automatic">Automatic</option>
<option value="Manual">Manual</option>
</select>
</p>
</div>
<button type="submit" class="gauto-theme-btn">Find Car</button>
</div>
</form>
You can dynamically add where statements to your query depending on values presence in the request and if they are not empty. Example:
public function multiSearch(Request $request){
$cars = Car::query();
if ( $request->filled('search') ) {
$cars->where('price', 'LIKE', "%{$request->input('search')}%");
}
if ( $request->filled('search_1') ) {
$cars->orWhere('model', 'LIKE', "%{$request->input('search_1')}%");
}
if ( $request->filled('search_2') ) {
$cars->orWhere('transmission', 'LIKE', "%{$request->input('search_2')}%");
}
return view('car.car-multisearch', ['cars'=> $cars->get() ]);
}
This solution do the job, but looks not good because you will need to add more if statements for each filter.)

Sorting by price in Mongo Laravel

Trying to sort assets based on price.
blade.php
<form action="/assets/search/" method="GET" class="form-inline">
#csrf
<div class="searchByPrice pt-3">
<p class="font-weight-bold">Search By Price</p>
<label for="price">Volume</label>
<input type="range" min="0" max="500000" id="price" step="100" name="price"
oninput="outputUpdate(value)">
<output for="price" id="volume">250000</output>
<script>
function outputUpdate(vol) {
document.querySelector('#volume').value = vol;
}
</script>
</div>
</form>
Controller
public function show(Request $request, $_id)
{
$query = Asset::query();
if ($p = $request->price) {
$query->where('price', '<', $p);
}
$assets = $query->get();
return view('assets::assets',compact('assets'));
}
dd();
DD($request->price);
I'm not getting the desired results. Any Solutions? Or are there any alternative solutions for sorting by price in Laravel mongo?
What I was doing wrong is that I needed to return the query inside the if() statement. Like this
public function show(Request $request, $_id)
{
$query = Asset::query();
if ($p = $request->price) {
$query->where('price', '<', $p);
$assets = $query->get();
return view('assets::assets',compact('assets'));
}
}

dropdown list with several times the same item

Below my drop-down list is not displaying correctly and I don't know where the problem is. Could it be in my SerieController? I want to create an edit/update system but I've had no success.
SerielController
public function edit($id)
{
$series = Serie::find($id);
$marks = Mark::find($id);
return view('admin.series.edit', compact('series', 'marks'));
}
public function update(Request $request, $id)
{
$request->validate([
'name' => 'required|string',
'fk_mark' => 'required'
]);
$series = Serie::find($id);
$marks = Mark::find($id);
$series->name = $request->get('name');
$series->fk_mark = $request->get('fk_mark');
$series->save();
return redirect()->route('series.index')
->with('success', 'updated successfully');
}
file series.edit.blade
<form class="panel-body" action="{{route('series.update',$series->id)}}" method="POST">
<input name="_method" type="hidden" value="PATCH">
#csrf
<fieldset class="form-group">
<label for="form-group-input-1">Name</label>
<input type="text" name="name" class="form-control" id="form-group-input-1" value="{{$series->name}}">
</fieldset>
<div class="form-group">
<label for="company-content">Select Mark</label>
<select name="fk_mark" id="" class="form-control">
#foreach($series->marks as $mark)
<option value="{{$marks->id}}">
{{$marks->name_mark}}
</option>
#endforeach
</select>
</div>
Model Serie
class Serie extends Model
{
protected $fillable = ['name', 'fk_mark'];
public function marks(){
return $this->belongsTo('App\Mark', 'fk_mark');
}
}
Serie Mark
class Mark extends Model
{
protected $fillable = ['name_mark'];
public function series(){
return $this->hasMany('App\Serie', 'fk_mark');
}
public function cars(){
return $this->hasMany('App\Car','fk_serie');
}
}
Thank you a lot for your help.
In your foreach you made a mistake, the variable should be $mark not $marks
<select name="fk_mark" id="" class="form-control">
#foreach($marks as $mark)
<option value="{{$mark->id}}">
{{$mark->name_mark}}
</option>
#endforeach
</select>
In your edit function you're sending just one Mark to your view, you need to send them all if you want all marks in your select.
public function edit($id)
{
$series = Serie::find($id);
$marks = Mark::all();
return view('admin.series.edit', compact('series', 'marks'));
}

DropDown list for foreign key

I would like to learn to create a dropdown list with a foreign key on Laravel.
For information, I have a table named series with 3 fields id, name, fk_mark.
Then, I have another table named marks with 2 fields id, name_mark.
My create works correctly, here is the proof.
I am stuck about the dropdownlist, what is the syntax please for my foreign key ?
<fieldset class="form-group">
<label for="form-group-input-1">Name serie</label>
<input type="text" name="name" class="form-control" id="form-group-input-1">
</fieldset>
<fieldset class="form-group">
<label for="form-group-input-1">FK Mark</label>
<input type="text" name="fk_mark" class="form-control" id="form-group-input-1">
</fieldset>
I have tried this but without result...
<div class="form-group">
<label for="company-content">Select compagny</label>
<select name="fk_mark" class="form-control">
#foreach($series as $serie)
<option value="{{$serie->id}}"> {{$serie->name}} </option>
#endforeach
</select>
</div>
Here is my Models
Model Mark
class Mark extends Model
{
protected $fillable = ['name_mark'];
public function series(){
return $this->hasMany('App\Serie', 'fk_mark');
}
}
Model Serie
class Serie extends Model
{
protected $fillable = ['name', 'fk_mark'];
public function marks(){
return $this->belongsTo('App\Mark', 'fk_mark');
}
}
SerieController
public function index()
{
$series = Serie::oldest()->paginate(5);
return view('admin.series.index', compact('series'))
->with('i', (request()->input('page', 1)-1)*5);
}
public function create()
{
return view('admin.series.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'fk_mark' => 'required'
]);
Serie::create($request->all());
return redirect()->route('series.index')
->with('success', 'save');
}
Thank you very much for your help.

Categories