Laravel record display from database - php

I have this leftJoin in my Laravel project
products = DB::table('products')
->leftJoin('destinations','products.id','=','destinations.product_id')
->get();
Tables:
Product: id,name Destinations:id,product_id,destination,quantity,target_person
but i don't knwo how to display leftJoin,(i just want to display name form product and quantity,destination ,target_person from destinations)
and i want to add new record
This is my form :
<form action="." method="POST" id="formid">
{{ csrf_field() }}
<div class="form-group">
<label for="first_name">Product name</label>
<input class="form-control" name="name" >
</div>
<div class="form-group">
<label for="last_name">Quantity</label>
<input class="form-control" name='quantity'>
</div>
<div class="form-group">
<label for="last_name">Destination<label>
<input class="form-control" name='Destination'>
</div>
<div class="form-group">
<label for="last_name">Target Perosn</label>
<input class="form-control" name='target_person'>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
i never do this,please help me this,im beginner in this.

As you have mentioned, I just want to display name from product and quantity,destination ,target_person from destinations. You have to use table alias like:
products = DB::table('products as t1')
->leftJoin('destinations as t2','t1.id','=','t2.product_id')
->select('t1.name', 't2.quantity', 't2.destination', 't2.target_person')
->get();

Related

Can't update data in Laravel 8

Currently, I am working on a blog. But when I try to update a post, it's not getting updated and no error is reproduced.
What's wrong with the code? Anyone, please give me some suggestions to improvise my code. I want to understand what's the problem. So, anybody can explain in detail then I will really appreciate this thing.
PostController code:-
public function update(Store $request,Post $post)
{
$data = $request->validated();
$post = $post->update($data);
return redirect('/index')->withMessage('successfully published');
}
View page code:-
<form method="POST" action='/update'>
#csrf
<table class="table table table-striped table">
<div class="form-group">
<label for="name" class="col-form-label text-md-right">Title</label><br>
<input required="required" placeholder="Enter title here" class="" type="text" name="title" class="" value="{{$data->title}}" required/>
</div>
<div class="form-group">
<label for="name" class="col-form-label text-md-right">Description</label>
</div>
<div>
<textarea name="description" class="" required>{{$data->description}}
</textarea></div>
<div>
<input type="submit" name="update" class="btn btn-success" value="Update" required />
</div>
</table>
</form>
Route file code :-
Route::get('posts/edit/{edit}',[PostController::class,'edit']);
Route::post('/update',[PostController::class,'update']);
Change your Route update to this :
Route::post('posts/update',['as'=>'update','uses'=>'PostController#update']);
And in View Page
<form method="POST" action="{{ route('update') }}">

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

Data from JSON response not displaying after assigning to instance variable

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.

Add multiple rows with relation many-to-many

I'm trying to save a multiple database records with image name and thumb(for now only text inputs). Every image has also a category/categories (made by multiselect and pivot table between image and categories with many to many relation). The question is: how to sync/attach those categories? I do not want to use foreach ::create, because it will generate unnecessary database requests. For now I have something like this:
my blade file (part in form)
<form action="/panel/images" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="row[1][title]">Image title</label>
<input type="text" name="row[1][title]" class="form-control" />
</div>
<div class="form-group">
<label for="row[1][thumb]">Thumb</label>
<input type="text" name="row[1][thumb]" class="form-control" />
</div>
<!-- date because insert methode don't create timestamps autimatically-->
<input type="hidden" name="row[1][created_at]" value="{{ $date }}" />
<input type="hidden" name="row[1][updated_at]" value="{{ $date }}" />
<div class="form-group">
<label for="multi[0][]">Categories</label>
<select name="multi[0][]" multiple>
#include('layouts.images_categories_select')
</select>
</div>
<div class="form-group">
<label for="row[2][title]">Image title</label>
<input type="text" name="row[2][title]" class="form-control" />
</div>
<div class="form-group">
<label for="row[2][thumb]">Thumb</label>
<input type="text" name="row[2][thumb]" class="form-control" />
</div>
<div class="form-group">
<label for="multi[1][]">Categories</label>
<select name="multi[1][]" multiple>
#include('layouts.images_categories_select')
</select>
</div>
<input type="text" name="row[2][created_at]" value="{{ $date }}" />
<input type="text" name="row[2][updated_at]" value="{{ $date }}" />
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
And the controller
public function store(Request $request)
{
$image = new Image;
$inputs = Input::get('row');
$multiSelect = Input::get('multi');
$image::insert($inputs);
$image->images_catetories()->sync($multiSelect);
return redirect()->route('newImage')->with('status', 'images added');
}
EDIT:
Right now it throws an error "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into image_images_category (0, image_id, images_category_id, 1) values (18, , 1, 19))"
where "image_images_category" is my pivot table and values 18, 1, 19 are from multiselect.

No query results for model [App\WhatTodoModel]

I am newbie in laravel and I try to insert a data form a form having foreign key by using hide such as the code is mention below:-
<form class="form-horizontal" role="form" action="/WhatTodo/store" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="task_id" value=" {{$what->task_id}}">
<input type="hidden" name="work_id" value="{{$what->work_id}}">
<div class="form-group">
<label class="control-label col-sm-2" for="name"> Name</label>
<div class="col-sm-5">
{!!Form::select('name',$name)!!}
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="work">work:</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="work" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" value="Submit">
</div>
</div>
</form>
I have the controller with function:-
public function create($id)
{
$what=WhatTodoModel::findorFail($id);
$name=WOrk::lists('name','name');
return view('what/create',compact('what','name'));
}
You haven't really told us what your issue is or what error you're getting, but my guess given the current question is:
Assuming you're trying to implement a resource route and resourceful controller, the create method is used to show a form to create a new object, not edit an existing one. The create method does not take any parameters, therefore $id will be blank and WhatTodoModel::findorFail($id); will throw an exception.
If you want to edit an existing record, you do that using the edit action.
For creating a new record, create shows the form, store saves the record.
For editing an existing record, edit shows the form, update saves the record.

Categories