Data isn't inserting into different tables from one form - laravel

I'm trying to submit data from one form into two different tables, but it is not submitting, my form contains on two parts one is model, i.e., when modal form submit then its data saves into one table and rest of other data into another table.
Controller
public function store(Request $request)
{
$request->validate([
'patient_fname' => 'required',
'patient_lname' => 'required',
'patient_email' => 'required',
'patient_dob' => 'required',
'patient_guardian' => 'required',
'patient_gender' => 'required',
'user_id' => 'required',
'patient_name' => 'required',
'patient_insurance' => 'required',
'patient_insurance_id' => 'required',
'patient_reason' => 'required',
'patient_new' => 'required',
'patient_contact_no' => 'required',
'patient_message' => 'required',
]);
$userdata = Userdata::create([
Input::get('patient_fname'),
Input::get('patient_lname'),
Input::get('patient_email'),
Input::get('patient_dob'),
Input::get('patient_guardian'),
Input::get('patient_gender')
]);
$form = Form::create([
Input::get('patient_name'),
Input::get('patient_insurance'),
Input::get('patient_insurance_id'),
Input::get('patient_reason'),
Input::get('patient_new'),
Input::get('patient_contact_no'),
Input::get('patient_message')
]);
return back();
}
form.php
protected $guarded = [];
protected $table = "forms";
protected $fillable=['patient_name',
'patient_insurance','patient_insurance_id', 'patient_reason',
'patient_new', 'patient_message'];
public function userdata()
{
return $this->belongsTo('App\Userdata');
}
userdata.php
protected $fillable = ['patient_fname', 'patient_lname',
'patient_email', 'patient_dob', 'patient_guardian',
'patient_gender','user_id'];
public function form()
{
return $this->hasMany('App\Form');
}

I would imagine this is because you're not supplying a key for the values in your create method.
Also, validate will return an array of the validated properties so you can just use that instead of using Input::get():
public function store(Request $request)
{
$data = $request->validate([
'patient_fname' => 'required',
'patient_lname' => 'required',
'patient_email' => 'required',
'patient_dob' => 'required',
'patient_guardian' => 'required',
'patient_gender' => 'required',
'user_id' => 'required',
'patient_name' => 'required',
'patient_insurance' => 'required',
'patient_insurance_id' => 'required',
'patient_reason' => 'required',
'patient_new' => 'required',
'patient_contact_no' => 'required',
'patient_message' => 'required',
]);
$userdata = Userdata::create([
'patient_fname' => $data['patient_fname'],
'patient_lname' => $data['patient_lname'],
'patient_email' => $data['patient_email'],
'patient_dob' => $data['patient_dob'],
'patient_guardian' => $data['patient_guardian'],
'patient_gender' => $data['patient_gender'],
]);
$form = Form::create([
'patient_name' => $data['patient_name'],
'patient_insurance' => $data['patient_insurance'],
'patient_insurance_id' => $data['patient_insurance_id'],
'patient_reason' => $data['patient_reason'],
'patient_new' => $data['patient_new'],
'patient_contact_no' => $data['patient_contact_no'],
'patient_message' => $data['patient_message'],
]);
return back();
}

Related

Laraval array validation with unique email while update

Updating form but email should be unique, request('contacts.*.id') is empty. It should have ID of that row. How can i get that ID?
return [
'email' => "required|email|unique:clients,email,".request('id'),
'client_type_text' => 'required',
'client_name' => 'required',
'phone_number' => 'required',
'contacts' => 'required|array',
'contacts.*.firstname' => 'required',
'contacts.*.lastname' => 'required',
'contacts.*.mobile' => 'required',
'contacts.*.email' => 'required|email|unique:users,email,'.request('contacts.*.id'),
];
It should work
return [
'email' => "required|email|unique:clients,email,".request('id'),
'client_type_text' => 'required',
'client_name' => 'required',
'phone_number' => 'required',
'contacts' => 'required|array',
'contacts.*.firstname' => 'required',
'contacts.*.lastname' => 'required',
'contacts.*.mobile' => 'required',
// check if email is unique for each contact id
'contacts.*.email' => [
'required',
'email',
function ($attribute, $value, $fail) {
$contact_id = explode('.', $attribute)[1];
$client_id = request('id');
$contact = \App\Models\Contact::where('id', $contact_id)->where('client_id', $client_id)->first();
if ($contact) {
$contact_email = $contact->email;
if ($contact_email != $value) {
$contact_with_email = \App\Models\Contact::where('email', $value)->first();
if ($contact_with_email) {
$fail('The email has already been taken.');
}
}
}
},
],
];

Route [dealer] not defined. in laravel?

I defined my route but it is not showing that Route [dealer] not defined.
Route::resource('/dealer', DealerController::class);
This is my controller where there is index, create and store method is in same page.
public function index()
{
$users = User::all();
return view('dealer', compact('users'));
}
public function create()
{
$dealers = Dealer::all();
return view('dealer', compact('dealers'));
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
'name_of_firm' => 'required',
'address' => 'required',
'number' => 'required',
]);
$user = User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password')),
'name_of_firm' => $request->input('name_of_firm'),
'address' => $request->input('address'),
'number' => $request->input('number'),
]);
return redirect()->route('dealer')->withSuccess('done');
}
https://laravel.com/docs/8.x/controllers#actions-handled-by-resource-controller
Look at the example from the documentation. There exists no such route in your ressource controller.
Depending on what you want you either have to use dealer.index, dealer.show or dealer.edit
Normally you would also use the plural form and not the singular form of the word.

User create and update using updateOrCreate()

I am creating and updating user using below code.
public function store(Request $request)
{
if ($request->ajax()) {
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users,email,' .$request->user_id,
'password' => 'required',
'role' => 'required',
]);
$user = User::updateOrCreate(['id' => $request->user_id],
[
'name' => $request->name,
'email' => $request->email,
'role' => $request->role,
'password' => Hash::make($request->password)
]);
}
}
When I am updating record Password is updating also. How can I solve the issue ?
How validation is working here 'email' => 'required|email|unique:users,email,' .$request->user_id, when I am creating a record ? Because when I am creating a record at that time $request->user_id is not available.

parse_url() expects parameter 1 to be string, array given when creating user laravel

Im building my onw registration in laravel and when im trying to hash my password i get the error parse_url() expects parameter 1 to be string, array given
//Controller
HomeController.php
$filteredValidation = $request->except('_token');
$password = Hash::make($filteredValidation['password']);
UserRegistrationRequest::create([
'firstname' => $filteredValidation['firstname'],
'lastname' => $filteredValidation['lastname'],
'email' => $filteredValidation['email'],
'year' => $filteredValidation['year'],
'avatar' => $filteredValidation['firstname'],
'buddy' => $filteredValidation['firstname'],
'password' => $password,
]);
//request
UserRegistrationRequest.php
public function rules()
{
return [
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required',
'year' => 'required',
'password' => 'required',
];
}
I have no idea why this is happening

Is there anyway group the errors of multiple form validations in laravel

In the given scenario
request()->validate([
'type' => 'required',
'category' => 'required'
]);
and Again
request()->validate([
'name' => 'required',
'gender' => 'required
]);
Is it possible to get some sort of centralized or complied error that encompasses both the validations?
Then you should use Validator facade to handle this kind of cases.
for ex.
$validator = Validator::make($request->only('type', 'category), [
'type' => 'required',
'category' => 'required'
]);
$validator2 = Validator::make($request->only('name', 'gender'), [
'name' => 'required',
'gender' => 'required'
]);
if ($validator->fails() || $validator2->fails()) {
// return merge $validator->errors() and $validator2->errors();
}

Resources