I am trying to format date with Carbon before importing my data due to the following reason
when importing dates they are changed to a value resembling this 101010(example) even though the excel value looks like 01/01/1001
Here is a discussion on it however I have had no luck with there suggested ideas and am wondering if there are any other recommenadations or if I am missing something?
here is my import ClientsImport.php
public function model(array $row)
{
return new Client([
'category' => $row[0],
'referral_type' => $row[1],
'first_name' => $row[2],
'middle_initial' => $row[3],
'last_name' => $row[4],
'occupation' => $row[5],
'dob' => \Carbon\Carbon::createFromFormat('m/d/Y', $row['6']),
'email' => $row[7],
'cell_phone' => $row[8],
'work_phone' => $row[9],
'has_spouse' => $row[10],
'spouse_first_name' => $row[11],
'spouse_middle_initial' => $row[12],
'spouse_last_name' => $row[13],
'spouse_occupation' => $row[14],
'spouse_dob' => \Carbon\Carbon::createFromFormat('m/d/Y', $row['15']),
'spouse_email' => $row[16],
'spouse_cell_phone' => $row[17],
'spouse_work_phone' => $row[18],
'street_address' => $row[19],
'city' => $row[20],
'state' => $row[21],
'postal_code' => $row[22],
]);
}
I have tried \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['6'])
and I get this error A non well formed numeric value encountered
And also tried \Carbon\Carbon::createFromFormat('m/d/Y', $row['6'])
and I get this error The separation symbol could not be found ↵The separation symbol could not be found
Related
This is the code in the migration:
$table->string('role')->default('Standard');
When I leave the input box blank, it gives me an error:
"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'role' cannot be null
How do we set the default value to "Standard" if the input box is left blank?
Code for Controller
public function store(Request $request)
{
//return ['message' => 'I have your data'];
$request->validate([
'firstname' => 'required|string|max:191',
'lastname' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users',
'password' => 'required|string|min:6',
]);
return User::create([
'firstname' => $request['firstname'],
'lastname' => $request['lastname'],
'email' => $request['email'],
'phone' => $request['phone'],
'role' => $request['role'],
'usernotes' => $request['usernotes'],
'password' => Hash::make($request['password']), //make sure to import Hash: use Illuminate\Support\Facades\Hash;
'created_by' => $request['created_by'],
'updated_by' => $request['updated_by'],
]);
}
In your code $request['role'] should be null which is causing the problem since the role field is not Nullable.
What you can do is, add the dwfault value if the role is null, just made following changes in your code and it should work.
public function store(Request $request)
{
//return ['message' => 'I have your data'];
$request->validate([
'firstname' => 'required|string|max:191',
'lastname' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users',
'password' => 'required|string|min:6',
]);
return User::create([
'firstname' => $request['firstname'],
'lastname' => $request['lastname'],
'email' => $request['email'],
'phone' => $request['phone'],
'role' => $request['role'] ?? 'Standard',
'usernotes' => $request['usernotes'],
'password' => Hash::make($request['password']), //make sure to import Hash: use Illuminate\Support\Facades\Hash;
'created_by' => $request['created_by'],
'updated_by' => $request['updated_by'],
]);
}
That should fix the issue.
Explanation: I am using Null coalescing (??) operator of PHP which will replace the null value with 'Standard'. It works only is PHP 7+, if you have a lower version of PHP then you can consider using the Ternary operator(?:).
Reference: https://www.php.net/manual/en/migration70.new-features.php
use nullable();
$table->string('role')->default('Standard')->nullable();
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
]);
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.'
]);
I am having a problem with validation - the validation doesn't pass and it also doesn't show what type of error is making it
$rules = array(
'first_name' => 'required|min:3',
'last_name' => 'required|min:3',
'email' => 'required|email|unique:users',
'company_name' => 'required|min:3',
'zip_code' => 'required|integer|between:3,10',
'address_1' => 'required|min:3',
'address_2' => 'required|min:3',
'city' => 'required|min:3',
'country' => 'required|min:3',
'state' => 'required|min:3',
'phone_num' => 'required|integer|between:4,10',
'security_answer' => 'required|min:10',
'password' => 'required|min:3|confirmed',
'password_confirmation' => 'required'
);
This is how I am retrieving errors
{{ $errors->first('first_name') }}
{{ $errors->first('last_name') }}
and so on
if I remove |between:4,10 from the phone_num and zip_codeeverything works fine otherwise it doesn't show me any error but also doesn't pass the validation
Is there something wrong I am doing here? I tried finding the answer online but couldn't understand the problem here
You should change validation rules
'zip_code' => 'required|integer|between:3,10'
'phone_num' => 'required|integer|between:4,10'
to
'zip_code' => 'required|digits|between:3,10'
'phone_num' => 'required|digits|between:4,10'
Your validation rule now for zip code expects an integer between 3 to 10. I suppose you need numeric value with length from 3 to 10.
am using code as below but am not getting any value.i gave as print_r($this->title); what i have to do to get the value. thanks
if(isset($_POST['is_ajax']) && $_POST['is_ajax']) {
print_r($this->title);
$respondentArray = array(
'state' => $_POST['state'],
'name' => $_POST['name'],
'title' => $_POST['title'],
'dline' => $_POST['directline'],
'email' => $_POST['email'],
'organization' => $_POST['organization'],
'address' => $_POST['address'],
'city' => $_POST['city'],
'state1' => $_POST['state1'],
'zip' => $_POST['zip'],
'generalphone' => $_POST['generalphone'],
'fax' => $_POST['fax'],
);
$this->session->set_userdata($respondentArray);
i guess this is what you need
echo $this->input->post('title') ;
$this->input->post(postedValues) this gets all the input values that is posted...
Use var_dump($this->title); to test