I am using Laravel-5.8 as backend for an application. I have written all the Api for the endpoints.
Laravel: ApiController
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email',
//'email' => 'required|email|unique:users|max:255',
'phone' => 'required|max:14',
'business_name' => 'required',
'truck_type' => 'required',
'truck_required' => 'required',
'quote_origin' => 'required',
'quote_destination' => 'required',
'commodity' => 'required',
// 'weight' => 'required',
'loading_date' => 'required'
]);
$clientquote = new ClientQuote([
'first_name' => $request->first_name,
'last_name'=> $request->last_name,
'email' => $request->email
'phone' => $request->phone,
'business_name' => $request->business_name,
'address' => $request->address,
'comment' => $request->comment,
'truck_type' => $request->truck_type,
'truck_required' => $request->truck_required,
'quote_origin' => $request->quote_origin,
'quote_destination' => $request->quote_destination,
'commodity' => $request->commodity,
'loading_date' => $request->loading_date
]);
$clientquote->save();
$mainData = array();
$mainData['to'] = $clientquote->toArray()['email'];
$mainData['from'] = "support#tsllimited.com";
$mainData['subject'] = "Client Quote";
$mainData['content'] = "Your Quote have been successfully received. You will hear from us shortly through the provided email. Thank you!";
$this->mailSend($mainData);
return response()->json([
'message' => 'Quote Successfully Sent!'
], 201);
}
public function indexClientQuote(Request $request) {
return response()->json(ClientQuote::get());
}
When I tested the Request on the POSTMAN, I got the error shown below:
I don't know why it's expecting ']'.
What could have caused the error?
you missed ',' after email
'email' => $request->email
'phone' => $request->phone,
To
$clientquote = new ClientQuote([
'first_name' => $request->first_name,
'last_name'=> $request->last_name,
'email' => $request->email, //here you missed comma
'phone' => $request->phone,
'business_name' => $request->business_name,
'address' => $request->address,
'comment' => $request->comment,
'truck_type' => $request->truck_type,
'truck_required' => $request->truck_required,
'quote_origin' => $request->quote_origin,
'quote_destination' => $request->quote_destination,
'commodity' => $request->commodity,
'loading_date' => $request->loading_date
]);
Related
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.');
}
}
}
},
],
];
i have a problem this controller is not working how can i do? should send mutiple emails how do i solve?
I don't know how to handle it
function submit(Request $request) {
$this->validate($request, [
'email' => 'required|email',
'file' => 'mimes:pdf,doc,docx'
]);
$data = array(
'name' => $request->name,
'cognome' => $request->cognome,
'luogo' => $request->luogo,
'date' => $request->date,
'telefono' => $request->telefono,
'email' => $request->email,
'citta' => $request->citta,
'provincia' => $request->provincia,
'studio' => $request->studio,
'lingua' => $request->lingua,
'livello' => $request->livello,
'lingua2' => $request->lingua2,
'livello2' => $request->livello2,
'file' => $request->file,
'agree' => $request->agree
);
Mail::send('mail', $data, function($message) use ($request,$data){
$message->to('luis#gmail.com', 'luis')->subject('Send mail ' . $request->name);
$message->from($request->email, $request->name);
if($request->hasFile('file')){
$message->attach($request->file('file')->getRealPath(), array(
'as' => $request->file('file')->getClientOriginalName(),
'mime' => $request->file('file')->getMimeType())
);
}
});
Session::flash('success', 'Mail spedita con sucesso');
}
I wish I could solve the problem
any advice? on how to do it?
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
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();
}
I have added 3 fields to the Laravel OOB Registration Form, they are Birth Month, Day, and Year. I pass these fields to the validator function in the RegisterController and convert them to an age with Carbon:
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
This part works fine, I can pass the variable to a field in the table and see the correct age.
How do I add $theAge to my Validator?
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
]);
I have tried the following but it appears to be ignored on validation:
$theAge => 'bail|min:21'
I have looked into the After Validation Hook but don't understand how to use it in my situation.
you can add the $theAge variable to the data array.
$data['age'] = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
You can put the calculated value back in the $data before you call the validator like this:
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
$data['age'] = $theAge;
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
'age' => 'min:21'
]);
Alternatively, you can let the user select their date of birth using date picker (e.g. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date) like this:
<input name="birthday" type="date">
and in your validator, do this define the age check plus a custom error message:
return Validator::make($data, [
// ... snipped
'birthday' => 'required|date|before_or_equal:' . Carbon::now()->subYears(21)->toDateString()
], [
'birthday.before_or_equal' => 'You must be at least 21 years old.'
]);