How to define action inside form in laravel? - laravel

I simply want to add an action to a form and I am trying as follows:
{{ Form::open(['action'=> ['AuthController#login'], 'method'=>"POST",'class'=>'login-form']) }}
But I am getting the following error:
Action App\Http\Controllers\AuthController#login not defined. (View: D:\server\htdocs\PMS\resources\views\custom_auth\login.blade.php)
I configure laravel collective Html. Whats wrong in my code?
Update:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use Auth;
class AuthController extends Controller
{
function show(){
return view('custom_auth.login');
}
public function login(Request $request){
print_r($request); exit;
$this->validate($request,[
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
]);
$user_data = array(
'email' => $request->get('email'),
'password' => $request->get('password')
);
if(Auth::attempt($user_data)){
return redirect('/dashboard');
}else{
return back()->with('error','Wrong Credential');
}
}
}

You don't need to put your action inside an array when using Form helper so try:
{{ Form::open(['action'=> 'AuthController#login', 'method'=>"POST",'class'=>'login-form']) }}
And of course, make sure that a public login() method exists inside your AuthController
Also, do not forget to add this in your routes file, routes/web.php:
Route::post('login', 'AuthController#login');
I hope it helps

Related

Assigning the role while user registration Laravel

I'm trying to make a register page with role as a radio button(consumer, supplier, Admin)
but it show me this error when I test the query in postman
Error: Class "App\Http\Models\Role" not found in file
my controller:
public function register(Request $request)
{
$request->validate([
'first_name'=>'required|string',
'last_name'=>'required|string',
'email'=>'required|string|unique:users',
'password'=>'required|string|min:6',
'phone_number'=>'required|string|min:10',
'role_name'=>'required|string'
]);
$role_a = $request->role_name;
if ($role_a == 'صاحب متجر'){
$role=Role::select('role_id')->where('role_name','صاحب متجر')->first();
$user->roles()->attach($role);
return response()->json($user);
}
elseif ($role_a == 'مشتري'){
$role=Role::select('role_id')->where('role_name','مشتري')->first();
$user->roles()->attach($role);
return response()->json($user);
}
$user=User::create([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'email' => $request->email,
'password' => Hash::make($request->password),
'phone_number' => $request->phone_number,
]);
And my use statement:
use Illuminate\Http\Request;
use App\Http\Models\User;
use App\Http\Models\Role;
use Illuminate\Support\Facades\Hash;
And my route:
Route::post('/register','App\Http\Controllers\AuthController#register');
and this what I have in tables:
Note: I didn't use custom packages like spatie for example
Thank you for trying to help!
You miss adding the Request class as an argument into your method. Your method should look like this:
public function register(Request $request)
{
//after validation
$data = $request->validated();
}
Dont forget to add use Illuminate\Http\Request; in your use statement.

Laravel - Auth User returns null

I'm trying to redirect the user to a 'dashboard' page after he has registered. However, when I dump and die the auth()-user(), it returns null. It should display the information of the authenticated user from my database.
Register Controller
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class RegisterController extends Controller
{
public function NavRegPage(){
return view('regPage');
}
public function store(Request $request){
$data = request()->validate([
'username' => ['required', 'max:20'],
'password' => ['required', 'min:5', 'max:20' ],
'email' => ['required', 'email'],
'url' => ['required', 'url'],
'dob' => ['required']
]);
$user = new User();
$user->username = request('username');
$user->password = request('password');
$user->email = request('email');
$user->url = request('url');
$user->dob = request('dob');
$user->save();
auth()->attempt($request->only('email', 'password'));
return redirect()->route('dash');
}
Dash controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashController extends Controller
{
public function index(){
dd(auth()->user());
}
}
Web.php
use App\Http\Controllers\RegisterController as RegisterController;
use App\Http\Controllers\DashController as DashController;
use Illuminate\Support\Facades\Route;
Route::get('/register', [RegisterController::class, 'NavRegPage']) ->name('register');
Route::get('dash', [\App\Http\Controllers\DashController::class, 'index']) ->name('dash');
Route::get('/', function () {
return view('index');
});
Route::post('/register', [RegisterController::class, 'store'])->name('register');
I want the dd to show me the authenticated user information so I can continue with my work.
Thank you
in web.php
Route::get('dash', [\App\Http\Controllers\DashController::class, 'index']) ->name('dash')->middleware('auth');
apply ->middleware('auth'); then your auth() will work on that controller

Class App\Http\Controllers\Auth\registerController does not exist in laravel 5.8

I can't understand what is happening here. on my local server its working fine but when I put everything on my live server I'm having this error
My web Route
Auth::routes(['verify' => true]);
Route::get('/', function(){
return view('auth.login');
})->name('auth.login')->middleware('auth');
Route::resource('/register', 'Auth\registerController#index')->name('register');
Route::group( ['middleware' => 'auth' ], function()
{
Route::get('/home', 'HomeController#index')->name('home');
Route::group(['namespace' => 'dashboard', 'prefix' => 'dashboard'], function() {
Route::get('/', 'DashboardController#index');
});
SO ON...
}
My RegisterController
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
public function index(Request $request)
{
$referral = '';
$keyword = $request->get('search');
$referral = Referral::where([
['code', $keyword],
['status', 0]
])->first();
if (is_null($keyword))
return view ( 'Auth.register');
elseif ($referral)
return view ( 'Auth.register', compact('referral', $referral))
->withDetails ( $referral )
->withQuery ( $keyword );
else
return view ( 'Auth.register')->withMessage ( 'The code you provided is not existing or already been taken.' );
}
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'country_code' => $data['country_code'],
'phone_number' => $data['phone_number'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
registerController Path
what do you this causing this?
Thank you in advance!
this line of code in your web.php file is causing the issue
Route::resource('/register', 'Auth\registerController#index')->name('register');
your class name starts with R but you have written using r. moreover it seems the route is a simple get route but you have written it as resource. change this too. so the route should be like
Route::get('register', 'Auth\RegisterController#index')->name('register');
How did you namespace your registerController?
Because laravel later versions use a rather different namespace than previous versions. Hence change your namespace (and imported files) to this;
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
If you are using laravel 8:
use App\Http\Controllers\Auth\RegisterController;
Auth::routes(['register' => false]);
Route::get('/user_register', [RegisterController::class, 'showRegistrationForm'])->name('register');
However it throws this error when I clean cache:
Unable to prepare route [user_register] for serialization. Another route has already been assigned name [register].
The new route works, but the old route yet it works

Validate method not found - Laravel

Laravel Framework 5.4.35
Contacts Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactEmail;
class ContactsController extends Controller
{
public function index() {
return view('contact.index');
}
public function sendContact (Request $request) {
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'message' => 'required|min:5',
]);
Mail::to('bump#bumpy.net')
->send(new ContactEmail($request));
return redirect('/contact/success');
}
public function success() {
return view('contact.success');
}
}
The Controller that extends:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
When it goes here:
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'message' => 'required|min:5',
]);
I get this output:
(1/1) BadMethodCallException Method validate does not exist
I have the namespace, the classes to be used. The call to the method seems to be ok.
What am I missing?
Care to advise?
If I create a validator instance manually using the Validator facade.
It seems to validate.
You mention your using version 5.4. The method you're using to validats via the request is only from version 5.5.
So you will need to do it like...
$this->validate($request, [
'name' => 'required|min:3',
'email' => 'required|email',
'message' => 'required|min:5',
]);
Hope this helps. Check out the 5.4v docs rather than the, aster/5.5v
https://laravel.com/docs/5.4/validation#validation-quickstart
Laravel 5.4
$this->validate($request, [
Laravel 5.5
$request->validate([

Call to undefined method Illuminate\Http\JsonResponse::validate() in Laravel 5.3

I am implementing a registration form using JSON post request and laravel 5.3 with the below Controller settings
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
$data = $data['Register'];
$validator = Validator::make($data, [
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
if($validator->fails())
{
$errors = $validator->errors()->all()[0];
//dd($errors);
return response()->json(['errors'=>$errors]);
}
else
{
return $validator;
}
}
protected function create(array $data)
{
$data = $data['Register'];
//dd($data);
User::create([
//'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
return response()->json(['success' => $data['email']], 200);
}
}
But i want to track server errors in the event of multiple registration with the same email. I have handled this on the client side but in need to handle on the backend too.
The Problem is with the validator function it keep returning below error
FatalThrowableError in RegistersUsers.php line 31:
Call to undefined method Illuminate\Http\JsonResponse::validate()
I have checked inside the framework code and there is a validate method which seems to be unrecognized with the json response any ideas?

Resources