How to create a user in Laravel? - laravel

I am creating a CMS App where the user should register first.
I created a RegisterController where I define index() method which returns the view register.blade.php!
When the user clicks Register button, the request should pass to create() method alongside /register URL and create a user.
Since, I already defined that /register should open register.blade.php, then how can I run another method to create a user under the same URL?
I also don't want to use php artisan make:controller RegisterController --resource.
RegisterController.php
<?php
namespace App\Http\Controllers\Authentication;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RegisterController extends Controller
{
public function index() {
return view('auth.register');
}
protected function validator(array $data) {
return $data->validate([
'fname' => 'required|string|max:255',
'lame' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
]);
}
protected function create(Request $request) {
$this->validator($request->all)->validate();
}
}
register.blade.php
#extends('master')
#section('title')
Register - CMS APP
#endsection
#section('styles')
<link rel="stylesheet" href="{{ asset('css/auth.css') }}">
#endsection
#section('register')
<form class="form-signin" method="post" accept-charset="utf8" action="/register/create">
#csrf
<img class="mb-4" src="http://logo.kenh.net/logo/bootstrap-4.svg.png" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Please Register</h1>
<label for="inputFirstName" class="sr-only">First Name</label>
<input name="fname" type="string" id="inputFirstName" class="form-control" placeholder="First Name" autofocus value="{{old('fname')}}">
#if($errors->has('fname'))
<div class="alert-danger">
<p>First Name is required</p>
</div>
#endif
<label for="inputLastName" class="sr-only">Last Name</label>
<input name="lname" type="string" id="inputLastName" class="form-control" placeholder="Last Name" autofocus value="{{old('lname')}}">
#if($errors->has('lname'))
<div class="alert-danger">
<p>Last Name is required</p>
</div>
#endif
<label for="inputEmail" class="sr-only">Email address</label>
<input name="email" type="email" id="inputEmail" class="form-control" placeholder="Email address" autofocus value="{{old('email')}}">
#if($errors->has('email'))
<div class="alert-danger">
<p>Email is required</p>
</div>
#endif
<label for="inputPassword" class="sr-only">Password</label>
<input name="password" type="password" id="inputPassword" class="form-control" placeholder="Password">
#if($errors->has('password'))
<div class="alert-danger">
<p>Password is required</p>
</div>
#endif
<button class="btn btn-lg btn-success btn-block" type="submit">Register</button>
<br>
Login
</form>
#endsection
Routes(web.php):
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('register', 'Authentication\RegisterController#index');
Route::get('/login', function() {
return view('auth.login');
})->name('login');

Why not use Laravel Auth which will build the auth database, views and logic for you?
php artisan make:auth
https://laravel.com/docs/5.6/authentication
EDIT:
Ok, so If I understand correctly, you want your post and get pointing to the same route url?
Route::match(array('GET','POST'),'/register', 'RegisterController#index');
{
//
});

You can try the routing in this form:
Route::get('/user', 'UserController#index');
This might work for you.
Also, don't forget to go through the official documentation of Laravel regarding Routing.
https://laravel.com/docs/5.6/routing
This will help you alot.

The form is sending a post request to a certain route which in this case is /register but you have not supplied that route yet.
Route::post('register', 'Authentication\RegisterController#create');
Additionally, your form action can just be action="register"

Related

The POST method is not supported for route register. Supported methods: GET, HEAD Laravel

i am going to make a registation form in laravel when i registered the user and click submit button. i got the error was
The POST method is not supported for route register. Supported methods: GET, HEAD.
what i tried so far i attached below.
Routes
Route::post('register', [RegisterContoller::class])->name('register');
Views
#extends('layout')
#section('content')
<div class="card">
<div class="card-header">Contact Form</div>
<div class="card-body">
<form action= "{{ route('register') }}" method="post">
{!! csrf_field() !!}
<label>First Name</label>
<input type="text" name="fname" id="fname" class ="form-control"> </br>
<label>Last Name</label>
<input type="text" name="lname" id="lname" class ="form-control"> </br>
<label>Email</label>
<input type="email" name="email" id="email" class ="form-control"> </br>
<label>Password</label>
<input type="password" name="password" id="password" class ="form-control"> </br>
<input type="submit" value="Save" class="btn btn-success">
</form>
</div>
</div>
#stop
Contoller
class RegisterContoller extends Controller
{
public function create()
{
return view('contact.create');
}
public function store(Request $request)
{
$input = $request->all();
Register::Create($input);
return view('contact.thanks');
}
}
Model
class Register extends Model
{
protected $table = "register";
protected $primarykey = "id";
protected $fillable = ["fname","lname","email","password"];
use HasFactory;
}
routes images
There isn't any method called register. store method is where you call the Register::Create method, passing it the input from form.
The route has to be like this:
Route::post('/register', [RegisterContoller::class, 'store'])->name('register');
EDIT:
Running the following command should work:
php artisan route:cache
php artisan route:clear
These will clear the routes cache.

Laravel validate method does not prevent user from validating

I don't have any errors, nor on client or server. To give you more context I'm creating a login form, I'm trying to prevent the user from authenticate himself if the text he typed on the input fields are corrects. If it's not correct, the user should not be redirected to the route (which has the name "authenticate").
there are 3 routes: login and home which are pages, and authenticate that's redirecting to a controller which got all the functions related to the login.
AuthController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AuthController extends Controller
{
public function login() {
return view('auth.login');
}
public function authenticate(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required'
]);
}
}
Login view
<form action="{{ route('authenticate') }}" method="post">
#csrf
<input type="email" name="email">
<input type="password" name="password">
<button class="submit">Log</button>
</form>
All the routes
Route::get('home', HomeController::class)
->name('home');
Route::get('login', [AuthController::class, 'login'])
->name('login');
Route::get('authenticate', [AuthController::class, 'authenticate'])
->name('authenticate');
So basically what I'm trying to do is to check if the text the user has typed into the email field is an email, and if all the fields have text in it, because everything is required. But it seems like it's not the case, even if the fields are empty the log in button redirects to the authenticate page. Did I do something wrong? If I didn't provide enough information I would be glad to provide more, thanks for reading :) have a nice day
add required field to your input tag
#if(Session::has('success'))
<div class="alert alert-success alert-dismissible">
{{Session::get('success')}}
</div>
#elseif(Session::has('failed'))
<div class="alert alert-danger alert-dismissible">
{{Session::get('failed')}}
</div>
#endif
<div class="form-group">
<label for="email" class="form-label">Email</label>
<input type="email" name="email" class="form-control" value="{{old('email')}}" required />
{!!$errors->first("email", "<span class='text-danger'>:message</span>")!!}
</div>
Try this...

How to develop custom login by auth middleware using laravel

I want to make a custom login system by auth middleware.
I don't have an idea how to do that.
controller
public function dologin(Request $request)
{
$request->validate([
'username' => 'required',
'password' => 'required'
]);
$username = $request->username;
$password = $request->password;
if (Auth::attempt(['username' => $username, 'password' => $password])) {
}
}
Blade view
<form action="{{route('login.action')}}">
<br>
<div class="form-row">
<div class="col">
<label>USERNAME</label>
<input type="text" class="form-control" placeholder="Enter Username" name="username">
</div>
</div>
<!--form-row-->
<br>
<div class="form-row">
<div class="col">
<label>PASSWORD</label>
<input type="password" class="form-control" placeholder="Enter password" name="password">
</div>
</div>
<!--form-row-->
<br>
<div class="form-group">
<input type="submit" name="btnsubmit" class="btn btn-success col-md-3" id="signup-btn" value="Login">
</div>
</form>
Route
// Login
Route::get('/login', 'LoginController#create')->name('login');
Route::get('/login/action', 'LoginController#dologin')->name('login.action');
Create your LoginController by runing this command in terminal:
php artisan make:controller LoginController
Then find your newly created controller under App\Http\Controllers\ and add a the use of Auth at the top of your controller
use Illuminate\Support\Facades\Auth;
Then add your dologin function to the controller and you're ready to go.

Laravel undefined variable in view, Laravel 5.2 using AdminLTE

I am getting an error in laravel 5.2
when I access it using the storeMessage function on my Controller
This is my ERROR STATEMENT:
ErrorException in 6e12af76911d23528f6e0ea587ccab13391eacf9.php line 37:
Undefined variable: thisPage (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php) (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php) (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php)
And this is my code:
View Code
<?php $thisPage = "jenisbeasiswa"; ?>
#extends('layouts.app')
#section('htmlheader_title')
Portal Beasiswa
#endsection
#section('contentheader_title')
Broadcast Beasiswa via Telegram Messenger
#endsection
#section('main-content')
<form action="{{ url('/send-message') }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" id="message" class="form-control" placeholder="Enter your query" rows="10"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
#endsection
and this is my Controller Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Telegram\Bot\FileUpload\InputFile;
use Telegram\Bot\Laravel\Facades\Telegram;
class TelegramBotController extends Controller
{
public function updatedActivity()
{
$activity = Telegram::getUpdates();
dd($activity);
}
public function sendMessage()
{
return view('beasiswa.telegram');
}
public function storeMessage(Request $request)
{
$request->validate([
'email' => 'required|email',
'message' => 'required'
]);
$text = "[HEADLINE] Informasi Portal Beasiswa\n"
. "<b>Email Address: </b>\n"
. "$request->email\n"
. "<b>Message: </b>\n"
. $request->message;
Telegram::sendMessage([
'chat_id' => env('TELEGRAM_CHANNEL_ID', ''),
'parse_mode' => 'HTML',
'text' => $text
]);
return redirect()->back();
}
}
Apparently sidebar.blade.php is referring to the $thisPage variable (which is declared at the top of your view code). My guess is, you are including the sidebar in the layouts.app template. But as you extend your layouts.app the $thisPage variable will get rendered in the sidebar first before the declaration comes in your view code. And thus, it will be undefined in the Sidebar.
So, you should try another approach. Why do you even want to set the $thisPage variable? Maybe you can pass it in from the controller?

ReflectionException in Route.php line 280: Method App\Http\Controllers\UserController::Signup() does not exist

I am a new programmer on laravel. I am currently using laravel 5.2. I ran into this error while i was trying to input data into a form i created as welcome.blade.php. I have check the route and it seems very okay. I dont what i may be doing wrong. This is problem display
ReflectionException in Route.php line 280:
Method App\Http\Controllers\UserController::Signup() does not exist
UserController
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function postSignUp(Request $request)
{
$email = $request['email'];
$first_name = $request['first_name'];
$password = bcrypt($request['password']);
$user = new User();
$user->email = $email;
$user->first_name = $first_name;
$user->password = $password;
$user->save();
return redirect()->back();
}
public function postSignIn(Request $request)
{
}
}
Routes
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
Route::post('/signup', [
'uses' => 'UserController#Signup',
'as' => 'signup',
]);
});
welcome.blade.php
#extends('layouts.master')
#section('title')
Welcome!
#endsection
#section('content')
<div class="row">
<div class="col-md-6">
<h2>Sign Up</h2>
<form action="{{route('signup')}}" method="post">
<div class="form-group">
<label for="email">Your Email</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="first_name">Your First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>
<div class="form-group">
<label for="password">Your Password</label>
<input class="form-control" type="text" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
</div>
<div class="col-md-6">
<h2>Sign In</h2>
<form action="#" method="post">
<div class="form-group">
<label for="email">Your Email</label>
<input class="form-control" type="text" name="email" id="email"></div>
<div class="form-group">
<label for="password">Your Password</label>
<input class="form-control" type="text" name="password" id="password"></div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
#endsection
You're using diffrent function name according to route, both will be same.
Replace your function name with this:-
postSignUp to SignUp
Thanks for your contribution as suggested i change the postSignUp to SignUp in the userController
public function postSignUp(Request $request)
change to
public function SignUp(Request $request)
also in the route I change the
'uses' => 'UserController#Signup',
change to
'uses' => 'UserController#SignUp',

Resources