Hello i am trying to create a UI with fname and lname and i want it to be added to my mysql db table.
following are my codes for reference.
**register1.blade.php**
#extends('master')
#section('content')
<div class="row">
<div class="span4 offset1">
<div class="well">
<legend>Please Register Here</legend> <br/>
{{ Form::open(array('url' => 'register1')) }}
#if($errors->any())
<div class="alert alert-error">
×
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
</div>
#endif
First Name:{{ Form::text('fname', '', array('placeholder' => 'First Name')) }}<br>
Last Name:{{ Form::text('lname', '',array('placeholder' => 'Last Name')) }}<br>
{{ Form::submit('Submit', array('class' => 'btn btn-success')) }}
<!-- {{ HTML::link('register', 'Sign Up', array('class' => 'btn btn-primary')) }}-->
{{ Form::close() }}
</div>
</div>
</div>
#stop
**HomeController.php**
public function getRegister1()
{
return View::make('home.register1');
}
public function postRegister1()
{
$input = Input::all();
$rules = array('fname' => 'required', 'lname' => 'required');
$v = Validator::make($input, $rules);
if($v->fails())
{
return Redirect::to('register1')->withErrors($v);
} else {
$credentials = array('fname' => $input['fname'], 'lname' => $input['lname']);
if(Auth::attempt($credentials))
{
return Redirect::to('admin');
} else {
return Redirect::to('register1');
}
}
}
**in models-> register.php**
<?php
use Illuminate\Auth\RegisterInterface;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class register extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'register1';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* #return string
*/
public function getFirstName()
{
return $this->fname;
}
/**
* Get the e-mail address where password reminders are sent.
*
* #return string
*/
public function getLastName()
{
return $this->lname;
}
**routes.php**
Route::get('register1','HomeController#getRegister1');
Route::post('register1','HomeController#postRegister1');
when i execute registration it gives me text boxes and submit. onclick of submit i have the following error
**illuminate \ Database \ QueryException**
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fname' in 'where clause' (SQL: select * from `users` where `fname` = sonu and `lname` = k limit 1)
please help me out with this issue.
It's throwing error because there is no fname column in users table. You should check your database structure, and make sure fname column exist.
Assuming you have a register table with all the necessary columns, you would need to tell your app to use that table for authentication. So in `app/config/auth.php', depending on which driver you're using, you'll need entries like the following:
'model' => 'register',
if you're using the 'eloquent' driver, or
'table' => 'register1'
if you're using the database driver.
Also, for what little it's worth, conventionally the class names for models are CamelCase and the table names are lowercase and plural. So, in your case, class Register .... and protected $table = registers;.
If the above doesn't get you up and running, maybe try looking through this tutorial: http://laravelbook.com/laravel-user-authentication/.
Related
So I'm trying to upload an image on update() function and it keeps giving me "C:\xampp\tmp\php38A9.tmp" file does not exist or is not readable." error. Following is my code:
EditForm.blade.php (Form with the Image Input):
{!! Form::model(Auth::user(),array('route'=>['profile.update',Auth::user()->id],'method'=>'PUT','files'=>'true')) !!}
<div class="form-group form-row">
<div class="col">
{!! Form::text('fname',null,['class'=>'form-control','placeholder'=>'Enter First Name']) !!}
</div>
<div class="col-5">
<div class="custom-file">
{!! Form::file('img',['class'=>'custom-file-input']) !!}
{!! Form::label('Choose Avatar',null,['class'=>'custom-file-label']) !!}
</div>
</div>
</div>
<div class="form-group">
{!! Form::text('lname',null,['class'=>'form-control','placeholder'=>'Enter Last Name']) !!}
</div>
<div class="form-group">
{!! Form::email('email',null,['class'=>'form-control','placeholder'=>'Enter Email']) !!}
</div>
<div class="form-group">
{!! Form::password('password',['class'=>'form-control','placeholder'=>'Enter Student Password']) !!}
</div>
<div class="form-group">
{!! Form::text('name',null,['class'=>'form-control','placeholder'=>'Enter Student Username']) !!}
</div>
<div class="form-group">
{!! Form::number('rollno',null,['class'=>'form-control','placeholder'=>'Enter Roll Number']) !!}
</div>
<div class="form-group">
{!! Form::select('class', [
'1st' => '1st',
'2nd' => '2nd',
'3rd' => '3rd',
'4th' => '4th',
'5th' => '5th',
'6th' => '6th',
'7th' => '7th',
'8th' => '8th',
'9th' => '9th',
'10th' => '10th',],
null, ['class'=>'custom-select','placeholder' => 'Choose Student Class']); !!}
</div>
<div class="form-group py-4">
{!! Form::submit('Create',['type'=>'submit','class'=>'btn btn-danger btn-block']) !!}
</div>
{!! Form::close() !!}
ProfileController.php:
class ProfileController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$users = User::all();
return view('myprofile',compact('users'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit(User $user)
{
$user = User::all();
return view('editprofile',compact('user'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(User $user, FileRequest $request)
{
if($request->hasfile('img')){
//getting the file from view
$image = $request->file('img');
$image_size = $image->getClientSize();
//getting the extension of the file
$image_ext = $image->getClientOriginalExtension();
//changing the name of the file
$new_image_name = rand(123456,999999).".".$image_ext;
$destination_path = public_path('/images');
$image->move($destination_path,$new_image_name);
//saving file in database
$user->image_name = $new_image_name;
$user->image_size = $image_size;
$user->save();
}
$user = Auth::user()->update($request->only(
'fname',
'lname',
'name',
'email',
'password',
'rollno',
'class',));
return redirect()->route('profile.index');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
FileRequest.php (Request to validate file types):
class FileRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'img' => 'mimes:jpeg,gif,png'
];
}
}
Edit Button (This is the button that the user clicks to get to EditProfile.blade.php):
{{ link_to_route('profile.edit','Edit Profile',[Auth::user()->id],['class'=>'btn btn-danger btn-block']) }}
So, when I upload the image and click Edit, it just gives me the error (I've attached a pictue of the error for everyone to see). Please let me know what I'm doing wrong here. Feel free to ask me to show more code if required.
I recently faced this problem and to fix this i used below method.
First go to your config/filesystems.php and inside disks array replace the local with below
'local' => [
'driver' => 'local',
'root' => public_path(),
],
Nad then in controller you can use it like below
if ($request->img) {
$file = $request->File('img');
$ext = $user->username . "." . $file->clientExtension();
$file->storeAs('images/', $ext);
$user->image_name = $ext;
}
I've been facing this problem for a while, for some reason, the below fix helps me. If you're on windows, it could be because of symlink problem. Try this:
php artisan config:cache
php artisan storage:link it doesn't matter if you've already linked it. These commands and then try re-uploading it again. I hope this helps.
I am trying to edit the entry into the table, but every time I click my edit button to take me to the edit page it gives me the error:
Trying to get property 'course_code' of non-object
Below is my code:
CourseController.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Course;
class CoursesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// $courses = Course::all();
// $courses = Course::orderBy('title', 'desc')->get();
$courses = Course::orderBy('course_code', 'desc')->paginate();
return view('subs.index')->with('courses', $courses);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('subs.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'course_code' => 'required',
'course_name' => 'required'
]);
$course = new Course;
$course->course_code = $request->input('course_code');
$course->course_name = $request->input('course_name');
$course->save();
return redirect('/subs')->with('success', 'New course added!');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($course_code)
{
$course = Course::find($course_code);
return view('subs.show')->with('course', $course);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($course_code)
{
$course = Course::find($course_code);
return view('subs.edit')->with('course', $course);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $course_code)
{
$this->validate($request, [
'course_code' => 'required',
'course_name' => 'required'
]);
$course = Course::find($course_code);
$course->course_code = $request->input('course_code');
$course->course_name = $request->input('course_name');
$course->save();
return redirect('/subs')->with('success', 'Course Updated!');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
show.blade.php:
#extends('layouts.app')
#section('content')
Go back
<div class="container">
<h1>{{$course->course_code}}</h1>
<h3>{{$course->course_name}}</h3>
</div>
Edit</td>
#endsection
edit.blade.php:
#extends('layouts.app')
#section('content')
Go back
<div class="container" style="width:50%; margin-left:350px;margin-top:20px">
<h1>Edit Course</h1>
{!! Form::open(['action' => ['CoursesController#update', $course->course_code], 'method'=>'POST']) !!}
<div class="form-group">
{{Form::label('course_code', 'Course code')}}
{{Form::text('course_code', $course->course_code,['placeholder'=>'Course code', 'class'=>'form-control col-md-3'])}}
</div>
<div class="form-group">
{{Form::label('course_name', 'Course name')}}
{{Form::text('course_name', $course->course_name,['placeholder'=>'Course name', 'class'=>'form-control col-md-7'])}}
</div>
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('Add Course',['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
</div>
#endsection
my model code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
//
}
Where did I go wrong and how can I fix this error?
The ::find() method will only return objects with a matching primary key, you cannot pass other values. If not object is found with this primary key, null will be returned and you can't get a property from null.
Either change your primary key, or better, update your query:
$course = Course::where('course_code', $course_code)->first();
It's also a good idea to add a check if the query actually returned something. Just to be sure to avoid the same error in the future:
if ($course === null) {
abort(404); // Show 404 page if course does not exist
}
If you change the primary key, don't forget to update the following properties on your Course model.
// The column name of your primary key.
protected $primaryKey = 'your_key_name';
// Indicating that the primary key is not a number.
public $incrementing = false;
I think here course_code is not primary key..so you can't get it by using find
Two ways.
1st passing your primary key(**id**) & update
2nd using where
$course = Course::where('course_code',$course_code)->first();
then update.
here you want to change the value of course_code also.so 1st one is correct for you passing id & use find & update that
You are using the wrong value to generate links for your model. You need to be using the id not course_code. The find method will use the primary key of the model to do its search.
Edit
Now you are passing the correct value for the parameter so you can use find on the model. If you want to use a different value other than id you can do that but you have to adjust your query to find it by a different field.
If you keep using course_code instead of id you just have to search based on that field:
$course = Course::where('course_code', $course_code)->first();
I have been following the tutorial on http://laravelcode.com/post/how-to-integrate-paypal-payment-gateway-in-laravel-54
I'm now trying to use this in an an application i'm developing but i'm quite new to PHP/Laravel so trying to get these paypal functions into my own Controller/forms that I have already built.
I have a controller "BookingsController" which has a form on ../bookings/create that when the EU presses the submit button it will enter the info into the DB, which works perfect, and then runs the PayPal bits to take the EU to the PayPal checkout which is where i've come unstuck.
My Controller:-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Market;
use App\Stall;
use App\Booking;
use Auth;
use GuzzleHttp\Transaction;
class BookingsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$market = Market::where('is_active', true)->orderBy('name')->pluck('name','id');
$stall = Stall::pluck('name','id')->all();
return view ('bookings.create', compact('market','stall'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'email',
'address' => 'required',
'phone' => 'required',
]);
//Uncomment the below line to test form values
// return $request->all();
$booking = new Booking;
$booking->name = $request->input('name');
$booking->email = $request->input('email');
$booking->address = $request->input('address');
$booking->phone = $request->input('phone');
$booking->market_id = $request->input('market_id');
$booking->stall_id = $request->input('stall_id');
$booking->itemtype = $request->input('itemtype');
$booking->clothesrail = $request->input('clothesrail');
$booking->businessname = $request->input('businessname');
$booking->insurance = $request->input('insurance');
//Get the stall cost
//$stallPrice = Stall::pluck('cost')->where('id', '=', $booking->stall_id);
$stallPrice = 15;
//Check if the user is logged in. If so then submit the user_id
if (Auth::check())
{
$booking->user_id = auth()->user()->id;
}
//return $stallPrice;
//$booking->save();
//Redirect user based on logged in session_status
if (Auth::check())
{
return redirect('/dashboard')->with('success', 'Stall Booked!');
}
return redirect('/confirm')->with('success', 'Stall Booked!');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$booking = Booking::find($id);
return view('bookings.edit')->with('booking', $booking);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'email' => 'email',
'address' => 'required',
'phone' => 'required',
]);
$booking = Booking::find($id);
$booking->name = $request->input('name');
$booking->email = $request->input('email');
$booking->address = $request->input('address');
$booking->phone = $request->input('phone');
$booking->market_id = $request->input('market_id');
$booking->stall_id = $request->input('stall_id');
$booking->itemtype = $request->input('itemtype');
$booking->clothesrail = $request->input('clothesrail');
$booking->businessname = $request->input('businessname');
$booking->insurance = $request->input('insurance');
//Check if the user is logged in. If so then submit the user_id
if (Auth::check())
{
$booking->user_id = auth()->user()->id;
}
$booking->save();
return redirect('/admin/dashboard')->with('success', 'Booking Updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$booking = Booking::find($id);
$booking->delete();
return redirect('/admin/dashboard')->with('success', 'Booking Removed');
}
}
My View with the form:-
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Book a Stall</div>
<div class="alert alert-info" role="alert">Clothes Rails are not provided. You will need to provide this yourself.</div>
<div class="panel-body">
{!!Form::open(['action' => 'BookingsController#store','method' => 'POST'])!!}
#if (Auth::check())
{{Form::bsText('name',Auth::user()->name)}}
{{Form::bsText('email',Auth::user()->email)}}
{{Form::bsText('address',Auth::user()->address)}}
{{Form::bsText('phone',Auth::user()->phone)}}
#else
{{Form::bsText('name','',['placeholder' => 'Name'])}}
{{Form::bsText('email','',['placeholder' => 'Email'])}}
{{Form::bsText('address','',['placeholder' => 'Address'])}}
{{Form::bsText('phone','',['placeholder' => 'Phone'])}}
#endif
<div class="form-group">
{!! Form::label('market_id', 'Date:') !!}
{!! Form::select('market_id', $market , null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('stall_id', 'Type of stall:') !!}
{!! Form::select('stall_id', $stall , null, ['id' => 'stall_id', 'class'=>'form-control'])!!}
</div>
{{Form::bsText('itemtype','',['placeholder' => 'Type of items to sell'])}}
<div class="form-group">
{!! Form::label('clothesrail', 'Clothes Rail?:') !!}
{!! Form::label('clothesrail', 'Yes') !!}
{!! Form::radio('clothesrail', 1, ['class'=>'form-control'])!!}
{!! Form::label('clothesrail', 'No') !!}
{!! Form::radio('clothesrail', 0, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('businessname', 'Business Name:') !!}
{!! Form::text('businessname', null, ['id' => 'businessname', 'class'=>'form-control hidden'])!!}
</div>
<div class="form-group">
{!! Form::label('insurance', 'Public Liability Insurance??:') !!}
{!! Form::label('insurance', 'Yes') !!}
{!! Form::radio('insurance', 1, ['id' => 'insurance', 'class'=>'form-control'])!!}
{!! Form::label('insurance', 'No') !!}
{!! Form::radio('insurance', 0, ['id' => 'insurance', 'class'=>'form-control'])!!}
</div>
{{Form::bsSubmit('Submit')}}
{!! Form::close() !!}
</div>
</div>
</div>
</div>
#endsection
NOTES:
The submit button calls the "store" method in the controller
I can't get the Stall Price to pull from the user's selection into the variable but I'll deal with that later as I can fix that, i'm just working with a fixed value variable to get it working.
Just change:
$stallPrice = Stall::pluck('cost')->where('id', '=', $booking->stall_id);
TO
$stallPrice = DB::table('your_stall_table')->find($booking->stall_id)->cost;
Here, you need to your_stall_table with your actual stall table name.
I'm trying to do a Password Reminder in Laravel 4
I've setup the Controller, But keep getting the error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in
'where clause' (SQL: select * from users where users.deleted_at
is null and email is null limit 1
This would be correct as my users' table has the column "user_email" not "email
Is their any particular way that I can change the query that Laravel runs, to a new / different where that says user_email instead of email.
My controller is as follows :
class RemindersController extends Controller {
/**
* Display the password reminder view.
*
* #return Response
*/
public function getRemind()
{
return View::make('users/password_remind');
}
/**
* Handle a POST request to remind a user of their password.
*
* #return Response
*/
public function postRemind()
{
switch ($response = Password::remind(Input::only('email')))
{
case Password::INVALID_USER:
return Redirect::back()
->with('error', Lang::get($response));
case Password::REMINDER_SENT:
return Redirect::back()
->with('status', Lang::get($response));
}
}
/**
* Display the password reset view for the given token.
*
* #param string $token
* #return Response
*/
public function getReset($token = null)
{
if (is_null($token)) App::abort(404);
return View::make('password.reset')->with('token', $token);
}
/**
* Handle a POST request to reset a user's password.
*
* #return Response
*/
public function postReset()
{
$credentials = Input::only(
'email',
'password',
'password_confirmation',
'token'
);
$response = Password::reset($credentials, function($user, $password)
{
$user->password = Hash::make($password);
$user->save();
});
switch ($response)
{
case Password::INVALID_PASSWORD:
case Password::INVALID_TOKEN:
case Password::INVALID_USER:
return Redirect::back()
->with('error', Lang::get($response));
case Password::PASSWORD_RESET:
return Redirect::to('/');
}
}
}
Users Model
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait, SoftDeletingTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('user_password', 'remember_token');
protected $dates = ['deleted_at'];
protected $primaryKey = "user_id";
protected $fillable = array('user_email');
public static $rules = array(
'user_firstname' => 'required|alpha',
'user_surname' => 'required|alpha',
'user_email' => 'required|email|unique:users',
'user_password' => 'required',
'user_telephone' => 'required|numeric'
);
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
return $this->user_password;
}
public function getEmail() {
return $this->user_email;
}
public function getReminderEmail() {
return $this->user_email;
}
public function getUserByEmail( $user_email )
{
return $this->where('user_email', '=', $user_email)->first();
}
}
and last but not least, My view :
{{ Form::open(array('url' => 'password/remind')) }}
#if (Session::has('error'))
<p style="color: red;">{{ Session::get('error') }}</p>
#elseif (Session::has('status'))
<p>{{ Session::get('status') }}</p>
#endif
<div class="form-group">
<label>Your Email Address</label>
<input name="user_email" type="email" class="form-control" placeholder="Your Email Address" data-error="Please enter your Email Address" value="{{{ Input::old('user_email') }}}" required>
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
<div class="help-block with-errors"></div>
</div><!-- /.form-group -->
<div class="text-center">
<button type="submit" class="cta">Reset Password</button>
</div><!-- /.text-center -->
{{ Form::close() }}
This question already has answers here:
laravel 4 custom named password column
(4 answers)
Closed 8 years ago.
I have a problem with laravel 4.2 authentication. Auth::attempt() always return false. Hash::check() return false.
I am tired to solve this problem. I read many tutorial and I can't find the solution.
Here are some of my important files:
my auth.php
<?php
return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
my UserModel.php
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model
*
* #var string
*/
protected $table = 'users';
/**
* The primary key used by the model
*
* #var integer
*/
protected $primaryKey = 'UserId';
/**
* The name of the "created at" column
*
* #var string
*/
const CREATED_AT = 'UserCreatedAt';
/**
* The name of the "updated at" column
*
* #var string
*/
const UPDATED_AT = 'UserUpdatedAt';
/**
* The attributes excluded from the model's JSON form
*
* #var array
*/
protected $hidden = array('UserPassword', 'UserRememberToken');
protected $fillable = array(
'UserName',
'UserSurname',
'UserCity',
'UserStreet',
'UserPostalCode',
'UserPostalCity',
'UserDeskPhone',
'UserMobilePhone',
'UserEmail',
'UserPassword',
'UserType',
'UserPermission',
'UserActive'
);
protected $guarded = array('UserId', 'HotelId', 'UserRememberToken', 'UserCreatedAt', 'UserUpdatedAt');
public static $rules = array(
'name'=>'required|alpha|min:2',
'surname'=>'required|alpha|min:2',
'email'=>'required|email|unique:users',
'password'=>'required|alpha_num|between:8,100|confirmed',
'password_confirmation'=>'required|alpha_num|between:8,100'
);
/**
* Get the unique identifier for the user
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user
*
* #return string
*/
public function getAuthPassword()
{
return $this->UserPassword;
}
/**
* Get the e-mail address where password reminders are sent
*
* #return string
*/
public function getReminderEmail()
{
return $this->UserEmail;
}
/**
* Get the remember token for the user
*
* #return string
*/
public function getRememberToken()
{
return $this->UserRememberToken;
}
/**
* Set the remember token for the user
*
* #var string
*/
public function setRememberToken($value)
{
$this->UserRememberToken = $value;
}
/**
* Get the remember token name used by the model
*
* #return string
*/
public function getRememberTokenName()
{
return 'UserRememberToken';
}
/**
* Get the user type
*
* #return integer
*/
public function getUserType()
{
return 'UserType';
}
}
my UserController.php
<?php
class UserController extends BaseController {
/*
|--------------------------------------------------------------------------
| User Controller
|--------------------------------------------------------------------------
*/
/**
* UserController's constructor
*/
public function __construct() {
$this->beforeFilter('csrf', array('on'=>'post'));
$this->beforeFilter('auth', array('only'=>array('getBackend')));
}
/**
* Show register page for the user
*/
public function getRegister()
{
return View::make('app.user.register');
}
/**
* Action after pressing the register button
*/
public function postCreate() {
$validator = Validator::make(Input::all(), User::$rules);
if ($validator->passes()) {
// validation has passed, save user in DB
$user = new User;
$user->UserName = Input::get('name');
$user->UserSurname = Input::get('surname');
$user->UserEmail = Input::get('email');
$user->UserPassword = Hash::make(Input::get('password'));
$user->save();
return Redirect::to('user/login')->with('message', 'Dodano użytkownika!');
} else {
// validation has failed, display error messages
return Redirect::to('user/register')
->with('message', 'Pojawiły się następujące błędy:')
->withErrors($validator)
->withInput();
}
}
/**
* Show login page for the user
*/
public function getLogin()
{
// Check if we already logged in
if (Auth::check())
{
// Redirect to backend homepage
return Redirect::to('backend')->with('message', 'Jesteś zalogowany!');
}
return View::make('app.user.login');
}
/**
* Action after pressing the login button
*/
public function postLogin()
{
// Get all the inputs
$data = array(
'UserEmail' => Input::get('email'),
'UserPassword' => Input::get('password')
);
// Declare the rules for the form validation
$rules = array(
'UserEmail' => 'required|email|min:6',
'UserPassword' => 'required|between:8,100'
);
// Declare error message for the rules for the form validation
$messages = array(
'UserEmail.required' => 'Adres e-mail nie może być pusty!',
'UserEmail.email' => 'Adres e-mail jest nieprawidłowy!',
'UserEmail.min' => 'Adres e-mail musi mieć minimum 6 znaków!',
'UserPassword.required' => 'Hasło nie może być puste!',
'UserPassword.between' => 'Hasło musi mieć od 8 do 100 znaków!'
);
// Validate the inputs
$validator = Validator::make($data, $rules, $messages);
// Check if the form validates with success
if ($validator->passes())
{
// Try to log the user in
if (Auth::attempt($data))
{
// Redirect to backend homepage
return Redirect::to('backend');
}
else
{
// Redirect to the login page
return Redirect::to('user/login')
->withErrors('Twój adres e-mail lub hasło jest nieprawidłowe!')
->withInput(Input::except('password'));
}
}
// Something went wrong
return Redirect::to('user/login')
->withErrors($validator)
->withInput(Input::except('password'));
}
/**
* Show the profile for the given user
*/
public function getProfile($id)
{
$user = User::find($id);
return View::make('app.user.profile', array('user' => $user));
}
/**
* Show backend homepage
*/
public function getBackend()
{
return View::make('app.backend.start');
}
}
my login.blade.php
#extends('app.user.master')
#section('title')
{{ 'Logowanie' }}
#stop
#section('content')
<div class="container-fluid">
<div id="page-login" class="row">
<div class="col-xs-12 col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3">
{{--
<div class="text-right">
Need an account?
</div>
--}}
<div class="box">
<div class="box-content">
{{ Form::open(array('url'=>'user/login', 'class'=>'form-signin')); }}
<div class="text-center">
<h3 class="page-header">{{ Config::get('app.name') }} - logowanie</h3>
</div>
#if($errors->has())
<div class="form-group">
<ul>
#foreach ($errors->all() as $error)
<li class="alert">{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="form-group">
<label class="control-label">E-mail</label>
{{ Form::text('email', Input::old('email'), array('class'=>'form-control', 'placeholder'=>'E-mail')) }}
</div>
<div class="form-group">
<label class="control-label">Hasło</label>
{{ Form::password('password', array('class'=>'form-control', 'placeholder'=>'Hasło')) }}
</div>
<div class="text-center">
{{ Form::submit('Zaloguj', array('class'=>'btn btn-large btn-primary btn-block')) }}
</div>
{{ Form::close() }}
</div>
</div>
</div>
</div>
</div>
#stop
The problem is your $data that you pass to Auth::attempt. You should change
if (Auth::attempt($data))
into
$dataAttempt = array(
'UserEmail' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::attempt($dataAttempt))
and add to your User model the following function:
public function getAuthPassword() {
return $this->UserEmail;
}
This is because you need to pass password in array as your password to attempt method (You can read more about it at How to change / Custom password field name for Laravel 4 and Laravel 5 user authentication)