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'));
}
}
Related
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
I need to store the comment and then show it without reloading the page using AJAX
meaning that I will not use redirect() and not even go the route: /comment/create
I am using Laravel 8
the form in HTML "post.blade.php"
<form action={{route('comment.store')}} method="post">
#csrf
<div class="form-floating">
<textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea2" style="height: 100px" name="feedback"></textarea>
<label for="floatingTextarea2">Leave a Comment</label>
<button type="submit" class="btn btn-outline-info mt-1">feedBack</button>
<input type="hidden" name='postid' value={{$post->id}}>
</div>
</form>
the Route "web.php"
Route::post('/comment/create','App\Http\Controllers\CommentController#store')->name('comment.store');
Route::get('/articles/{id}/','App\Http\Controllers\ArticlesController#getArticle')->name('singleArticles');
The Article Controller: the function where sho the article
public function getArticle($id){
$row = DB::table('posts')
->join('categories','categories.id','=','posts.category_id')
->join('users','users.id','=','owner_id')
->select('posts.*','categories.name as name_categories','users.name as owner_name','users.id as owner_id')
->where('posts.id',$id)
->first();
$data['post'] = $row;
$data['page_title'] = $row->title;
$data['categories'] = DB::table('categories')->get();
//this line here is for importing comments and showing them later
$data['result'] = DB::table('comments')
->join('posts','posts.id','=','comments.post_id')
->join('users','users.id','=','contributor_id')
->select('comments.*','users.name as name')
->where('posts.id',$id)
->orderby('comments.id','desc')
->get();
return view('post',$data);
}
And the Comment Controller where I store the comments after submitting
public function store(Request $request)
{
$id_user = Auth::user()->id;
$ldate = date('Y-m-d H:i:s');
$feedback = $request->input('feedback');
$postid = $request->input('postid');
$data = array('contributor_id'=>$id_user,'text'=>$feedback,'post_id'=>$postid,'created_at'=>$ldate);
DB::table('comments')->insert($data);
return redirect()->route('singleArticles',$postid);
}
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>
i'm totally new to laravel. i'm trying to POST an input from another table but in same database using Helpers. I try to get name by email using Helpers. But i got this error:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\ims\resources\views\leave\add.blade.php)
Below is my Helpers:
public static function getnamebyemail($id)
{
$userdata = DB::table('hr_intern_table')->where('id', '=', $id)->first();
if(!empty($userdata))
{
return $userdata->name;
}
return '';
}
Below is the form that i try to get and post:
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div class="form-group form-float">
<label for="name" class="form-label">Trainee Name</label>
<div class="form-line">
<input type="text" class="form-control" name="name" disabled value="{{Helpers::getnamebyemail($data->name)}}"/>
</div>
</div>
</div>
Below is my controller:
public function add($id)
{
$data = DB::table('leave_req_tb')->where('id', '=', $id)->first();
return view('leave.add', compact('data', 'id'));
}
public function store_request(Request $request, $id)
{
$data = $this->validate($request, [
'leave_applied'=>'required',
'start_leave'=>'required',
'end_leave'=>'required',
'justification'=>'required',
'sv_approval'=>'required',
'hod_approval'=>'required',
'hr_approval'=>'required',
]);
DB::table('leave_req_tb')->where('id', '=', $id)->insert([
'intern_id'=>$intern_id,
'leave_applied'=>$request->leave_applied,
'start_leave'=>$request->start_leave,
'end_leave'=>$request->end_leave,
'justification'=>$request->justification,
'sv_approval'=>$request->sv_approval,
'hod_approval'=>$request->hod_approval,
'hr_approval'=>$request->hr_approval,
'created_at' => now(),
]);
return redirect('/leave/view')->with('success', 'The profile has been updated successfully.');
}
I appreciate if someone can help me and explain why i got this error because i don't think there is a problem with my code.
Helper functions are global and don't have a class
So in your Helpers.php
Just place the
function getnamebyemail($id)
{
$userdata = DB::table('hr_intern_table')->where('id', '=', $id)->first();
if(!empty($userdata))
{
return $userdata->name;
}
return '';
}
along with the required imports
In your blade just use
{{getnamebyemail($data->name)}}
After selecting Filter,I want to show summary tab/message what we selected.
I googled it and found session method, is it suitable in my case?
Here is my blade
{!! Form::open(['url'=>'/jobseekers','method'=>'GET', 'class'=>'form', 'id'=>'search_data']) !!}
<div class="form-group col-md-4">
<input type="text" name="fullname" placeholder="Name" value="{{ request()->input('fullname')}}" class="form-control"/>
</div>
<div class="form-group col-md-4">
<input type="text" name="fb_name" placeholder="Fb Name" value="{{ request()->input('fb_name')}}" class="form-control"/>
</div>
<button class="btn btn-flat btn-primary">Search</button>
</div>
{!! Form::close() !!}
and in my controller
public function index(Request $request)
{
$result = null;
if(count($request->all())!=0){
if ($request->has('sub_search')) {
$jobseekers = Jobseeker::Subsearch($request)->paginate(10);
dd($applicant_information);
}else{
$result=Jobseeker::Search($request)->paginate(10);
// dd($orders);
}
}
else{
$jobseekers = Jobseeker::with('calllogs')->orderBy('created_at', 'desc')->paginate(16);
}
return view('backend.jobseekers.index',compact('jobseekers','result'));
}
I am using get method to filter,and i want to show like
The Search results for fullname and fb_name are:
Is there any way to do like that in my case? Please guide me, thanks.
Are you trying to display filtered result in view? If so, change your code to:-
public function index(Request $request)
{
$fullname = $request->fullname;
$fb_name= $request->fb_name;
$result = null;
if(count($request->all())!=0){
if ($request->has('sub_search')) {
$jobseekers = Jobseeker::Subsearch($request)->paginate(10);
dd($applicant_information);
}else{
$result=Jobseeker::Search($request)->paginate(10);
// dd($orders);
}
}
else{
$jobseekers = Jobseeker::with('calllogs')->orderBy('created_at', 'desc')->paginate(16);
}
return view('backend.jobseekers.index',compact('jobseekers','result'))->with('fullname',$fullname)->with('fb_name',$fb_name);
}
All you need to is to access the passed variable from this controller is like
The Search results for {{$fullname}} and {{$fb_name}} are:
and loop your result here...