How to check the data already exists or not laravel - laravel

I have 2 tables. Patient table and reservation table.
Patient table:
ID
name
medical record number (norekmed)
Table of reservations
ID
idpatient
idroom
How do I check patient data already or not if I make a reservation?
Checking is by comparing between a reservation form field with norekmed in the patient table.
If patient data already exists, we can make a reservation. And if there is no patient data, we cannot make a reservation.
If it turns out this is not good, and there is a better method, I accept that.
Reservation controller (store)
$this->validate($request,
[
'idpatient' => 'required|unique:reservation,idpatient',
'idroom' => 'required',
]);
Patient::where(function($query) {
$query->has('id')
->orHas('norekmed');
})->find(1);
$reservasi = new Reservasi();
$reservasi->idpatient = $request->idpatient;
$reservasi->idroom = $request->idroom;
$reservasi->save();

A simple solution could be to use firstOrCreate and let the validator check if the patient id and room id exist. If I would have done this I would do the following:
$this->validate($request, [
// This will check if the patient exists.
'idpatient' => 'required|exists:patients,id',
// This will check if the room exists.
'idroom' => 'required|exists:room,id',
]);
// Get the reservation or create one if it does not exist.
$reservasi = Reservasi::firstOrCreate([
'idpatient' => $request->idpatient,
'idroom' => $request->idroom,
]);
...

Related

After Update, The data from another Table will be deleted

I Have two different database table named Customers and Change_info_request
customers will request of changing their infomation and it will stored in Change_info_request and me the admin will be the one who will approve for changing information.
after the update the information inside the Change_info_request will transfer to the Customers table and the data inside the Change_info_request will be deleted .
And that's is my problem, i want to do it in the same controller like this
UpdateRequest
public function UpdateRequest(ClientUpdate $request){
$res = Customer::where('id', $request->id)
->update([
'first_name' => $request->new_first_name,
'last_name' => $request->new_last_name,
'birthdate' => $request->new_birthdate,
'gender' => $request->new_gender,
'pending_update_info' => 2,
]);
if($res) {
return response()->wrap($res);
} else {
return response()->wrap(["message" => "something went wrong!"]);
}
}
How will i delete the info from Change_info_request inside this controller ?
Sorry for the wrong grammar and im only a student that studying laravel.
you have id of that row in Change_info_request table right?
i have done this before this was my way:
i store new information in my table called edit-user (just like your Change_info_request ) and when i show them in admin dashboard i have their id!
when admin click on approve button it will send the id of that row to some function!
in function i find the row in edit-user table with id that i have, and replace my new data in my users table (your Customers table) and when i finished replacing data i will delete data from edit-user with that id!
Change_info_request::find('customer_id', $id)->delete();
with id of that row in edit-user(your Change_info_request table) table you can do anything
and of course your in Change_info_request table you should have user_id field
good luck!
Before your if condition, you can add something like:
$del = Change_info_request::find('customer_id', $request->id)->delete();

Laravel unique validation on update not working by passing id in validation

In my controller i am inserting data in three different table at a time.
I have put validation before inserting in some of the unique fields. But at the time of Update it says email already exists. I have searched a lot and got a solution which is working for everyone except for me by passing id in validation rule.
I have tried
$this->validate($request,[
'admission_no'=>"required|unique:students,admission_no,$id",
'student_email=>"required|unique:students,student_email,$id",
'guardian_email'=>"required|unique:student_parents,guardian_email,$id"
]);
$student = Student::find($id);
$student->admission_no = $request->admission_no;
$student->student_email = $request->student_email;
$student->save();
//parent model
$parent = StudentParent::where('student_id',$student->id)->first();
$parent->guardian_email = $request->guardian_email;
$parent->save();
It doesn't work this way i dont know why
also the main problem is StudentParent model is different how to pass id in validation for this model to unique update
Your students and student_parents table has different $id, So you need to pass student_parents table id on guardian_email validation :
$validatedData = $request->validate([
'admission_no'=>"required|unique:students, admission_no, $id", // expect students table id
'student_email'=>"required|unique:students, student_email, $id", // expect students table id
'guardian_email'=>"required|unique:student_parents, guardian_email, $student_parents_id" // pass student_parents table id here
]);
Hope this helps

Validate multiple fields to be unique in Laravel

My table is named Cars with 2 fields (id, matriculation), how to adapt my syntax below?
'input_field' => 'unique:<table name>,<column name for this input field>, <unique id>, <unique id column in table>';
I have tried this: ???
'matriculation' => 'required|unique:cars,matriculation, uniqueID ???? , unique id colum in table ????
Do know you what is the unique id and unique id column ?
Thank you for your help.
If you want to validate the matriculation upon update to be unique but keep the id intact, force ignoring it with a custom Validator rule
$car = Get_The_Car_Here; // Here assign the car to a variable
Validator::make($data, [
'marticulation' => [
'required',
\Illuminate\Validation\Rule::unique('cars')->ignore($car->id),
],
]);
Hope this helps

How to get hasMany relation in laravel?

Pls bear with me . I am working on api with laravel :
The Idea is that I have table called ( cards ) this table contain column called ( service_id ) .
this column has relation with table ( services ).
This is cards database structure in image :
image of database
All thing is good with add one service_id , but now I need to make table cards hasMany services
So How can I do it , and the database structure what will be?
this is my old code to add new card :
public function store(Request $request)
{
$validator = \Validator::make($request->all(), [
'user_id' => 'required|unique:cards|exists:users,id',
'service_id' => 'required',
'numPhone' => 'required',
'location' => 'required',
],[],[
'user_id' => '( User id )',
'service_id' => 'service id',
'numPhone' => 'Phone Number',
]);
if ($validator->fails()){
return $this->apiRespone(null,$validator->errors()->all(),'Some input required');
}
$card = Card::create($request->all());
return $this->apiRespone(new cardResource($card),'Add card Successfully',201);
}
I think you need to create pivot table "cards_services" that has column id, card_id, service_id column and used relationship Sync() method of laravel to store/update.
In cards modal
public function services(){
return $this->belongsToMany('App\Models\Service','cards_services', 'card_id', 'service_id');
}
For save it.
$cards->services()->sync([1,2]); //1 and 2 is service ID and $cards is card modal object
here you have service_id in your cards table
so i think it will be easier to implement service hasMany cards and cards belongsTo service
add this in your service model
public function cards(){return $this->hasMany('App\Cards');}
add this in your cards model
public function service(){return $this->belongsTo('App\Service');}
Note: please rewrite path and Model name according to your requirement

laravel backpack crud display relationship in list view filter by and update

How can you display relationships fields and filter by in the list and update views?
lets say I have a transactions table
Id, customer_id, product_id, purchase_date, payment_status
a customer table
Id, first_name, last_name, email, password
a products table
Id, product_name, product_description, product_price, stock
for example. And I wanted to show in my transactions view:
ID, customer.first_name customer.last_name, product.product_name, purchase_date, payment_status
and wanted to filter the results by customer name and product name, I have read through the docs and cannot see how to achieve this.
In your CrudController in the setup() method :
$this->crud->setListView('your-custom-view');
If you want, you can use the default list view as a starter and modify it
you can find the default file in vendor/backpack/crud/src/resources/views/list.blade.php.
Don't modify it there because it will show on all Crud views..just take it as a template and save 'your-custom-view' in views.
Maybe this will be of help : https://laravel-backpack.readme.io/v3.3/docs/filters
Edit:
Instead of customer_id you want the customer first_name and so on right? This will do the trick:
In your TransactionCrudController:
$this->crud->addColumn([
// 1-n relationship
'label' => "customer", // Table column heading
'type' => "select",
'name' => 'customer_id', // the column that contains the ID of that connected entity;
'entity' => 'customer', // the method that defines the relationship in your Model
'attribute' => "first_name", // foreign key attribute that is shown to user
'model' => "App/Models/Customer", // foreign key model
]);
Is this what you are looking for? If not let me know.

Resources