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
Related
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
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>
So, I have my SPA at about 98% functional. The app pulls data from a MySQL database and allows the user to edit/delete records. For the page in question, it will edit/delete data of the specified student ID but it will not properly display the data in the text fields.
If you do not input a value it will display the first item in the JSON array but not the one you specify in the search field.
I did not do a very good job at explaining that but here is the HTML page that uses a function to pass data to the controller which then selects the corresponding student by ID and assigns it to the variable $scope.student. I then try to display the student data on the HTML page by using student.first_name (or any property) but it does not work correctly.
<h3>Edit/Delete Student with ID: {{student.student_id}}</h3>
<div class="form-group">
<label for="sid">Student ID:</label>
<input type="text" class="form-control" id="sid" ng-model="sid">
</div>
<p><button type="button" class="btn btn-primary" ng-click="getRecord(sid)">
Get Student Info </button> </p>
<div class='row'>
<div class="col-md-6">
<div class="form-group">
<label for="first_name">First Name:</label>
<input type="text" class="form-control" id="first_name" ng-model="student.first_name">
</div>
<div class="form-group">
<label for="last_name">Last Name:</label>
<input type="text" class="form-control" id="last_name" ng-model="student.last_name">
</div>
<div class="form-group">
<label for="hrs_completed">Hrs Completed:</label>
<input type="text" class="form-control" id="hrs_completed" ng-model= "student.hrs_completed">
</div>
<div class="form-group">
<label for="hrs_attempted">Hrs Attempted:</label>
<input type="text" class="form-control" id="hrs_attempted" ng-model= "student.hrs_attempted">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="gpa_points">GPA Points:</label>
<input type="text" class="form-control" id="gpa_points" ng-model= "student.gpa_points">
</div>
<div class="form-group">
<label for="major">Major:</label>
<input type="text" class="form-control" id="major" ng-model="student.major">
</div>
<div class="form-group">
<label for="advisor_id">Advisor ID:</label>
<input type="text" class="form-control" id="advisor_id" ng-model="student.advisor_id">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" id="email" ng-model="student.email">
</div>
</div>
<button type="button" class="btn btn-primary" ng-click="updateRecord()">Update</button>
<button type="button" class="btn btn-primary" ng-click="deleteRecord()">Delete</button>
And here is my controller on my Javascript page:
app.controller('editCtrl', function($scope, $http) {
$http.get("getStudentData.php")
.then(function (response) {
$scope.students = response.data;
});
$scope.getRecord = function(sid) {
id = sid;
$scope.student = $scope.students.find(s=>s.id == sid);
};
Do I need to make a seperate GET request to the server for this to work properly or do I just need to reference the student object differently?
I think there is mismatch in what you are getting as response.data and what you are trying to .find() and then what you are binding on html.
Check this plunkr.
You are trying to render by searching id property.
$scope.students.find(s=>s.id == sid);
where as you are rendering on UI using student_id property.
{{student.student_id}}
So, surely there is mismatch in what you are getting as response.data and what you are rendering using $scope.student properties. I think my plunkr will show that your function is correct.
In case this answer doesn't work, share your response.data.
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();
I do have three fields which are text input which is of type number.these three data are being saved with different ids but on the same save button.i.e i am inserting data into all the field and then clicking on the save button but the my first data is being saved by different id and the same for the rest of two data.for example if i put 1 in first input,2 in second and 3 in the next since these are number type.but 1 is being saved by say by id:11,2 is being saved by say by id:12,3 is being saved by say by id:13.
my view is like below:
<form class="form">
<div class="control-group">
<label class="control-label">High Priority</label>
<div class="controls">
<input type="number"name="sval"id="sval"/>Days
</div>
</div>
<div class="control-group">
<label class="control-label ">Low Priority</label>
<div class="controls">
<input type="number"name="sval" id="sval"/>Days</div>
</div>
<div class="control-group">
<label class="control-label ">Normal</label>
<div class="controls">
<input type="number" name="sval" id="sval"/>Days </div>
</div>
<button id="btn" class="btn btn-primary insert">Save</button>
</form>
and the queries are:
var sqld:String;
sqld = "Delete from app_settings where kunnr = '"+kunnr+"' and ( skey = 'hp_days' or skey = 'np_days' or skey = 'lp_days' )";
wcisql.query(sqld,'clearRDays');
}
public function saveRDays():void{
var sqlu:String;
sqlu = "Insert into app_settings(kunnr,skey,sval) Values ('"+kunnr+"','hp_days','"+hp_days.value+"')";
wcisql.query(sqlu,'saveRDays');
sqlu = "Insert into app_settings(kunnr,skey,sval) Values ('"+kunnr+"','np_days','"+np_days.value+"')";
wcisql.query(sqlu,'saveRDays');
sqlu = "Insert into app_settings(kunnr,skey,sval) Values ('"+kunnr+"','lp_days','"+lp_days.value+"')";
wcisql.query(sqlu,'saveRDays');
$sql = "Select * from app_settings where kunnr = '$kunnr'";
}
i am adding image as well so that u can get better idea:
how to do this please suggest..
and for my view:
use Multiple Insert statement
ckeck out this referenced link:
Multiple INSERT statements vs. single INSERT with multiple VALUES
&
http://www.techonthenet.com/sql/insert.php
The problem is you can only use one ID for each HTML element.
So there are two ways to achieve this, one is dynamic the second is static.
Static Solutions:
<div class="control-group">
<label class="control-label">High Priority</label>
<div class="controls">
<input type="number"name="sval1"id="sval1"/>Days
</div>
</div>
<div class="control-group">
<label class="control-label ">Low Priority</label>
<div class="controls">
<input type="number"name="sval2" id="sval2"/>Days</div>
</div>
<div class="control-group">
<label class="control-label ">Normal</label>
<div class="controls">
<input type="number" name="sval3" id="sval3"/>Days </div>
</div>
<button id="btn" class="btn btn-primary insert">Save</button>
Dynamic Solution
<div class="control-group">
<label class="control-label">High Priority</label>
<div class="controls">
<input type="number" name="sval[]"/>Days
</div>
</div>
<div class="control-group">
<label class="control-label ">Low Priority</label>
<div class="controls">
<input type="number" name="sval[]"/>Days</div>
</div>
<div class="control-group">
<label class="control-label ">Normal</label>
<div class="controls">
<input type="number" name="sval[]"/>Days </div>
</div>
<button id="btn" class="btn btn-primary insert">Save</button>
Now when you post the form the items will come in array so you have an array
$_GET['sval'] or $_POST['sval'] contains array(1,2,3)