how to add date search like DATE_FROM and DATE_TO? i have already existing controller using search box and drop-down filter to my blade template, now how do i add date range filter. and also how to add it inside my date-picker in blade
Note: in my database i only have the default created_at column
Controller:
public function index(Request $request)
{
$search=$request->input('search');
if(request()->has('lead_status')){
$leads=Lead::where('lead_status', request('lead_status'))
->paginate(5)
->appends('lead_status',request('lead_status'));
}
else{
$leads=Lead::orderBy('created_at','desc')->search($search)->paginate(5);
}
return view ('leads.index')->with('leads',$leads);
}
VIEW Date-Picker
<div class="col-sm-4 form-group">
<label>FROM</label>
<input type="date" name="" value="" class="form-control">
</div>
<div class="col-sm-4 form-group">
<label>TO</label>
<input type="date" name="" value="" class="form-control">
</div>
With where date you can search by the date
and may more at here Laravel
Lead::whereDate('created_at', '=', date('Y-m-d'))->get();
WhereBetween
Lead::whereBetween('created_at', [date('Y-m-d', strtotime($input['from'])), date('Y-m-d', strtotime($input['to']))])->get();
Your soluation
$leads = Lead::query();
if($request()->has('lead_status')){
$leads = $leads->where('lead_status', $request('lead_status'));
}
if($request()->has('from') && $request()->has('to')){
{
$from_date = date('Y-m-d', strtotime($request()->has('from')));
$to_date = date('Y-m-d', strtotime($request()->has('to')));
$leads = $leads->whereBetween('created_at', [$from_date, $to_date]);
}
$leads = $leads->orderBy('created_at','desc')->paginate(5);
And in your controller access the request input parameter like so:
use Illuminate\Http\Request;
public function index(Request $request)
{
$search= $request->input('date');
$leads=Lead::where('lead_status''=', $search)->get();
return view ('leads.index')->with('leads',$leads);
}
ROUTE with
Route::get('/leads/{date}', 'YourControllerName#index');
Related
So, in my project, a user needs to register first before logging-in. I am trying to display their first, middle and last name. But, I am having a problem since multiple data are displayed on my input field. What is the correct query? Here is my controller code
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$input.="<input value={$first_name->seq_id}>{$first_name->first_name}</input>";
}
return $input;
}
public function step1()
{
$users = UserModel::all();
$data['optStatus']=$this->get_civil_status();
$data['displayFirstName']=$this->get_first_name();
return view ("enrollment-steps.step1", $data);
}
Here is the blade file
<div class="col-sm-12 col-lg-4">
<div class="form-group row">
<label for="firstName" class="col-sm-3 text-right control-label col-form-label">First
Name</label>
<div class="col-sm-9">
<input class="form-control" type="text" id='firstName' readonly value="{!! $displayFirstName !!}" >
</div>
</div>
</div>
this is the data that it displays
There is some confusing stuff going on there, but you probably want
<input class="form-control" type="text" id='firstName' readonly value="{{ $data['displayFirstName'] }}" >
which will display the value with the key 'displayFirstName' in the $data array.
dd is your friend, use this in your blade file to see what you have in your $data variable.
{{dd($data)}}
In you controller bind the data's in a single variable, then show it in the blade
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$input.="<input value={$first_name->seq_id}>{$first_name->first_name}</input>";
}
return $input;
}
Instead of this, do something like this:
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$displayname = $first_name->seq_id . $first_name->first_name;
$input.="<input value="{$displayname}"</input>";
}
return $input;
}
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'm trying to implement multiple timezone in laravel. I have already JS which collect users Timezone and successfully stored in 'timezone' column in Users table.
I have a view blade where user can choose the date and the time to schedule when their campaign will be sent. Im struggling to pass the users timezone to be shown in the view ("h:i A",). At the moment shows the timezone from config/app.php which is in my case 'Europe/Amsterdam'.
Here is the part of the view with bootstrap datepicker and time.
<div class="form-group">
<div class="col-md-5">
<label for="" class="form-label">Start Sending:</label>
<br>
<input type="radio" name="send_at" #if(old('send_at') != "now") checked #endif value="now" id="send-now">
<label for="send-now">Now</label><br>
<input type="radio" #if($mailing && strtotime($mailing->send_at) > time()) checked #endif name="send_at" value="latter" id="send-latter">
<label for="send-latter">Later</label><br>
<?php
$ts = time();
if($mailing && !old('send_date')) {
$ts = strtotime($mailing->send_at);
} elseif(old('send_date')) {
$ts = strtotime(old('send_date') .' '. old('send_time'));
}
?>
<div class="row">
<div class="col-md-6 pb-20">
<input class="datepicker form-control" type="text" name="send_date" value="{{ date("m/d/Y", $ts) }}">
</div>
<div class="col-md-6">
<div class="input-group bootstrap-timepicker timepicker">
<input type="text" name="send_time" value="{{ date("h:i A", $ts) }}" id="timepicker" class="form-control input-small">
<span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
<span class="input-group-addon">CET</span>
</div>
</div>
</div>
</div>
</div>
I tried to add this kind of attribute in my model but Im not sure which arguments I should add here to get time shown correctly in the view:
public function getCreatedAtAttribute($value)
{
$timezone = optional(auth()->user())->timezone ?? config('app.timezone');
return Carbon::parse($value)->timezone($timezone);
}
Here is the method from the model. I also need to convert time in the Send_At column If user use Australian time for example it will write exact Australian time in the database but campaign will be send 8 hours latter since time different and because my database is setup to European time. Im not sure how to achieve to convert time in that column.
protected $table = 'mailings';
public function saveMailing(User $user, $data)
{
$subscribers = isset($data['subscribers']) && $data['subscribers'] ? $data['subscribers'] : null;
$subscribersIds = array();
if($subscribers) {
$subscribersArr = explode(",", $subscribers);
if(count($subscribersArr) > 0) {
foreach ($subscribersArr as $sid) {
if($sid > 0) {
$subscribersIds[$sid] = $sid;
}
}
}
}
$this->user_id = $user->id;
$this->promo_id = $data['promo_id'];
$this->mailinglist_id = $data['mailinglist_id'] > 0 ? $data['mailinglist_id'] : null;
$this->subject = $data['subject'];
$this->body = $data['body'];
$this->send_at = $data['send_at'];
$this->type = isset($data['type']) && $data['type'] == 'reminder' ? 'reminder' : 'notification';
$this->subscribers = count($subscribersIds) > 0 ? implode(",", $subscribersIds) : null;
$this->save();
}
Thanks for your help!
I believe the easiest solution would be to add the following to the boot method in AppServiceProvider.php
public function boot()
{
date_default_timezone_set('Africa/Lagos');
}
If that doesn't work, you can try replacing time() with laravel's helper method - now(UTC) and provide the timezone as a parameter.
Also, change strtotime as in the following:
Carbon::createFromFormat('Y-m-d H:i:s', $mailing->send_at, 'UTC')
->setTimezone('Africa/Lagos')
you can change datetime format by following
Carbon doc
return Carbon::parse($value)->format('g:i A')->timezone($timezone);
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...