Laravel Reservation Form not Mailing - php

I have a 'contact' and 'reservation' form on a client site. The 'contact' form works well and when filled out, it sends the email to my mailtrap.io account. As far as I can see the structure of my 'reservation' form and controller is exactly the same as that of the 'contact form', however, no emails are sent out when the 'reservation' form is filled out. I can't figure out why.
When I dd the data on both the 'contact' and 'reservation' forms, I can see the array results. However, as mentioned, the emails for the 'reservation' form is not being sent to mailtrap.io.
Here's my web.php file
Route::get('reservation', 'PageController#reservation');
Route::post('reservation', 'PageController#postReservation');
Route::get('contact', 'PageController#contact');
Route::post('contact', 'PageController#postContact');
Here's my Controller for both the 'contact' and 'reservation' form. The contact and reservation functions are in the same controller:
public function reservation()
{
return view('reservation');
}
public function postReservation(Request $request)
{
dd($request->all()); //-> Fill in data to test if form works
$request->validate([
'name' => 'required|string|min:2',
'email' => 'required|email',
'phone' => 'required|digits:10',
'date' => 'required|date_format:Y/m/d|after:today',
'seats' => 'required|integer',
'message' => 'min:10'
]);
$data = array (
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'date' => $request->date,
'time' => $request->time,
'seats' => $request->seats,
'reservationMessage' => $request->message,
);
Mail::send('emails.reservation', $data, function ($message) use ($data) {
$message->from('contact#venueat50.co.za');
$message->to('info#webdevpro.co.za');
$message->subject($data['name']);
});
return redirect('reservation')->with('success', 'Thank you. We will contact you to confirm the booking');
}
public function contact()
{
return view ('contact');
}
public function postContact(Request $request)
{
dd($request->all()); //-> Fill in data to test if form works
$request->validate([
'name' => 'required|string|min:2',
'email' => 'required|email',
'number' => 'required|digits:10',
'subject' => 'required|min:3',
'message' => 'required|min:10'
]);
$data = array(
'name' => $request->name,
'email' => $request->email,
'number' => $request->number,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from('contact#venueat50.co.za');
$message->to('info#webdevpro.co.za');
$message->subject($data['subject']);
});
return redirect('contact')->with('success', 'Your message has been sent. We will reply promptly');
}
I also have an email template setup for both the 'contact' and 'reservation' form. Not sure if that is needed.
Let me also attach the 'contact' and reservation forms:
Contact Form:
<form method="post" id="contact-form" action='{{ action('PageController#postContact') }}'>
{{ csrf_field() }}
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
<label>Name</label>
#if($errors->has('name'))
<small class="form-text invalid-feedback">{{ $errors->first('name') }}</small>
#endif
<p><input type="text" name="name" class="reservation-fields" value="{{ old('name') }}"/></p>
<label>Email</label>
#if($errors->has('email'))
<small class="form-text invalid-feedback">{{ $errors->first('email') }}</small>
#endif
<p><input type="text" name="email" class="reservation-fields" value="{{ old('email') }}"/></p>
<label>Mobile Nr</label>
#if($errors->has('number'))
<small class="form-text invalid-feedback">{{ $errors->first('number') }}</small>
#endif
<p><input type="text" name="number" class="reservation-fields" value="{{ old('number') }}"/></p>
<label>Subject</label>
#if($errors->has('subject'))
<small class="form-text invalid-feedback">{{ $errors->first('subject') }}</small>
#endif
<p><input type="text" name="subject" class="reservation-fields" value="{{ old('subject') }}"/></p>
<label>Message</label>
#if($errors->has('message'))
<small class="form-text invalid-feedback">{{ $errors->first('message') }}</small>
#endif
<p> <textarea name="message" id="msg-contact" class="reservation-fields" rows="7"></textarea></p>
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
<p class="contact-btn"><input type="submit" value="Send message" id="submit"/></p>
And Lastly the Reservation Form:
<form method="post" id="reservation-form" action={{ action('PageController#postReservation') }}>
{{ #csrf_field() }}
<div class="row">
<div class="col-md-4">
<label>Name*</label>
<p><input type="text" name="name" class="reservation-fields" required/></p>
</div>
<div class="col-md-4">
<label>Email*</label>
<p><input type="text" name="email" class="reservation-fields" required/></p>
</div>
<div class="col-md-4">
<label>Phone*</label>
<p><input type="text" name="phone" class="reservation-fields" required/></p>
</div>
</div>
<!--end row-->
<div class="row">
<div class="col-md-4">
<label>Date*</label>
<p><input type="date" name="datepicker" id="datepicker" class="reservation-fields" size="30" required/></p>
</div>
<div class="col-md-4">
<label>Time*</label>
<p>
<select name="time" class="reservation-fields" >
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
<option value="13:00">13:00</option>
<option value="14:00">14:00</option>
<option value="15:00">15:00</option>
<option value="16:00">16:00</option>
</select>
</p>
</div>
<div class="col-md-4">
<label>Seats*</label>
<p><input type="text" name="seats" class="reservation-fields" required/></p>
</div>
</div>
<!--end row-->
<label>Special Requests</label>
<p> <textarea name="message" id="message2" class="reservation-fields" cols="100" rows="4" tabindex="4"></textarea></p>
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
<p class="alignc"><input type="submit" value="Book Now" id="submit" /></p>

Related

Laravel update only refreshes a page

My update page isn't working, when I'm submitting a form it only refreshes a page and adds some stuff to my URL, for example: http://127.0.0.1:8000/admin/employees/102/edit?_method=PUT&_token=mj3lrHqoYm1wiLwWiNQ8OX0kNRjmW4QVRuLpgZxY&image_path=&name=Libbie+Ebert&phone_number=380324244497&email=brekke.lola%40example.org&position_id=7&payment=186799&head=Asia+Frami&recruitment_date=2022-01-10. I already cleared route cache and its still not working, and also tried to use dd function but it's not showing up. What can be wrong? My model:
class Employee extends Model
{
use HasFactory;
protected $casts = [
'recruitment_date' => 'datetime:d-m-y',
];
protected $fillable= ['name', 'position_id', 'phone_number',
'recruitment_date', 'email', 'head',
'image_path', 'payment', 'admin_created_id', 'admin_updated_id'];
public function position()
{
return $this->belongsTo(Position::class, 'position_id');
}
}
Controller function:
public function update(StoreEmployeeRequest $request, $id){
$input = $request->validated();
$employee = Employee::findOrFail($id);
$employee->update([
'name' => $request->input('name'),
'position_id' => $request->input('position_id'),
'phone_number' => '+' . $request->input('phone_number'),
'recruitment_date' => $request->input('recruitment_date'),
'email' => $request->input('email'),
'head' => $request->input('head'),
'image_path' => $input['image_path'],
'payment' => number_format($request->input('payment')),
'admin_updated_id' => auth()->id(),
]);
return to_route('admin.employees.index');
}
Request:
public function rules()
{
return [
'name' => 'required|min:2|max:255',
'recruitment_date' => 'required|date',
'phone_number' => 'required',
'email' => 'required|email',
'payment' => 'required|numeric',
'position_id' => 'required',
'image_path' => 'image|mimes:jpg,png|max:5000|dimensions:min_width=300,min_height=300'
];
}
View:
<form action="{{ route('admin.employees.update', ['id' => $employee->id]) }}" enctype="multipart/form-data">
#method('PUT')
#csrf
<label for="image_path" class="form-label">Photo</label>
<input type="file" name="image_path" class="form-control-file" >
#error('image_path')
<div class="alert alert-danger mt-2" role="alert"><strong>{{ $message }}</strong></div>
#enderror
<label for="name" class="form-label">Name</label>
<input type="text" name="name" class="form-control" value="{{ $employee->name }}" placeholder="Name is...">
#error('name')
<div class="alert alert-danger mt-2" role="alert"><strong>{{ $message }}</strong></div>
#enderror
<label for="phone_number" class="form-label">Phone</label>
<input type="text" name="phone_number" class="form-control" value="{{ old('phone_number', preg_replace('/[^0-9]/', '', $employee->phone_number)) }}" placeholder="380...">
#error('phone_number')
<div class="alert alert-danger mt-2" role="alert"><strong>{{ $message }}</strong></div>
#enderror
<label for="email" class="form-label">Email</label>
<input type="email" name="email" class="form-control" value="{{ old('email', $employee->email) }}" placeholder="Email is...">
#error('email')
<div class="alert alert-danger mt-2" role="alert"><strong>{{ $message }}</strong></div>
#enderror
<label for="position_id" class="form-label">Position</label>
<select class="form-control" name="position_id" aria-label=".form-select-lg example">
#foreach ($positions as $position)
<option value="{{ $position->id }}" {{$employee->position_id == $position->id ? 'selected' : ''}}>{{ $position->name }}</option>
#endforeach
</select>
#error('position_id')
<div class="alert alert-danger mt-2" role="alert"><strong>{{ $message }}</strong></div>
#enderror
<label for="payment" class="form-label">Salary, $</label>
<input type="text" name="payment" class="form-control" value="{{ old('payment', preg_replace('/[^0-9]/', '', $employee->payment)) }}" placeholder="Salary is...">
#error('payment')
<div class="alert alert-danger mt-2" role="alert"><strong>{{ $message }}</strong></div>
#enderror
<label for="head" class="form-label">Head</label>
<input type="text" id="head" name="head" class="form-control" value="{{ old('head', $employee->head) }}" placeholder="Head is...">
#error('head')
<div class="alert alert-danger mt-2" role="alert"><strong>{{ $message }}</strong></div>
#enderror
<label for="recruitment_date" class="form-label">Date</label>
<input type="datetime-local" name="recruitment_date" value="{{ old('recruitment_date', $employee->recruitment_date) }}" class="form-control">
#error('recruitment_date')
<div class="alert alert-danger mt-2" role="alert"><strong>{{ $message }}</strong></div>
#enderror
<div class="mt-4 text-center">
<button type="submit" class="btn btn-secondary">Submit</button>
</div>
</form>
Route:
Route::put('/admin/employees/{id}/edit', [EmployeeController::class, 'update'])->name('admin.employees.update');
You are missing the method attribute in your form, and the default one is GET. Adding #method('PUT') is not enough.
Change your code like this:
<form
action="{{ route('admin.employees.update', ['id' => $employee->id]) }}"
method="POST"
enctype="multipart/form-data">
i think you should you POST method while submitting the form.

How do I keep the value of a checkbox in an invalid form in this Laravel 8 application?

I am working on a Laravel 8 blogging application. The "Add article" form has a "switch" (checkbox) that lets the user choose whether or not the post will be a featured one.
The form:
<form method="POST" action="{{ route('dashboard.articles.add') }}" enctype="multipart/form-data" novalidate>
#csrf
<div class="col-md-12 #error('title') has-error #enderror">
<input id="title" type="text" placeholder="Title" class="form-control #error('title') is-invalid #enderror" name="title" value="{{ old('title') }}" autocomplete="title" autofocus>
#error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="row mb-2">
<label for="short_description" class="col-md-12">{{ __('Short description') }}</label>
<div class="col-md-12 #error('short_description') has-error #enderror">
<input id="short_description" type="text" placeholder="Short description" class="form-control #error('short_description') is-invalid #enderror" name="short_description" value="{{ old('short_description') }}" autocomplete="short_description" autofocus>
#error('short_description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<label for="category" class="col-md-12">{{ __('Category') }}</label>
<div class="col-md-12 #error('category_id') has-error #enderror">
<select name="category_id" id="category" class="form-control #error('category_id') is-invalid #enderror">
<option value="0">Pick a category</option>
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<div class="col-md-12 d-flex align-items-center switch-toggle">
<p class="mb-0 me-3">Featured article?</p>
<input id="featured" class="mt-1" type="checkbox" name="featured">
<label class="px-1" for="featured">{{ __('Toggle') }}</label>
</div>
</div>
<button type="submit" class="w-100 btn btn-primary">{{ __('Save') }}</button>
</form>
The controller:
class ArticleController extends Controller
{
private $rules = [
'category_id' => 'required|exists:article_categories,id',
'title' => 'required|string|max:190',
'short_description' => 'required|string|max:190',
];
private $messages = [
'category_id.required' => 'Please pick a category for the article',
'title.required' => 'Please provide a title for the article',
'short_description.required' => 'The article needs a short description',
'short_description.max' => 'The short description field is too long',
];
public function categories() {
return ArticleCategory::all();
}
public function create() {
// Load the view and populate the form with categories
return view('dashboard/add-article',
['categories' => $this->categories()]
);
}
public function save(Request $request) {
// Validate form (with custom messages)
$validator = Validator::make($request->all(), $this->rules, $this->messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator->errors())->withInput();
}
$fields = $validator->validated();
// Turn the 'featured' field value into a tiny integer
$fields['featured'] = $request->get('featured') == 'on' ? 1 : 0;
// Data to be added
$form_data = [
'user_id' => Auth::user()->id,
'category_id' => $fields['category_id'],
'title' => $fields['title'],
'slug' => Str::slug($fields['title'], '-'),
'short_description' => $fields['short_description'],
'featured' => $fields['featured'],
];
// Insert data in the 'articles' table
$query = Article::create($form_data);
if ($query) {
return redirect()->route('dashboard.articles')->with('success', 'The article titled "' . $form_data['title'] . '" was added');
} else {
return redirect()->back()->with('error', 'Adding article failed');
}
}
}
The problem
When an attempt is made to submit the invalid form, the fields that are valid keep their values.
The unintended exception to the rule is the checkbox <input id="featured" class="mt-1" type="checkbox" name="featured">
What is my mistake?
Your input has no value, so you can only check on existence not on value in controller ($request->get('featured') will never have the value on)
You need to use old('featured') on that field too
You can skip the check on the featured field in the controller if you set the values to 0 and 1 in the form
<div class="row mb-2">
<div class="col-md-12 d-flex align-items-center switch-toggle">
<p class="mb-0 me-3">Featured article?</p>
<input type="hidden" value="0" name="featured">
<input {{ old('featured') ? 'checked':'' }} id="featured" value="1" class="mt-1" type="checkbox" name="featured">
<label class="px-1" for="featured">{{ __('Toggle') }}</label>
</div>
</div>
The hidden field is to send the value 0 if the checkbox is not checked. A checkbox input is not sent in the request if it is not checked.
When checked, the input value will be overwritten to 1.
In the controller you can directly use the input
$fields = $validator->validated();
// Data to be added
$form_data = [
'user_id' => Auth::user()->id,
'category_id' => $fields['category_id'],
'title' => $fields['title'],
'slug' => Str::slug($fields['title'], '-'),
'short_description' => $fields['short_description'],
'featured' => $fields['featured'],
];

In Laravel ,how to validate all the input fields, display error messages if any, and return back to the form with the inputs if there are errors

I am trying to validate user registration form using Laravel Validator::make. The code works, including showing the error messages in the respective fields.
But my problems are :-
1) No matter what I type in the email field, it always produces an error. So, it does not redirects me to the next page, even if I type the correct format example#example.com . I tried removing the "email" validator and tried. It still shows me "email field is required" message
2) In addition to that,if there are any errors in one or more fields,the page gets redirected back but the input fields are not filled up with the past input .
Here is the Controller page
public function showdata(Request $request){
$first_name = $request->first_name;
$last_name = $request->last_name;
$email = $request->eid;
$password = $request->password;
$confirm_password = $request->confirm_password;
$mobno = $request->mobno;
$dob = $request->dob;
$gender = $request->gender;
$address = $request->address;
$country = $request->country;
$time = $request->dt;
$messages=
[
'required' => "The :attribute field is required",
'email.email' => "The :attribute :input format should be example#example.com/.in/.edu/.org....",
'email.unique' => "The :attribute :input is taken. Please use another email address",
'confirm_password.same' => "Password and Confirm password fields must match exactly",
'mobno.digits' => "The :attribute field accepts only numbers",
'mobno.digits:10' => "The :attribute should be 10 digits long",
'dob.date_format' => "The date format :input should be YYYY-MM-DD",
'address.string' => "The :attribute :input must be in the form of a string"
];
$rules = [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:8',
'confirm_password' => 'required|min:8|same:password',
'mobno' => 'required|digits:10',
'dob' => 'required|date|date_format:Y-m-d',
'gender' => 'required',
'address' => 'required|string',
'country' => 'required'
];
$validate = Validator::make($request->all(),$rules,$messages);
if($validate->fails()){
return back()->withInput()->withErrors($validate->messages());
}
else{
return view('recheck_form')
->with('first_name',$first_name)
->with('last_name',$last_name)
->with('email',$email)
->with('password',$password)
->with('mobno',$mobno)
->with('dob',$dob)
->with('gender',$gender)
->with('address',$address)
->with('country',$country)
->with('time',$time);
}
}
}
And the Blade Page
<div class="container pt-2">
<div class="row shadow p-3 mb-5 bg-info rounded text-center text-white">
<div class="col ">
<h2>New User Registration</h2>
</div>
</div>
</div>
<div class="row m-5 p-5 bg-light text-info">
<div class="col">
<form class="form-group" method="post" action="recheck-form" autocomplete="off">
<div class="form-group {{ $errors->has('first_name') ? ' has-error' : '' }}">
#csrf
<label for="fname">First Name:</label>
<input type="text" class="form-control" name="first_name">
<small class="text-danger">{{$errors->first('first_name') }}</small>
</div>
<div class="form-group {{ $errors->has('last_name') ? ' has-error' : '' }}">
<label for="lname">Last Name:</label>
<input type="text" class="form-control" name="last_name">
<small class="text-danger">{{ $errors->first('last_name') }}</small>
</div>
<div class="form-group {{ $errors->has('email') ? ' has-error' : '' }}">
<label for="eid">Email/Username:</label>
<input type="text" class="form-control" name="eid">
<small class="text-danger">{{ $errors->first('email') }}</small>
</div>
<div class="form-group {{ $errors->has('password') ? ' has-error' : '' }}">
<label for="pwd">Password:</label>
<input type="password" class="form-control" name="password">
<small class="text-danger">{{$errors->first('password') }}</small>
</div>
<div class="form-group {{ $errors->has('confirm_password') ? ' has-error' : '' }}">
<label for="confpwd">Confirm Password:</label>
<input type="password" class="form-control" name="confirm_password">
<small class="text-danger">{{ $errors->first('confirm_password') }}</small>
</div>
<div class="form-group {{ $errors->has('mobno') ? ' has-error' : '' }}">
<label for="mobno">Mobile Number:</label>
<input type="text" class="form-control" name="mobno">
<small class="text-danger">{{ $errors->first('mobno') }}</small>
</div>
<div class="form-group {{ $errors->has('dob') ? ' has-error' : '' }}">
<label for="dob">Date of Birth(in YYYY-MM-DD):</label>
<input type="text" class="form-control" name="dob">
<small class="text-danger">{{ $errors->first('dob') }}</small>
</div>
<div class="form-group {{ $errors->has('gender') ? ' has-error' : '' }}">
<label for="gender">Gender:</label>
<br>
<input class="form-check-input" type="radio" name="gender" id="Female" value="Female">
<label class="form-check-label" for="Female">
Female
</label>
<input class="form-check-input" type="radio" name="gender" id="Male" value="Male">
<label class="form-check-label" for="Male">
Male
</label>
<input class="form-check-input " type="radio" name="gender" id="Other" value="Other">
<label class="form-check-label" for="Other">
Other
</label>
<small class="text-danger">{{ $errors->first('gender') }}</small>
</div>
<div class="form-group {{ $errors->has('address') ? ' has-error' : '' }}">
<label for="address">Address:</label>
<textarea class="form-control" rows="5" name="address"></textarea>
<small class="text-danger">{{ $errors->first('address') }}</small>
</div>
<div class="form-group {{ $errors->has('country') ? ' has-error' : '' }}">
<label for="country">Country:</label>
<select name="country" class="form-control" id="countrylist">
<option value disabled selected>Select Country</option>
#foreach($countryname as $key => $country)
<option id="countryname" value="{{$country->countryname}}">{{$country->countryname}}</option>
#endforeach
</select>
<small class="text-danger">{{ $errors->first('country') }}</small>
</div>
<div class="form-group">
<label for="dt">Date and Time of Submission:</label>
<input type="text" class="form-control" name="dt" readonly value=#php date_default_timezone_set("Asia/Kolkata"); echo date("Y-m-d,H:i:s ") #endphp>
</div>
<div class="form-group text-center">
<input type="submit" class="btn btn-primary mb-2">
</div>
I dont know how to upload images to Stack Overflow questions. If you can tell me, I can show you the exact problem via images.
First of all, you are doing wrong in your code. In the validator rules and messages array, you did write wrong.
Please check the below code It will resolve your issue.
$messages=
[
'eid.required' => "The :attribute field is required",
'eid.email' => "The :attribute :input format should be example#example.com/.in/.edu/.org....",
'eid.unique' => "The :attribute :input is taken. Please use another email address",
'confirm_password.same' => "Password and Confirm password fields must match exactly",
'mobno.digits' => "The :attribute field accepts only numbers",
'mobno.digits:10' => "The :attribute should be 10 digits long",
'dob.date_format' => "The date format :input should be YYYY-MM-DD",
'address.string' => "The :attribute :input must be in the form of a string"
];
$rules = [
'first_name' => 'required',
'last_name' => 'required',
'eid' => 'required|email|unique:users',
'password' => 'required|min:8',
'confirm_password' => 'required|min:8|same:password',
'mobno' => 'required|digits:10',
'dob' => 'required|date|date_format:Y-m-d',
'gender' => 'required',
'address' => 'required|string',
'country' => 'required'
];
The page gets redirected back but the input fields are not filled up with the past input.
For this issue you can try the below approach will definitely work
$validate = Validator::make($request->all(),$rules,$messages);
if($validate->fails()){
return redirect()->back()->withErrors($validate->messages())->withInput();
}
And in your input value attribute please write like this value="{{ old('name') }}"
<input type="text" name="name" value="{{ old('name') }}" />
<input type="email" name="email" value="{{ old('email') }}" />
<input type="text" name="first_name" value="{{ old('first_name') }}" />
The names of your field and rules dont match. please update
"name="eid" to be name="email" as that's what the rule 'email' => 'required|email|unique:users' will look for.
Then also update $email = $request->email;
In order to reset the previously entered values after the error re-direct, please update your view like this:
In view:
<input type="text" class="form-control" name="email" value="{{ old('email') }}">
Note,
If you want to keep it as eid, thats fine too, you just need to make sure everything uses eid, ie:
<div class="form-group {{ $errors->has('eid') ? ' has-error' : '' }}">
<label for="eid">Email/Username:</label>
<input type="text" class="form-control" name="eid" value="{{ old('eid') }}">
<small class="text-danger">{{ $errors->first('eid') }}</small>
</div>
$email = $request->eid;
'eid.email' => "The :attribute :input format should be example#example.com/.in/.edu/.org....",
'eid.unique' => "The :attribute :input is taken. Please use another email address",
'eid' => 'required|email|unique:users',

Problem with updating data in database in Laravel

I have a form that has username, job, date of birth and city inputs. I am trying to update them through my form. When I click submit button my form submits but data remains unchanged. I successfully read them from database but when I try and update nothing happens. Any help is appreciated. Here is my code.
UserController.php
public function showProfile($username, Request $request)
{
$profileId = User::getIdFromUsername($username);
$userForShowProfile = User::with('userProfile')->where('id', $profileId)->first();
return view('profile.show', compact('userForShowProfile'));
}
public function updatePersonalData(UpdatePersonalDataRequest $request)
{
$user = Auth::user();
$request->validated();
$user->where('id', $user->id)->update(
[
'username' => $request->username,
'job' => $request->job,
'date_of_birth' => $request->date_of_birth,
'updated_at' => Carbon::now()
]
);
$city = City::where('name', $request['city'])->first();
if ($city != null && $city->count() > 0) {
$request->user()->city()->associate($city->id);
}
$request->user()->save();
return response()->json(null, 204);
}
web.php
Route::get('profile/{profile}', 'UserController#showProfile')->name('profile.show');
Route::patch('profile/personal', 'UserController#updatePersonalData')->name('profile.update.personal.data');
show.blade.php
<section data-edit="generalInfo" class="editGeneralInfo">
<form action="{{ route('profile.update.personal.data') }}" method="POST" class="flex">
#method('PATCH')
#csrf
<div class="form-group">
<label for="" class="textBold">Name</label>
<input type="text" name="username" value="{{ $userForShowProfile->username }}">
</div>
<div class="form-group">
<label for="" class="textBold">Ort</label>
<input type="text" name="job" value="{{ $userForShowProfile->job }}">
</div>
<div class="form-group">
<label for="" class="textBold">Beruf</label>
<input type="text" name="city" value="{{ $userForShowProfile->city->name }}">
</div>
<div class="form-group mb-0">
<label for="" class="textBold">Geburtsdatum</label>
<input type="text" placeholder="dd/mm/yyyy" name="date_of_birth" value="{{ $userForShowProfile->date_of_birth }}">
<p class="infoText mt-2">Dein Geburtsdatum wird nicht öffentlich angezeigt.</p>
<p class="infoText">Wir ermitteln damit nur dein Alter</p>
</div>
<div class="form-group">
<label for="" class="textBold">Button</label>
<input type="submit" class="form-control" name="submit" value="BUTTON">
</div>
</form>
</section>
rules
public function rules()
{
return [
'username' => ['string', 'max:255', 'unique:users,username'],
'job' => ['string', 'max:255'],
'date_of_birth' => ['date', 'date_format:d.m.Y'],
'city' => ['string', 'max:255', 'exists:cities,name']
];
}
I have a same problem in my code the error is:
I save like this : $user->save();
While i change the data using: $user and $user->profile
So i added in my code another line: $user->profile->save()
It work now

Laravel 5.5, cant pass data after validation

i have this validation for registering new users but every time I submit it just reloads and stays in the page, I am lost, but when I use the Registration::create($request->all()) without validation from the top it pushes through and saves the data. please help
my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Registration;
class RegistrationsController extends Controller
{
public function index(){
return view('registrations.create');
}
public function store(){
request()->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
$validatedUser = Registration::create([
'name' => request('name'),
'email' => request('email'),
'password' => bcrypt(request('password'))
]);
return redirect()->home();
}
}
here is my create.blade
<form action="{{ route('registrations.store') }}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="">Full Name</label>
<input type="text" class="form-control" name="name" id="name" aria-describedby="helpId" placeholder="Juan Dela Cruz">
<small id="helpId" class="form-text text-muted">Ex. Juan Dela Cruz</small>
</div>
<div class="form-group">
<label for="">Password</label>
<input type="password" class="form-control" name="password" id="password" placeholder="type password here..">
</div>
<div class="form-group">
<label for="">Confirm password</label>
<input type="password" class="form-control" name="password_confirm" id="password_confirm" placeholder="type password here..">
</div>
<div class="form-group">
<label for="">Email address</label>
<input type="email" class="form-control" name="email" id="email" aria-describedby="emailHelpId" placeholder="juandelacruz#gmail.com">
<small id="emailHelpId" class="form-text text-muted">Must be valid email address</small>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
for my route i just used Route::resource
thank you
This is probably because your password confirmation fails.
Your confirmation field must be named : password_confirmation and not password_confirm as the documentation say :
The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

Categories