How to view data based on date range using laravel and mysql - php

Route:
Route::post('dategraph','Chatbot\TrackerController#dategraph');
Controller:
public function dategraph(Request $request)
{
$dategraph = DiraStatistics::all()->whereBetween('date_access', [$from, $to])->get();
$dates = $dategraph('date_access');
return view('AltHr.Chatbot.graph', compact('dates'));
}
View:
<form id="form-project" role="form" action="{{action('AltHr\Chatbot\TrackerController#dategraph')}}" autocomplete="off" method="POST">
{{csrf_field()}}
<!-- <canvas id="myChart" width="150" height="50"></canvas> -->
<div class="form-group-attached">
<div class="row">
<div class="col-lg-6">
<div class="form-group form-group-default required" >
<label>From</label>
<input type="date" class="form-control" name="from" required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group form-group-default required" >
<label>To</label>
<input type="date" class="form-control" name="to">
</div>
</div>
</div>
</div>
<button class="btn alt-btn-black btn-xs alt-btn pull-right" type="submit">Next</button>
</form>
Hi guys, so im trying to view the data from the selected dates as the code ive written. But im getting an error. Did i write it correctly? or am i missing something?

You do not have a $from variable.
You need to pull out posted variables from the request.
The method get() will return a Collection of objects. You can, for example, turn it to a flat array by plucking the column and turning it toArray()
$dategraph = DiraStatistics::whereBetween(
'date_access',
[
$request->get('from'),
$request->get('to')
]
)->get();
$dates = $dategraph->pluck('date_access')->toArray();

Related

Form data comes null in Laravel [duplicate]

This question already has answers here:
Does form data still transfer if the input tag has no name?
(3 answers)
Closed 3 years ago.
i don't understand why my form data comes null, someone can help me please ?
Contorller (CartesController.php):
public function store(Request $request)
{
$carte = new Cartes();
dd(request('numero')); // null
}
Route (web.php):
Route::post('/addcartes', 'CartesController#store');
Form (addcartesview.blade.php):
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-xl-12">
<div class="card">
<div class="card-header">Ajouter une carte : </div>
<div class="card-body">
<form method="post" action="./addcartes">
{{ csrf_field() }}
<label for="numero">Numero</label>
<input type="number" class="form-control" id="numero" placeholder="Numero" >
<button type="submit" class="btn btn-primary">Add Card</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection}
Your form is wrong. it is not
<input type="number" class="form-control" id="numero" placeholder="Numero" >
It should be
<input type="number" class="form-control" id="numero" placeholder="Numero" name="numero" >
Your controller is wrong. it is not
dd(request('numero'));
It should be
dd($request->input('numero'));
If you want to print out all request for debugging you should use
dd($request->all());
Read more about Laravel request here.
add name attribute on input tag
Check your action and try dd($request->all());

Can I block one table from updating?

I have two tables in my database one practitioner and the second one practitioner_specialty, both of them have fields effective_date and expiry_date. When I use the form to update both if those tables the last effective_date and expiry_date are being saved to both of the tables instead of two separate ones.
Is there a way to block one table from updating so I can update only the second one, or maybe is there a way to save it using different id/name so they will be unique ones for practitioner and practitioner_specialty?
Here are my form and controller used for updating tables.
Controller:
public function updatePractitioner(Request $request, $id)
{
$this->validate($request, [
'effective_date' => 'required',
]
);
$fields = $request->all();
$primary_key = $this->PractitionerRepository->getIdName();
$primary_key_specialty = $this->PractitionerSpecialtyRepository->getIdName();
$practitioner_specialty_id = PractitionerSpecialty::where('practitioner_id', $id)->value('practitioner_specialty_id');
$fields[$primary_key] = $id;
$this->PractitionerRepository->update($fields);
$fields[$primary_key_specialty] = $practitioner_specialty_id;
$this->PractitionerSpecialtyRepository->update($fields);
return back()->with('successUpdate', 'Practitioner has been updated!');
}
update form in blade.php :
<div class="edit-practitioner" style="display: none;">
<form style="box-shadow: none;" action="/practitioner/update/{{$practitioner->practitioner_id}}" method="post"
class="j-pro" id="update-practitioner-form">
{{ csrf_field() }}
<div class="j-content">
<div id="j-row-id" class="j-row">
<div class="row">
<div class="col-sm-12 col-lg-12 col-xl-5">
<div class=" j-unit">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>Practitioner Information</span>
</div>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->effective_date}}"
name="effective_date" id="effective_date">
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->expiry_date}}"
name="expiry_date" id="expiry_date">
</div>
<label class="j-label">{{trans('personalData.phoneNumber')}}</label>
<div class="j-input">
</label>
<input type="tel" value="{{$practitioner->phone}}"
name="phone" id="phone">
</div>
<label class="j-label">{{trans('personalData.mobileNumber')}}</label>
<div class="j-input">
<input type="tel" value="{{$practitioner->mobile}}"
name="mobile" id="mobile">
</div>
<label class="j-label">{{trans('personalData.email')}}</label>
<div class="j-input">
<input type="email" value="{{$practitioner->email}}"
name="email" id="email">
</div>
</div>
</div>
<div class="col-xl-1 j-unit"></div>
<div class="col-sm-12 col-lg-12 col-xl-6">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>{{trans('settings.specialty')}}</span>
</div>
<select name="practitioner_specialty_id_update"
id="practitioner_specialty_id_update"
class="form-control-practitioner required">
#foreach($specialties as $specialty)
<option
value="{{$specialty->specialty_id}}">{{$specialty->name}}</option>
#endforeach
</select>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
#isset($practitioner_specialty->practitioner_specialty_id)
<input type="date" value="{{$practitioner_specialty->effective_date}}"
name="effective_date" id="effective_date">
#endisset
#empty($practitioner_specialty->practitioner_specialty_id)
<input type="date" name="effective_date" id="effective_date">
#endempty
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
#isset($practitioner_specialty->practitioner_specialty_id)
<input type="date" value="{{$practitioner_specialty->expiry_date}}"
name="expiry_date" id="expiry_date">
#endisset
#empty($practitioner_specialty->practitioner_specialty_id)
<input type="date" name="expiry_date" id="expiry_date">
#endempty
</div>
</div>
</div>
</div>
<div class="j-divider j-gap-bottom-45 j-gap-top-10"></div>
<button type="submit"
class="btn btn-editpanel btn-success btn-round">Save changes
</button>
<!-- end /.footer -->
<button id="update-cancel-button-practitioner" href="javascript:window.location.href=window.location.href" type="button"
class="btn btn-editpanel btn-danger btn-round">Cancel
</button>
</div>
</form>
</div>
Well you use the same array for both of the tables, hence the reason to update all the fields. You can try to exclude the ones for one form and add them to the other, for example:
$practictionerFields = $request->except('expiry_date', 'effective_date');
$practictionerFields[$primary_key] = $id;
$this->PractitionerRepository->update($practictionerFields);
// and use the other $fields array for the PractitionerSpecialty model.
You can use LOCK query here.
lock is a flag associated with a table. MySQL allows a client session to explicitly acquire a table lock for preventing other sessions from accessing the same table during a specific period. A client session can acquire or release table locks only for itself. It cannot acquire or release table locks for other sessions.
You can read more here: http://mysqltutorial.org/mysql-table-locking

I want to update values in two tables

I created an edit form to update values in my database and then show it on the main page. The problem is that it doesn't save the data to DB.
the request passes: Status Code: 302 Found
with all the values that I want to change (for example adding phone and mail):
effective_date_practitioner: 2019-01-01
expiry_date_practitioner:
phone_number_practitioner: 918273645
mobile_number_practitioner:
email_practitioner: test#test.pl
practitioner_specialty_id_update: 1
effective_date_specialty: 2019-01-01
expiry_date_specialty:
Edit blade
<div class="edit-practitioner" style="display: none;">
<form style="box-shadow: none;" action="/practitioner/update/{{$practitioner->practitioner_id}}" method="post"
class="j-pro" id="update-practitioner-form">
#csrf
<div class="j-content">
<div id="j-row-id" class="j-row">
<div class="row">
<div class="col-sm-12 col-lg-12 col-xl-5">
<div class=" j-unit">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>Practitioner Information</span>
</div>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->effective_date}}"
name="effective_date_practitioner">
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->expiry_date}}"
name="expiry_date_practitioner">
</div>
<label class="j-label">{{trans('personalData.phoneNumber')}}</label>
<div class="j-input">
</label>
<input type="tel" value="{{$practitioner->phone}}"
name="phone_number_practitioner">
</div>
<label class="j-label">{{trans('personalData.mobileNumber')}}</label>
<div class="j-input">
<input type="tel" value="{{$practitioner->mobile}}"
name="mobile_number_practitioner">
</div>
<label class="j-label">{{trans('personalData.email')}}</label>
<div class="j-input">
<input type="email" value="{{$practitioner->email}}"
name="email_practitioner">
</div>
</div>
</div>
<div class="col-xl-1 j-unit"></div>
<div class="col-sm-12 col-lg-12 col-xl-6">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>{{trans('settings.specialty')}}</span>
</div>
<select name="practitioner_specialty_id_update"
id="practitioner_specialty_id_update"
class="form-control-practitioner required">
#foreach($specialties as $specialty)
<option
value="{{$specialty->specialty_id}}">{{$specialty->name}}</option>
#endforeach
</select>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner_specialty->effective_date}}"
name="effective_date_specialty">
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner_specialty->expiry_date}}"
name="expiry_date_specialty">
</div>
</div>
</div>
</div>
<div class="j-divider j-gap-bottom-45 j-gap-top-10"></div>
<button type="submit"
class="btn btn-editpanel btn-success btn-round">Save changes
</button>
<!-- end /.footer -->
<button id="update-cancel-button-practitioner" type="button"
class="btn btn-editpanel btn-danger btn-round">Cancel
</button>
</div>
</form>
</div>
web.php
Route::post('/practitioner/update/{id}', 'Crud\Settings\PractitionerController#updatePractitioner')->name('updatePractitioner');
Controller:
<?php
namespace App\Http\Controllers\CRUD\Settings;
use App\Models\Practitioner;
use App\Models\PractitionerCompetenceLevel;
use App\Models\PractitionerSpecialty;
use App\Repositories\PractitionerRepository;
use Illuminate\Http\Request;
class PractitionerController extends CrudController
{
protected $repository;
public function __construct(PractitionerRepository $repository)
{
$this->middleware('auth');
$this->repository = $repository;
}
public function updatePractitioner($id, Request $request)
{
$this->validate($request, [
'effective_date_practitioner' => 'required',
'effective_date_specialty' => 'required',
]
);
$input = $request->all();
$input['data'][$this->repository->getIdName()] = $id;
$this->repository->update($input['data']);
return back()->with('successUpdate', 'Practitioner has been updated!');
}
}
My guess is that the data I want to update belongs to two different tables in DB one called practitioner and the other one called practitioner_specialty
As per your code you are trying to do mass assignment to your model
You may do this using the $fillable property on the model
protected $fillable = ['effective_date_practitioner','effective_date_specialty',......];
And you can use attach() method to updated data in related tables

How to edit one column in a row of the database in laravel

How to edit one column in a row of the database in laravel
I can't update one column of row has multiple columns by laravel
My edit :
public function edit($id)
{
$addremark=bookappoitment::findOrFail($id);
return view('admin.ManageTime.addremarks', compact('addremark'));
}
My update:
public function update(Request $request, $id)
{
$request->validate([
'Remarks'=>'required'
]);
$data=bookappoitment::find($id);
$data->Remarks = $request->get('Remarks');
$data->save();
return view('/home');
}
link to update:
Update
form:
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Remarks :</label>
<textarea class="form-control" rows="10" name="Remarks" placeholder="Remarks">{{ $addremark->Remarks }}</textarea>
</div>
<a class="btn btn-success btn-mini deleteRecord " href="{{route('BookAppoint.update',$addremark->id)}}">Update</a>
</div>
</div>
</div>
You can update 1 (or more, just add to the array) column through the DB class.
DB::table('yourTable')->where('id', $id)->update(['remarks' => $request->input('Remarks')]);
You can also do it for a model like so:
bookappoitment::where('id', $id)->update(['remarks' => $request->input('Remarks')]);
It's in the Laravel documentation here.
<form class="" action="index.html" method="{{ route('BookAppoint.update', $addremark->id) }}">
#method('PATCH')
#csrf
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Remarks">Remarks :</label>
<textarea name="remarks" rows="10" placeholder="Remarks">{{$addremarks->Remarks}}</textarea>
</div>
</div>
<input type="submit" name="submit" value="Update">
</div>
</div>
</form>

laravel 5.1 form and routing issue

Hi i am trying to get the output from my form to save and even when i try to echo(print_r) the output it but it simply goes to the post request and does not echo any output.
my route looks like:
Route::controller('stove', 'StoveController', [
'anyData' => 'stove.data',
'getIndex' => 'stove',
]);
Route::get('newstove', 'StoveController#addData');
Route::post('newstove', 'StoveController#store');
my controller:
public function addData()
{
return view('stoves.new');
}
public function store()
{
$input = Request::all();
Stove::create($input);
return redirect('stove');
}
and finally my form is
<form class="form-horizontal" action="/stove">
<fieldset>
<div class="control-group">
<label class="control-label" for="stoveno">Stove Number</label>
<div class="controls">
<input type="text" class="span4" id="stoveno" value="CP001000">
</div> <!-- /controls -->
</div> <!-- /control-group -->
<div class="control-group">
<label class="control-label" for="refno">Ref Number</label>
<div class="controls">
<input type="text" class="span4" id="refno" value="cff001">
</div> <!-- /controls -->
</div> <!-- /control-group -->
<div class="control-group">
<label class="control-label" for="manufacturedate">Manufacture Date</label>
<div class="controls">
<input type="date" class="span4" id="manufacturedate">
</div> <!-- /controls -->
</div> <!-- /control-group-->
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
<button class="btn">Cancel</button>
</div> <!-- /form-actions -->
</fieldset>
</form>
Thanks
Change the first line of your form to the following..
<form class="form-horizontal" action="/newstove" method="post">
This should submit your form via the POST method to the last route in your routes file.
From what I can see the first part of your routes file is not required...
Route::controller('stove', 'StoveController', [
'anyData' => 'stove.data',
'getIndex' => 'stove',
]);
Route::post('newstove', 'StoveController#store');
You need to add method="post" to the form. and change the action="/stove" to action="/newstove"
Also, isn't it Route::resource for adding controllers to the route list?

Categories