Laravel User Registration Issue - php

I am trying to make a simple user registration form using laravel4. I am currently getting this error when I submit the form:
Symfony\Component\Debug\Exception\FatalErrorException thrown with message "Undefined class constant 'rules'"
Stacktrace:
#1 Symfony\Component\Debug\Exception\FatalErrorException in C:\wamp\www\growinlove.tld\app\controllers\UsersController.php:12
#0 Illuminate\Exception\Handler:handleShutdown in <#unknown>:0
This is my Controller:
class UsersController extends BaseController {
//Run when user visits site...Load a view
public function index(){
return View::make('home.index');
}
//Handles processing of registration form data
public function postCreate(){
$validator = Validator::make(Input::all(), User::rules);
if($validator->passes()){
//Save in DB - Success
$user = new User;
$user->fname = Input::get('fname'); //Get the details of form
$user->lname = Input::get('lname');
$user->email = Input::get('email');
$password->password = Hash::make(Input::get('password'));//Encrypt the password
$user->save();
return Redirect::to('/books')->with('Thank you for Registering!');
}else{
//Display error - Failed
return Redirect::to('/')->with('message', 'The Following Errors occurred')->withErrors($validator)->withInput();
}
}
}
This is my View:
<!-- Register Form -->
<form action="{{ action('UsersController#postCreate') }}" method="post" role="form">
<h2 class="form-signup-heading">Register</h2>
<!-- Display Errors -->
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
<!-- First Name -->
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" name="fname" />
</div>
<!-- Last Name -->
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" name="lname" />
</div>
<!-- Email -->
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" />
</div>
<!-- Password-->
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" />
</div>
<!-- Confirm Password -->
<div class="form-group">
<label>Confirm Password</label>
<input type="password" class="form-control" name="password_confirmation" />
</div>
<input type="submit" value="Register" class="btn btn-primary"/>
</form>
This is my Model:
public static $rules = array(
'firstname'=>'required|alpha|min:2',
'lastname'=>'required|alpha|min:2',
'email'=>'required|email|unique:users',
'password'=>'required|alpha_num|between:6,12|confirmed',
'password_confirmation'=>'required|alpha_num|between:6,12'
);
use UserTrait, RemindableTrait;

The problem is at the line 12 of UserController.
Instead User::rules use User::$rules.

Replace in your controller
$validator = Validator::make(Input::all(), User::rules);
with
$validator = Validator::make(Input::all(), User::$rules);

Related

Laravel expiring session after submitting any form

When I am submitting my forms in laravel, the user session expires and it logs out the current user. I thought it was a csrf token problem, so I disabled its verification but the problem persists.
My view
<form action="{{url('/save-user-details')}}" method="POST" enctype="multipart/form-data"
>
#csrf
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Primeiro Name</label>
<input type="text" class="form-control"
name="firstName" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Apelido</label>
<input type="text" class="form-control" name="lastName" required>
</div>
</div>
</div>
<button type="submit" class="button pull-right" value="">Actualizar Perfil</button>
</form>
My Route list
Route::post('/save-user-details', 'DashboardController#saveUser');
Route::get('/editar-usuario', 'DashboardController#editProfile');
My Controller
public function __construct()
{
$this->middleware('auth');
}
public function saveUser(Request $request){
$user_id = auth()->user()->id;
$user = User::find($user_id);
$detalhes = new Detalhesuser;
$detalhes->user_id = $user_id;
$detalhes->firstName = $request->input('firstName');
$detalhes->lastName = $request->input('lastName');
$detalhes->profissao = $request->input('profissao');
$detalhes->instituicao = $request->input('instituicao');
$detalhes->biografia = $request->input('biografia');
$detalhes->save();
$user->detalhesUser_id = $detalhes->id;
$user->save();
return redirect('/dashboard')->with('success', 'Detalhes salvos');
}
My sessions are being stored in a file. I tried to change it to database, the problem still persisted.
Any idea where I may be doing this wrong?

Use of undefined constant title - assumed 'title'

I am pretty new in Laravel,
actually I am trying to create a crud operation using Laravel 5.6, so I have created create, delete function successfully, but on update function I am getting an error
Please find the attched image for detailed error
Use of undefined constant title - assumed 'title' (this will throw an Error in a future version of PHP)
Controller
public function edit($id){
$blogCategories = BlogCategories::find($id);
if (empty($blogCategories)) {
Flash::error('Category not found');
return redirect(route('categories.index'));
}
return view('cms/BlogCategories/editCategory')->with('blogCategories', $blogCategories);
}
public function update(Request $request, $id){
$blogCategories = BlogCategories::find($id);
$blogCategories->title = $request->get(title);
$blogCategories->slug = $request->get(slug);
$blogCategories->description = $request->get(description);
$blogCategories->featured_image = $request->get(featured_image);
$blogCategories->save();
return redirect()->back();
}
Model
class BlogCategories extends Model{
protected $fillable = ['title', 'slug', 'description', 'featured_image'];
protected $guarded = [];
}
Form
<form action="{{route('categories.update', $blogCategories->id)}}" method="post" class="m-form m-form--fit m-form--label-align-right">
#csrf
#method('put')
<div class="m-portlet__body">
<div class="form-group m-form__group">
<label>Title</label>
<input type="text" class="form-control m-input" name="title" id="title" value="{{$blogCategories->title}}" aria-describedby="emailHelp" placeholder="Muhammad Owais">
</div>
<div class="form-group m-form__group">
<label>slug</label>
<input type="text" class="form-control m-input" name="slug" id="slug" value="{{$blogCategories->slug}}" aria-describedby="emailHelp" placeholder="mail#domain.com">
</div>
<div class="form-group m-form__group">
<label>Description</label>
<textarea class="form-control" name="description" id="description" value="{{$blogCategories->description}}" placeholder="Enter Description"></textarea>
</div>
<div class="form-group m-form__group">
<label>Featured Image</label>
<input type="text" class="form-control m-input" name="featured_image" id="featured_image" value="{{$blogCategories->featured_image}}" aria-describedby="emailHelp" placeholder="Enter Amazon S3 URL">
</div>
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions">
<button type="submit" class="btn btn-primary">
Submit
</button>
<button type="reset" class="btn btn-secondary">
Cancel
</button>
</div>
</div>
</form>
Use quotes for your all HTTP requests like :
$request->get('title');
$request->get('slug');

error in update file - Trying to get property of non-object, when there is no session

I have a form for the user edit his profile account. So it appears for each field the value if there is a value for each field with: "value="{{$user->name}}". But sometimes is appearing this error:
Trying to get property of non-object
Do you know how to correct the issue?
<form method="post" action="{{route('user.update')}}">
{{csrf_field()}}
<div>
<label for="name">Name</label>
<input type="text" value="{{$user->name}}" name="name" class="form-control" id="name">
</div>
<div>
<label for="surname">Surname</label>
<input type="text" value="{{$user->surname}}" name="surname" class="form-control" id="surname">
</div>
<!-- other fields -->
<input type="submit" value="Update"/>
</form>
The update method:
public function updateGeneralInfo(Request $request){
$this->validate($request, [
'name' => 'required',
]);
$user = Auth::user();
$user->name = $request->name;
...
$user->save();
return redirect()->back();
}
In your controller you can do a check before you return the view:
if(Auth::check()){
//return view and other stuff
}
else {
//redirect to login
}
In your blade:
<form method="post" action="{{route('user.update')}}">
{{csrf_field()}}
<div>
<label for="name">Name</label>
<input type="text" value="{{auth()->user()->name}}" name="name" class="form-control" id="name">
</div>
<div>
<label for="surname">Surname</label>
<input type="text" value="{{auth()->user()->surname}}" name="surname" class="form-control" id="surname">
</div>
<!-- other fields -->
<input type="submit" value="Update"/>
</form>

i have a form in laravel when i click on submit button it says token mismatch [duplicate]

This question already has answers here:
TokenMismatchException in VerifyCsrfToken.php Line 67
(36 answers)
Closed 6 years ago.
when I click on submit button it gives an error token mismatch, I can't understand why it gives me this error
here is my rout
Route::get('admin-login', array('uses' => 'adminController#showlogin'));
// route to process the form
Route::post('admin-login', array('uses' => 'adminController#dologin'));
my controller
public function dologin(){
$uname = Input::get('username');
$password = Input::get('password');
if (Auth::attempt(array('username' => $uname, 'password' => $password))){
return "success";
}
else {
return "Wrong Credentials";
}
}
my form
<form role="form" method="post" action="">
<input name="_token" hidden value="{{ csrf_token() }} " />
<div class="form-group">
<label for="username">UserName: </label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">Password: </label>
<input type="password" class="form-control" id="password" name="password">
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
here is error
TokenMismatchException in VerifyCsrfToken.php line 53:
Please add {!! csrf_field() !!} inside your <form>.
<form role="form" method="post" action="">
{!! csrf_field() !!}
<div class="form-group">
<label for="username">UserName: </label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">Password: </label>
<input type="password" class="form-control" id="password" name="password">
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
Don't use the Input facade. Better pass the $request object like this:
public function dologin(Request $request)
{
$uname = $request->username;
$password = $request->password;
if (Auth::attempt(array('username' => $uname, 'password' => $password))){
return "success";
}
else {
return "Wrong Credentials";
}
}
Keep in mind to put use Illuminate\Http\Request; on top of your class.

How to show errors in client side in Laravel

How can I show errors in client side with Laravel controller in Laravel 4.2?
I want to show this error:
D:\xampp\htdocs\guestlara\app\views\index.php(51): Illuminate\Exception\Handler->handleError(8, 'Undefined varia...', 'D:\xampp\htdocs...', 51, Array)
public function login_user() {
$rules = array(
'email'=> 'Required|Between:3,64|Email|Unique:users',
'password'=>'Required|AlphaNum|Between:4,8|Confirmed',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
log::error($messages);
// redirect our user back to the form with the errors from the validator
return Redirect::to('/')->withErrors($validator);
} else {
// attempt to do the login
if (Auth::attempt($users)) {
return Redirect::to('dashboard');
} else {
return Redirect::to('/');
}
}
}
This is the HTML code:
<form action="login_user" method="post">
<div class="alert alert-info">
Message
</div>
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" name="email" placeholder="Enter Email Address" />
<div class="error"><?php echo $messages; ?></div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" name="password" placeholder="Enter Password" />
<div class="error"><?php echo $messages; ?></div>
</div>
<input type="submit" name="submit" value="Login" class="btn btn-default">
<input type="button" name="register" value="Register" class="btn btn-default">
<input type="button" name="forgot-password" value="forgot-password" class="btn btn-default">
</form>

Categories