I get an error that I am trying to understand: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'username' in 'where clause' (SQL: select * from users where username = hi#hi.hi limit 1) when trying to login
My 'users' table:
id email password name admin created_at updated_at
1 go#go.go $2y$10$1R/21A3C32nwkjtEgDFcyuMc2D4AtgeFnTh1ygLEcQl... go 1 2014-11-01 21:04:33 2014-11-01 21:04:33
2 hi#hi.hi $2y$10$Akb9BoLWrOcQT0KFee7vvujtrzkbeRQnUDcuCXepeAd... hi 0 2014-11-02 16:15:47 2014-11-02 16:15:47
My form in views (displays correctly)
#extends('login_c')
#section('loginform')
<?= '<span style="color:red">' .
Session::get('login_error') . '</span>' ?>
{{ Form::open() }}
{{ Form::label('email', 'Email address: ') }}
{{ Form::text('email', Input::old('email')) }}
<br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
<br>
{{ Form::submit('Login!') }}
{{ Form::close() }}
#stop
The problem is that I specified my password before in the registration and I do not understand why I either get "Could not login" or the SQL injection error.
oute::get('registration', function()
{
return View::make('registration');
});
Route::post('registration', array('before' => 'csrf',
function()
{
$rules = array(
'email' => 'required|email|unique:users',
'password' => 'required|same:password_confirm',
'name' => 'required'
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
return Redirect::to('registration')->withErrors
($validation)->withInput();
}
$user = new User;
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->name = Input::get('name');
$user->admin = Input::get('admin') ? 1 : 0;
if ($user->save())
{
Auth::loginUsingId($user->id);
return Redirect::to('profile');
}
return Redirect::to('registration')->withInput();
}));
Route::get('profile', function()
{
if (Auth::check())
{
return 'Welcome! You have been authorized!';
}
else
{
return 'Please Login';
}
});
Route::get('login', function()
{
return View::make('login');
});
Route::post('login', function()
{
$user = array(
'name' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::attempt($user))
{
return Redirect::to('profile');
}
return Redirect::to('login')->with('login_error',
'Could not log in.');
});
Route::get('secured', array('before' => 'auth', function()
{
return 'This is a secured page!';
}));
My register form: (Sorry, I am not posting my full view, as it is not relevant, I am sure that the error is in the form and/or usermodel.
{{ Form::open() }}
{{ Form::label('email', 'Email address: ') }}
{{ Form::text('email', Input::old('email')) }}
<br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
<br>
{{ Form::label('password_confirm',
'Retype Password: ') }}
{{ Form::password('password_confirm') }}
<br>
{{ Form::label('name', 'Name: ') }}
{{ Form::text('name', Input::old('name')) }}
<br>
{{ Form::label('admin', 'Admin?: ') }}
{{ Form::checkbox('admin','true',
Input::old('admin')) }}
<br>
{{ Form::submit('Register!') }}
{{ Form::close() }}
#stop
How should I change my user model? Why does it save the wrong password?
Try to change
$user = array(
'name' => Input::get('email'),
'password' => Input::get('password')
);
to
$user = array(
'email' => Input::get('email'), // You're using email to log in users, not name in your form
'password' => Input::get('password')
);
Related
I am trying to insert data into the database neither it shows an error nor saves the data into my database below are the codes. here is the create blade php page .
#extends('layouts.app')
#section('content')
<h1>Create a Camp</h1>
<!-- if there are creation errors, they will show here -->
{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url' => 'camps')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name', Request::old('name'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('district', 'District') }}
{{ Form::text('district', Request::old('district'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('address', 'Address') }}
{{ Form::text('address', Request::old('address'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('block', 'Block') }}
{{ Form::text('block', Request::old('block'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('population', 'population') }}
{{ Form::text('population', Request::old('population'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Create the Camp!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
#endsection
here is the controller the index controller works fine when I manually enter the data it fetches from the database
here is the store controller and also it not validate the forms data , i am new i dont know how to do it
public function store(Request $request)
{
$rules = array(
'name' => 'required',
'district'=> 'required',
'address' => 'required',
'block' => 'required|numeric',
'Population' => 'required|numeric',
);
$validator = Validator::make(Request::all(), $rules);
if ($validator->fails()) {
return Redirect::to('camp/create')
->withErrors($validator)
->withRequest(Request::except('password'));
} else {
// store
$camp = new Camp;
$camp->name = Request::get('name');
$camp->district = Request::get('district');
$camp->address = Request::get('address');
$camp->block = Request::get('block');
$camp->population = Request::get('population');
$camp->save();
// redirect
Session::flash('message', 'Successfully created Camp!');
return view('camp\camps',['camps'=>$camps]);
}
}
You forgot to add parenthesis after Camp it should be like this:
$camp = new Camp();
$camp->name = $request->name;
$camp->district = $request->district;
$camp->save();
Today I have made a comments system, but isn't completed.In my database I have all fields filled by dates, but user_id isn't.How can I be able to save in column user_id, the id of authenticated user?Here is my code.
CommentsController:
public function store(Request $request, $post_id)
{
$this->validate($request, array(
'username' => 'required|max:255',
'mail' => 'required|mail|max:255',
'comment' => 'required|min:5|max:2000',
));
$post = Post::find($post_id);
$comment = new Comment();
$comment->username = $request->username;
$comment->email = $request->email;
$comment->comment = $request->comment;
$comment->approved = true;
$comment->post()->associate($post);
$comment->save();
Session::flash('message', "Message posted successfully!");
return Redirect::back();
}
My view
<div class="row">
<div id="comment-form">
{{ Form::open(['route' => ['comments.store', $post->id], 'method' => 'POST']) }}
<div class="row">
<div class="col-md-6">
{{ Form::label('username', "Username:") }}
{{ Form::text('username', null, ['class' => 'form-control']) }}
</div>
<div class="col-md-6">
{{ Form::label('email', "Email:") }}
{{ Form::text('email', null, ['class' => 'form-control']) }}
</div>
<div class="col-md-6">
{{ Form::label('comment', "Comment:") }}
{{ Form::text('comment', null, ['class' => 'form-control']) }}
</div>
{{ Form::submit('Add Comment', ['class' => 'btn btn-success btn-xs']) }}
</div>
{{ Form::close() }}
</div>
My route
Route::post('comments/{post_id}', ['uses' => 'CommentsController#store', 'as' => 'comments.store']);
I tried to put Auth::user()->id inside my form, but I failed...
For you get the authenticated user you should only use the Auth facade. I show how to use it below.
use Illuminate\Support\Facades\Auth;
$user = Auth::user(); // Get the currently authenticated user...
$user_id = Auth::id(); // Get the currently authenticated user's ID...
Therefore, to save a user associated in a comment you have two options:
$user_id = Auth::id();
$comment->user_id = $user;
$comment->save();
Or
$user = Auth::user();
$comment->user = $user;
$comment->save();
If you have more questions about Laravel authentication take a look at https://laravel.com/docs/5.8/authentication#retrieving-the-authenticated-user.
Hello im new in laravel 4.2 and i m making an application in the controllers, i made my own function called c_updateSystemUser($id) and here is the code
public function c_updateSystemUser($id)
{
//
session_start();
$getsCapt = $_SESSION["captcha"];
$rules = array(
'username' => 'required|min:2|max:50|regex:/^[a-zA-Z0-9\-\s]+$/|exists:dbo_systemusers,SystemUserName,$id',
'description' => 'required|min:1|max:100|regex:/^[a-zA-Z0-9\-\s]+$/',
'usertype' => 'required|numeric',
'capt' => 'required|numeric'
);
$validator = Validator::make(Input::all(), $rules);
// process the inputs given by the user
if ($validator->fails())
{
return Redirect::to('viewsu/' . $id)
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
// store the values into the database
$su = SystUser::find($id);
$su->SystemUserTypeID = Input::get('usertype');
$su->SystemUserName = Input::get('username');
$su->SystemUserDescription = Input::get('description');
$su->timestamps = false;
$su->save();
Session::flash('message', 'Successfully created system user!');
return Redirect::to('viewsu');
}
}
}
i wanted this function to be called when the user clicked the update button on my view file but when the button is clicked, it is redirecting to some other page (my create user page) i don't know why
here is the code of my form in my blade
{{ Form::model($su, array('route' =>array('csu.update',$su->SystemUserID),'method'=>'PUT'))}}
<div class="form-group">
{{ Form::label('usertype', 'User Type') }}
{{ Form::select('usertype', [null=>'Please Select user type'] + $sTyp , array('class'=>'form-control'))}}
{{ Form::label('current' , 'Current Type: (unedited) '.$su->SystemType , array('class' => 'label label-primary')) }}
</div>
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('username', $su->SystemUserName, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('description', 'Description') }}
{{ Form::textarea('description', $su->SystemUserDescription, array('class' => 'form-control','rows' => '3')) }}
</div>
<div class="form-group">
{{ Form::label('captcha', 'CAPTCHA image: ') }}
</br>
{{ HTML::image('http://localhost:8080/laravel3/app/captcha.php', 'alt' , array( 'width' => 200, 'height' => 35 )) }} </br></br>
{{ Form::text('capt', Input::old('capt'), array('class' => 'form-control','placeholder' => 'enter generated captcha')) }}
</div>
{{ Form::submit('Edit System User', array('class' => 'btn btn-primary')) }}
{{ Form::close()}}
i don't know how to make the form call the function public function c_updateSystemUser($id) in my controller
my controller file is called systemUsers.php
any ideas? any help would be appreciated
If your view name is update and its in views->csu folder you can do it like following
{{ Form::model($su, array('route' =>array('csu.update.c_updateSystemUser',$su->SystemUserID),'method'=>'PUT'))}}
#yourentry
{{Form::close()}}
Your route should be like following:
Route::resource('csu','systemUsers');
In systemUsers.php add c_updateSystemUser($id){} method.
I am new in laravel and I am trying to update users but I am stuck here. When I press the update button it does not do anything....
//HomeController.php
public function getUpdate($username) {
$user = array();
$users = User::where("username", "=", $username)->get();
foreach ($users as $key => $value) {
if (isset($value->username)) {
$user['username'] = $value->username;
$user['email'] = $value->email;
$user['password'] = $value->password;
}
}
$username = (object) $user;
return View::make('home.update', array('username' => $username));
}
public function postUpdate($username) {
$input = Input::all();
$rules = array('username' => 'required|unique:users',
'email' => 'required|unique:users|email',
'password' => 'required'
);
$v = Validator::make($input, $rules);
if ($v->fails()) {
Redirect::to('home.update')
->withErrors($v)
->withInput(Input::except('password'));
} else {
$user = User::find($username);
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->save();
return Redirect::to('home.index')
->with('message', 'Successfully updated!');
}
}
//view
#extends('master')
#section('content')
<div class="span12 well" style="opacity: 0.9">
<h4>Update personal details:</h4>
{{ Form::open(array('url' => 'update/'.Auth::user()->username.'/update')) }}
#if($errors->any())
<div class="alert alert-error">
×
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
</div>
#endif
<div class="form-group">
{{ Form::label('username', 'Username:') }}
{{ Form::text('username',$username->username, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'E-mail:') }}
{{ Form::text('email', $username->email, array('class' => 'form-control')) }}
</div>
<p>{{ Form::submit('Update', array('class' => 'btn btn-success'))}}</p>
{{ Form::close() }}
</div>
#stop
//routes
Route::get('update/{username}', 'HomeController#getUpdate');
Route::post('update/{username}/update', 'HomeController#postUpdate');
Try this:
Change this:
{{ Form::open(array('url' => 'update/'.Auth::user()->username.'/update')) }}
Into:
{{ Form::open(array('url' => 'update/'.Auth::user()->id.'/update')) }}
Your route must be:
Route::post('update/{id}/update', 'HomeController#postUpdate');
And your controller must be:
public function postUpdate($id) { //I change this to id based on the url of your route
$input = Input::all();
$rules = array('username' => 'required|unique:users',
'email' => 'required|unique:users|email',
'password' => 'required'
);
$v = Validator::make($input, $rules);
if ($v->fails()) {
Redirect::to('home.update')
->withErrors($v)
->withInput(Input::except('password'));
} else {
$user = User::find($id); //User::find() only finds ID
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->save();
return Redirect::to('home.index')
->with('message', 'Successfully updated!');
}
}
I'm trying to use Laravel's Mail class for the first time and am having some difficulties. I have a simple contact form that should be sent as an email to the admin. Here is what I have tried
Controller (Will be refactored into Model once I can get it working)
public function store()
{
$validation = new Services\Validators\Contact;
if($validation->passes())
{
$fromEmail = Input::get('email');
$fromName = Input::get('name');
$subject = "Email from user at website.com";
$data = Input::get('message');
$toEmail = 'test#dummyemail.com';
$toName = 'Mitch Glenn';
Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->to($toEmail, $toName);
$message->from($fromEmail, $fromName);
$message->subject($subject);
});
return Redirect::to('/')
->with('message', 'Your message was successfully sent!');
}
return Redirect::back()
->withInput()
->withErrors($validation->errors);
}
View
{{ Form::open(array('action' => 'ContactController#store', 'class' => 'row', 'id' => 'contact')) }}
{{ Form::label('name', 'Name') }}
{{ Form::text('name', '', array('class' => 'full')) }}
{{ Form::label('email', 'Email') }}
{{ Form::email('email', '', array('class' => 'full')) }}
{{ Form::label('message', 'Message') }}
{{ Form::textarea('message', '', array('class' => 'full')) }}
{{ Form::submit('Send Now', array('class' => 'btn btn-large btn-primary')) }}
{{ Form::close() }}
#include ('_partials.errors')
When I fill our the contact form and hit send, I get this error:
Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, string given
Set your data variable to be an array:
$data = array( 'message' => Input::get('message') );