Misredirect with validation in laravel - php

I have two different routes:
Route::post('contact', 'PublicController#postContact');
Route::post('inquiry', [ 'as' => 'inquiry', 'uses' => 'PublicController#postInquiry']);
and their methods:
public function postContact(Request $request){
$token = $request->input('g-recaptcha-response');
if (strlen($token) >0 ) {
$this->validate($request, [
'fname' => 'required',
'lname' => 'required',
'phone' => 'required',
'email' => 'required|email',
'subject' => 'min:3',
'message' => 'min:10']);
$data = array(
'fname' => $request->fname,
'lname' => $request->lname,
'phone' => $request->phone,
'email' => $request->email,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('rahunn3#gmail.com');
$message->subject($data['subject']);
});
Session::flash('success', 'Your Email was sent sucessfully!');
return redirect('/contact');
}
}
and:
public function postInquiry(Request $request){
$token = $request->input('g-recaptcha-response');
if (strlen($token) >0 ){
$this->validate($request, [
'fname' => 'required',
'lname' => 'required',
'phone' => 'required',
'country' => 'required',
'email' => 'required|email',
'subject' => 'min:3',
'message' => 'min:10']);
$data = array(
'fname' => $request->fname,
'lname' => $request->lname,
'phone' => $request->phone,
'country' => $request->country,
'email' => $request->email,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
Mail::send('emails.inquiry', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('rahunn3#gmail.com');
$message->subject($data['subject']);
});
Session::flash('success', 'Your Email was sent sucessfully!');
return view('public.pages.thankyou');
}
}
the trouble i'm having is the second method is not working as it should be.The inquiry form is in view(single.blade.php). After sumbitting the form user should be redirected to view(thankyou.blade.php) view but it is redirecting to view(single.blade.php) without sending email and doesn't show any errors.
If I remove the validation part:
$this->validate($request, [
'fname' => 'required',
'lname' => 'required',
'phone' => 'required',
'country' => 'required',
'email' => 'required|email',
'subject' => 'min:3',
'message' => 'min:10'
]);
from the second method postInquiry it works perfectly(sends email and redirects do thankyou.blade.php)
Can anyone tell me what wrong I am doing here ?

It is redirecting because your validation is failing.
Add the following code to the top of your single.blade.php file and it will tell you why the validation is failing
#if($errors->has())
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
#endif
See here for more information : https://laravel.com/docs/5.3/validation#quick-displaying-the-validation-errors

Related

Get API errors from Laravel into Flutter

I'm using this code for User Sign Up in Laravel:
class UsersController extends Controller
{
public function register(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8',
'phone' => 'required|unique:users',
'type' => 'boolean',
'verified' => 'boolean'
]);
$user = User::create([
'name' => $validatedData['name'],
'email' => $validatedData['email'],
'phone' => $validatedData['phone'],
'type' => $validatedData['type'],
'verified' => $validatedData['verified'],
'password' => Hash::make($validatedData['password']),
]);
$token = $user->createToken('auth_token')->accessToken;
return response()->json([
'user' => $user,
'access_token' => $token,
'token_type' => 'Bearer',
]);
}
As you can see , some of my field are unique , so I want to display a message in my front side that displays the error from the back side.
Exemple : When a user enters a used phone number , I want to return an error saying : this phone number has been used .
How can I achieve this ? Thank you.
You can get a User record from the database by phone. If the record exists, then do your logic
class UsersController extends Controller
{
public function register(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8',
'phone' => 'required|unique:users',
'type' => 'boolean',
'verified' => 'boolean'
]);
// get a user record by phone
$record = User::where('phone', $validatedData['phone'])->find();
if (!empty($record)) {
return response()->json([
'error' => 'this phone number has been used'
])
}
//.....
}

Laravel crud, update error (Call to a member function update() on null)

So, I'm trying to make update function, using this
public function editProfile(Request $request, $id)
{
// Validating
$request->validate([
// 'username' => 'nullable|unique:users|min:4',
// 'email' => 'nullable|unique:users|email:rfc,dns',
'contact_phone' => 'nullable|digits_between:9,12',
'bio_en' => 'nullable',
'bio_ar' => 'nullable',
'icon' => 'nullable',
'logo' => 'nullable',
'plan_id' => 'required',
'category_id' => 'required',
'is_local' => 'required',
]);
$user = User::find($id);
$user->update([
'username' => $request->username,
'email' => $request->email,
'contact_phone' => $request->contact_phone,
'bio_en' => $request->bio_en,
'bio_ar' => $request->bio_ar,
'logo' => $request->logo,
'icon' => $request->icon,
'plan_id' => $request->plan_id,
'category_id' => $request->category_id,
'referred_by_id' => Auth::user()->id,
]);
if ($user) {
return back()->with("success", __("editedSuccessfully"));
} else {
return back()->with('error', __("somethingWentWrongTryAgainLater"));
}
}
but I get the error
Call to a member function update() on null
shows on line $user->update
hope anyone can help me
It returns null because it cannot find the user with User::find($id). Check that you have a user by $id

Alternately login after register in Laravel

ok, I'm trying to login the users after success registration into their panels. the database line built successfully. but alternately user login become successful but once not.
Here is the Request Controller Code:
$request->merge(['phone'=> '09123456789']);
$request->merge(['name' => 'User']);
$request->merge(['password' => 'pass123456789']);
$request->merge(['email' => 'user#email.com']);
$credentials = $request->validate([
'name' => 'required',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/',
'password' => 'required',
'email' => 'required',
]);
User::create([
'name' => $request->name,
'email' => $request->email,
'phone' => $credentials['phone'],
'password' => Hash::make($credentials['password'])
]);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->route('panel');
}
The Model:
protected $fillable = [
'name',
'phone',
'email',
'password',
];
let me know if I didn't describe perfectly
First, I would stop using validation in the controller and use form request validation instead. It's been the standard since Laravel 5.
Try this simplified code using Auth::login() instead:
public function createUser(Request $request)
{
$request->validate([
'name' => 'required|string|max:64',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/',
'password' => 'required|string|max:64',
'email' => 'required|email|max:64',
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'password' => Hash::make($request->password),
]);
Auth::login($user);
return redirect()->route('panel');
}

how can i send an email when user register but to the specific email in Laravel 8

I need when a user registers successfully to send notification email to the reference email field
the notification include one line text with link buttom and when he click on the buttom will redirect to my website
I need to send that notification to the referance_email field
public function create(array $input)
{ $massage = ['tc_no_pasaport_no.unique'=> 'Sorry, internal error .',
'phone.unique'=>'Sorry, internal error .' ];
Validator::make($input, [
'phone' => ['required', 'string', 'max:255','unique:users'],
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'tc_no_pasaport_no' => ['required','max:11','unique:users'],
'place_of_birth' => ['required'],
'date_of_birth' => ['required'],
'educational_status' => ['required','string'],
'school_department' => ['required'],
'address' => ['required'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
],$massage)->validate();
$users = User::where('email', '=', $input['email'])->first();
if ($users === null) {
return User::create([
'name' => $input['name'],
'email' => $input['email'],
'phone' => $input['phone'],
'password' => Hash::make($input['password']),
'membership_status' =>'pasif',
'membership_type' =>'standard',
'last_activation_date' => Carbon::now(),
'membership_end_date' => Carbon::now(),
'temporary_id' => random_int(1000000, 9999999),
'tc_no_pasaport_no' => $input['tc_no_pasaport_no'],
'place_of_birth' => $input['place_of_birth'],
'date_of_birth' => $input['date_of_birth'],
'educational_status' => $input['educational_status'],
'school_department' => $input['school_department'],
'Institution_and_unit' => $input['Institution_and_unit'],
'address' => $input['address'],
'referance_email' => $input['referance_email'],
'letter_of_Intent'=>$input['letter_of_Intent'],
'created_at' => Carbon::now(),
]);
}
You can use PHP Mailer library to send email in PHP.

Laravel - Call to a member function create() on null

I am trying to save form details into my database, through a controller. When I click submit, I am shown this error:
in ClinicController.php line 58 at
ClinicController->store(object(Request))
Line 58 (See below for the entire function) is:
$request->user()->create([
Controller:
public function store(Request $request)
{
$request->user()->create([
'name' => $request->name,
'email' => $request->email,
'password' => $request->password
]);
$request->user()->clinic()->create([
'clinic_name' => $request->clinic_name,
'telephone' => $request->telephone,
'address_1' => $request->address_1,
'address_2' => $request->address_2,
'city' => $request->city,
'postcode' => $request->postcode
]);
return redirect('/home');
}
User Model:
public function clinic() {
return $this->belongsTo(Clinic::class);
}
Clinic Model:
public function user() {
return $this->belongsTo(User::class);
}
Routes.php:
Route::post('/clinic', 'ClinicController#store');
Any help would be hugely appreciated. Many thanks.
public function store(Request $request)
{
$user = App\User::create([
'name' => $request->name,
'email' => $request->email,
'password' => $request->password
]);
$clinic = App\Clinic::create([
'clinic_name' => $request->clinic_name,
'telephone' => $request->telephone,
'address_1' => $request->address_1,
'address_2' => $request->address_2,
'city' => $request->city,
'postcode' => $request->postcode
])
$user->clinics()->attach($clinic->id);
return redirect('/home');
}
If you want the currently authorized user, just use app()->user(). Also, you can simplify a lot of your logic and make it much easier to read.
auth()
->user()
->create($request->only([
'name',
'email',
'password'
]))
->clinic()
->create($request->only([
'clinic_name',
'telephone',
'address_1',
'address_2',
'city',
'postcode'
]));
return redirect('/home');
Change your controller to this:
public function store(Request $request)
{
App\User::create([
'name' => $request->name,
'email' => $request->email,
'password' => $request->password
]);
App\Clinic::create([
'clinic_name' => $request->clinic_name,
'telephone' => $request->telephone,
'address_1' => $request->address_1,
'address_2' => $request->address_2,
'city' => $request->city,
'postcode' => $request->postcode
]);
return redirect('/home');
}
I think problems with $fillable property, make sure to check it.
$user = App\User::create([
'name' => $request->name,
'email' => $request->email,
'password' => $request->password
]);
$user->clinic()->create([
'clinic_name' => $request->clinic_name,
'telephone' => $request->telephone,
'address_1' => $request->address_1,
'address_2' => $request->address_2,
'city' => $request->city,
'postcode' => $request->postcode
]);
return redirect('/home');

Categories