Create method stopped working - laravel

I have this simple code
public function store(Request $request)
{
$this->Validate($request, [
'name' => 'required|min:3|max:20',
'age' => 'required|integer|between:18,99',
'height' => 'required|integer|between:120,230',
'weight' => 'required|integer|between:30,150',
'region' => 'required|integer',
'city' => 'required',
'phone' => 'required|integer'
]);
Person::create([
'name' => $request->name,
'age' => $request->age,
'height' => $request->height,
'weight' => $request->weight,
'city_id' => $request->city,
'region_id' => $request->region,
'phone' => $request->phone,
'user_id' => Auth::user()->id
]);
}
This code stop working but it doesn't show error. I have only white page. This code has worked before. And ne record doesn't create. What happend?
#edit
Now it works but... In phpmyadmin everything is ok. New records are added coccectly but tinker show my only one record which I added manually in phpmyadmin. If I try choose the lastest record added by me:
$latest = Person::where('user_id', Auth::user()->id)->latest()->first();
It show me this single element which I created in phpmyadmin. What's wrong?

Replace store as below:
public function store(Request $request) {
$this->Validate($request->all(), [
'name' => 'required|min:3|max:20',
'age' => 'required|integer|between:18,99',
'height' => 'required|integer|between:120,230',
'weight' => 'required|integer|between:30,150',
'region' => 'required|integer',
'city' => 'required',
'phone' => 'required|integer'
]);
$person= new Person();
$person->username= $request['username'];
$person->age = $request['age'];
$person->height = $request['height'];
$person->weight = $request['weight'];
$person->region = $request['region'];
$person->city = $request['city'];
$person->phone = $request['phone'];
$person->save();
}

in your controller :
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:3|max:20',
'age' => 'required|integer|between:18,99',
'height' => 'required|integer|between:120,230',
'weight' => 'required|integer|between:30,150',
'region' => 'required|integer',
'city' => 'required',
'phone' => 'required|integer'
]);
Person::create([
'name' => $request->name,
'age' => $request->age,
'height' => $request->height,
'weight' => $request->weight,
'city_id' => $request->city,
'region_id' => $request->region,
'phone' => $request->phone,
'user_id' => Auth::user()->id
]);
return redirect()->back()->with('success','Person Created');
}
in your blade view :
#if (count($errors) > 0)
#foreach ($errors->all() as $error)
<p class="alert alert-danger alert-dismissible fade show" role="alert">{{ $error }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</p>
#endforeach
#endif

Related

how to exit when I return laravel Validation?

i' new to laravel here I have created a registration API and private function for validation but not show validation.
that is my validation function
`private function validateInfo($request, $toValidate) {
$validator = Validator::make($request->all(), $toValidate);
if ($validator->fails()) {
$errors = $validator->errors()->toArray();
$values = array_values($errors);
$arr = [];
foreach ($values as $errorlst)
array_push($arr, $errorlst[0]);
$error = implode(',', $arr);
return response()->json(['error' => true, 'message' => $error], 400);
exit;
}
}`
call function under switch case :-
switch ($request->user_type) :
case "teacher":
$this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
case "student" :
$this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
endswitch;
it is not showing validation error. Any Mistake?
Thank You
instead of this you can create a form request to validate your request.
class UserRequest extends FormRequest
{
protected function failedValidation(Validator $validator) {
throw new HttpResponseException(
response()->json([
"message" => implode(',',$validator->errors()->all())
],422));
}
public function rules(){
return [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
}
}
please check docs for more info https://laravel.com/docs/8.x/validation#form-request-validation
You Need to return For Display Like
switch ($request->user_type) :
case "teacher":
return $this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
case "student" :
return $this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
endswitch;

Problems in implement reCaptcha V3 in laravel

I am using Google reCaptcha v3. I am trying to implement it onto my profile page.It's load and shown in my page ,but when I want to verify it ,it does not work ,I am using this package:https://github.com/RyanDaDeng/laravel-google-recaptcha-v3#settings
in this config :
'secret_key' => env('RECAPTCHA_V3_SECRET_KEY', '6LcqC'),
'site_key' => env('RECAPTCHA_V3_SITE_KEY', '6LcqnrO-K-xb'),
in this blade :
<form>
{!! GoogleReCaptchaV3::renderOne('contact_us_id','contact_us') !!}
</form>
in my controller:
public function get_data_edit(Request $request)
{
$request->validate(
[
'phone' => ['required', 'max:11', new phone],
'year' => 'nullable|numeric',
'month' => 'nullable|numeric|max:12',
'day' => 'nullable|numeric|max:31',
'sex' => 'nullable|boolean',
'marital_status' => 'nullable|boolean',
'f_name' => 'required|max:15',
'l_name' => 'required|max:20',
'job' => 'nullable|string',
'email' => 'nullable|email',
'national_code' => 'nullable|min:10|max:10',
'family_role' => 'nullable|string',
'g-recaptcha-response' => [new GoogleReCaptchaV3ValidationRule('contact_us')]
],
[]
);
where is my problem ?

How to switch destination mail address by selecting check box in Laravel contact form

This is contact form and sorking.
I would like to switch email destination by User selecting check box.
For example. Here is a column called genders.
When user check 'man' select box, email destination will be [TO]'man_survey#12345677.site' and [CC] is 'man_cc_survey#12345677.site'.
When user check 'female', email destination will be [TO] 'female_survey#12345677.site' and [CC] is 'female_cc_survey#12345677.site'
Could you teach me how to add this function to my current code?
My Laravel framework is 5.7.28
public function complete(ContactRequest $request)
{
$input = $request->except('action');
if ($request->action === 'back') {
return redirect()->action('ContactsController#index')->withInput($input); }
if (isset($request->type)) {
$request->merge(['type' => implode(', ', $request->type)]);
}
// store data
Contact::create($request->all());
// send mail
\Mail::send(new \App\Mail\Contact([
'to' => $request->email,
'to_name' => $request->name,
'from' => 'survey#12345677.site',
'from_name' => 'from name',
'subject' => 'Thank you',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
]));
// recive mail
\Mail::send(new \App\Mail\Contact([
'to' => 'survey#12345677.site',
'to_name' => 'to name',
'from' => $request->email,
'from_name' => $request->name,
'subject' => 'you got mail',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
], 'from'));
return view('contacts.complete');
}
}
UPDATE THis worked
public function complete(ContactRequest $request)
{
$input = $request->except('action');
if ($request->action === 'back') {
return redirect()->action('ContactsController#index')->withInput($input); }
if (isset($request->type)) {
$request->merge(['type' => implode(', ', $request->type)]);
}
// store data
Contact::create($request->all());
if($request->gender == 'male') {
// send mail
\Mail::send(new \App\Mail\Contact([
'to' => $request->email,
'to_name' => $request->name,
'from' => 'man_survey#12345677.site',
'from_name' => 'from name',
'subject' => 'Thank you',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
]));
// recive mail
\Mail::send(new \App\Mail\Contact([
'to' => $email,
'to_name' => 'to name',
'from' => $request->email,
'from_name' => $request->name,
'subject' => 'you got mail',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
], 'from'));
return view('contacts.complete');
} else {
// send mail
\Mail::send(new \App\Mail\Contact([
'to' => $request->email,
'to_name' => $request->name,
'from' => 'female_survey#12345677.site',
'from_name' => 'from name',
'subject' => 'Thank you',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
]));
// recive mail
\Mail::send(new \App\Mail\Contact([
'to' => $email,
'to_name' => 'to name',
'from' => $request->email,
'from_name' => $request->name,
'subject' => 'you got mail',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
], 'from'));
return view('contacts.complete');
}
}
You can use a radio button in the form to see what Gender the user has selected.
In HTML
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="no_answer">Prefer Not to Answer
In the controller you can use something like this
public function complete(ContactRequest $request)
{
$input = $request->except('action');
if ($request->action === 'back') {
return redirect()->action('ContactsController#index')->withInput($input); }
if (isset($request->type)) {
$request->merge(['type' => implode(', ', $request->type)]);
}
// store data
Contact::create($request->all());
if($request->gender == 'male') {
$email = 'man_survey#12345677.site';
$cc_email = 'man_cc_survey#12345677.site'
} else {
$email = 'female_survey#12345677.site';
$cc_email = 'female_cc_survey#12345677.site'
}
// send mail
\Mail::send(new \App\Mail\Contact([
'to' => $request->email,
'to_name' => $request->name,
'from' => 'survey#12345677.site',
'from_name' => 'from name',
'subject' => 'Thank you',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
]));
// recive mail
\Mail::send(new \App\Mail\Contact([
'to' => $email,
'to_name' => 'to name',
'from' => $request->email,
'from_name' => $request->name,
'subject' => 'you got mail',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
], 'from'));
return view('contacts.complete');
}

laravel how to validate as equal to a variable

in laravel validation (registering) i want to compare one of the fields with a php variable (it should be equal with that)
how can i do this?
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
}
You can do it for example for name field like this:
$variable = "something"
return Validator::make($data, [
'name' => [
'required',
Rule::in([$variable]),
],
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
Remember to import Rule Class (use Illuminate\Validation\Rule;)
You can get more info in: https://laravel.com/docs/5.4/validation#rule-in
EDIT
As suggested by #patricus, you can also concatenate the variable
$variable = "something"
return Validator::make($data, [
'name' => 'required|in:'.$variable,
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
EDIT2
If you have a variable that is an array:
$variable = ['one','two'];
return Validator::make($data, [
'name' => 'required|in:'.implode(",", $variable),
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
Or
$variable = ['one','two']
return Validator::make($data, [
'name' => [
'required',
Rule::in($variable),
],
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);

In codeigniter redirect to paypal after order placed ,submitting form from controller

I'm very new to codeigniter.my question is after submitting billing and shipping address how to i redirect to paypal.I mean is there any way to redirect/form submission from controller.Below is my place order function in controller
function place_order()
{
if ($this->input->server('REQUEST_METHOD') === 'POST')
{
$this->form_validation->set_rules('bill_first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('bill_last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('address', 'Address', 'trim|required');
$this->form_validation->set_rules('phone', 'Phone', 'trim|required');
if ($this->form_validation->run())
{
$data_to_store = array(
'bill_first_name' => $this->input->post('bill_first_name'),
'bill_last_name' => $this->input->post('bill_last_name'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'phone' => $this->input->post('phone'),
'country' => $this->input->post('country'),
'city' => $this->input->post('city'),
'zip' => $this->input->post('zip'),
'user_id' => $this->session->userdata('sess_user_id'),
'session_id' => $this->session->userdata('session_id'),
'create_date' => date('Y-m-d H:i:s')
);
$customer=$this->checkout_model->add_user('tbl_customer',$data_to_store);
if($customer){
$data = array(
'customer_id' => $customer,
'user_id' => $this->session->userdata('sess_user_id'),
'session_id' => $this->session->userdata('session_id'),
'create_date' => date('Y-m-d H:i:s')
);
$order=$this->checkout_model->add_user('tbl_order',$data);
if($order)
{
if ($cart = $this->cart->contents()):
foreach ($cart as $item):
$order_detail = array(
'order_id' => $order,
'product_id' => $item['id'],
'qty' => $item['qty'],
'product_price' => $item['price'],
'sub_total' => $item['qty'] * $item['price']
);
$cust_id = $this->checkout_model->add_user('tbl_order_details',$order_detail);
endforeach;
endif;
}
$this->cart->destroy();
}
paypallllllllllllllllllllllll
}
}
On the bottom of this page of the manual, you can find the redirect()-function (using the URL-helper).
--- EDIT ---
Maybe something like this (see this answer):
$query_data = array(
'business' => 'your-paypal-email-address',
'cmd' => '_xclick',
'item_name' => 'Order #' . $order,
'amount' => '100.00', // update with your total price
'shipping' => '10.00'
);
header('Location: https://www.paypal.com/cgi-bin/websrc/?' . http_build_query($query_data));

Resources