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?
Related
I tried to edit and updated the employee info, unfortunately, it doesn't work
I fetch the employee id but when I sent the updated data it's not working.
it shows Requested URL not found on the server
this is my controller
public function edit_function($id){
$user = User::find($id);
return view('employee.empedit')->with('user',$user);
}
public function update(Request $request,$id){
$user = User::find($id);
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->phonenumber = $request->input('phonenumber');
$user->profession = $request->input('profession');
if($request->hasfile('images')){
$file= $request->file('images');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->move('uploads/user/', $filename);
$user->images= $filename;
}
$user->save();
return redirect('empprofile')->with('success', 'Data Updated.');
}
this is my view
<form method="post" action="/updateimages/{{ $user->id }}" enctype="multipart/form-data">
<div class="container">
<div class="jumbotron">
<h2>Update The Information Of Employee</h2>
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="form-group">
<label >Name:</label>
<input type="text" class="form-control" id="name" placeholder="Enter Name" name="name" value="{{ $user->name }} ">
</div>
<div class="form-group">
<label >Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email" value="{{ $user->email }} ">
</div>
<div class="form-group">
<label >Phone Number:</label>
<input type="text" class="form-control" id="phonenumber" placeholder="Enter Phone Number" name="phonenumber" value="{{ $user->phonenumber }} ">
</div>
<div class="form-group">
<label >Profession :</label>
<input type="text" class="form-control" id="profession" placeholder="Enter Profession" name="profession" value="{{ $user->profession }} ">
</div>
<div class="form-group">
<label >Image :</label>
<input type="file" class="form-control" id="images" placeholder="" name="images" value="{{ $user->images }}">
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit" name="submit" style="width:50%;">Update Data</button>
</div>
</div>
</div>
</form>
this is my route
Route::get('edit_profile/{id}' , "empController#edit_function");
Route::put('/updateimages/{id}', "empController#update");
it shows Requested URL not found on the server
Since I am not a Big fan of Url and id So i will go with
name based routing and Route Model Binding
Step 1: Refactor Routes
Route::get('edit_profile/{user}' , "empController#edit_function")
->name('user.editProfile');
Route::put('/updateimages/{user}', "empController#update")
->name('user.updateProfile');
Step 2: Refactor Controller Method
public function edit_function(User $user)
{
$user = $user;
return view('employee.empedit')->with('user',$user);
}
public function update(Request $request,User $user)
{
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->phonenumber = $request->input('phonenumber');
$user->profession = $request->input('profession');
if($request->hasfile('images')){
$file= $request->file('images');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->move('uploads/user/', $filename);
$user->images= $filename;
}
$user->save();
return redirect('empprofile')->with('success', 'Data Updated.');
}
Step 3: Edit Html and Switch to route helper
<form method="POST" action="{{route('user.updateProfile',['user' => $user])}}" enctype="multipart/form-data">
Kindly Comment Below if you are facing any issues
Its because some other route replce your existing route. You can solve it by debugging. it will cost your time. I had a better solution,
You name your route. and call the route by route() function.
From your above information,
It may be,
in route ->
Route::put('/updateimages/{id}', "empController#update")->name('updateImage');
in view (form action) ->
<form method="post" action="{{ route('updateImage', $user->id ) }}" enctype="multipart/form-data">
I am working with Laravel 5.6 and going to update My user table values (name,email,password) as the system admin.
blade file
<form action="{{route('users.update',$user->id)}}" method="POST">
{{method_field('PUT')}}
{{csrf_field()}}
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" value="{{$user->name}}" >
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" name="email" value="{{$user->email}}">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<div class="form-group">
<label for="password_confirmation">Confirm Password</label>
<input type="password" class="form-control" id="password_confirmation" name="password">
</div>
<button type="submit" class="btn btn-primary">Edit User</button>
</form>
and My controller,
public function update(Request $request, $id)
{
$user = User::findOrFail($id);
$user->name = $request->name;
$user->uservalue = $request->uservalue;
$user->email = $request->email;
$user->password = bcrypt($request->input('password'));
$user->save();
return view('users.show')->withUser($user);
}
Problem
my password confirmation is not working
That means I can enter password without confirmation and or wrong confirmation password. How can I fix this problem?
You have to add a password confirm validation. For that to work, your second password field has to be called password_confirmation (or foo and foo_confirmation)
public function update(Request $request, $id)
{
$user = User::findOrFail($id);
$request->validate([
'password' => 'sometimes|min:6|confirmed'
]);
$user->name = $request->name;
$user->uservalue = $request->uservalue;
$user->email = $request->email;
$user->password = bcrypt($request->input('password'));
$user->save();
return view('users.show')->withUser($user);
}
You can add more validation fields as explained here. If the validation fails, the browser will bring the user back to the update page. The errors are in the laravel errorbag available in your views as $errors.
sometimes means that the following rules are needed if there is an input. As you want to update user information, your visitors can leave the password field empty if they don't want to update the password. Unset the password field (if empty) before updating the user input.
I have the following problem:
I want to insert into my database, with input fields.
Here is my html:
<div class="row">
<form action="{{ action('test#store') }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="col-md-2 col-sm-2">
<label for="MtrNr">MtrNr:</label>
<input type="text" name="MtrNr" class="form-control">
</div>
<div class="col-md-2 col-sm-3">
<label for="Vorname">Vorname:</label>
<input type="text" name="Vorname" class="form-control">
</div>
<div class="col-md-2 col-sm-3">
<label for="Nachname">Nachname:</label>
<input type="text" name="Nachname" class="form-control" >
</div>
<div class="col-md-2 col-sm-2">
<label for="Klassenname">Klassenname:</label>
<input type="text" name="Klassenname" class="form-control">
</div>
<div class="col-md-2 col-sm-2">
<button class="btn btn-primary option-button button_rowAllign" type="submit">Schüler hinzufügen</button>
</div>
</form>
</div>
My controller:
public function store(Request $request)
{
$user = new schueler;
$user->MatNr = Input::get("MtrNr");
$user->vorname = Input::get("Vorname");
$user->nachname = Input::get("Nachname");
$user->klassenname = Input::get("Klassenname");
$user->save();
}
And here my routes:
Route::post("{{ action('test#store') }}", "test#store");
The thing is, that I already inserted into my database. But when i tried to
reproduce it, it doesn't work.
For local server I use xampp.
I hope you can help me.
The problem is that you only created a new Schueler object and gave the data to it. But you didn't save it in the database. You only made local changes.
Use this method to save.
public function store(Request $request) {
$user = new schueler;
$user->MatNr = Input::get("MtrNr");
$user->vorname = Input::get("Vorname");
$user->nachname = Input::get("Nachname");
$user->klassenname = Input::get("Klassenname");
$user->save();
}
Try changing your routes like this
Route::post("/test", "test#store"); // change /test to /anything you want
Before all make sure you have the database setup and there are all tables in that database like schuelers - For that do php artisan migrate
If you want to Insert record into the database you miss to add $user->save() in your controller
public function store(Request $request)
{
$user = new schueler;
$user->MatNr = Input::get("MtrNr");
$user->vorname = Input::get("Vorname");
$user->nachname = Input::get("Nachname");
$user->klassenname = Input::get("Klassenname");
$user->save();
}
I think something wrong with your route.
Route::post("{{ action('test#store') }}", "test#store");
Route should be something like below:
Route::post("schueler/store", "yourcontrollername#store");
//This is sample code, Please update with your one.
database details:
dbtable : guests
dbcolumns : id (A_I), guestname, guestemail
when i submit a data it's only refresh the page and take no action i'm beginner please tell me where's the problem and why
i made insert form by basic php and i passed but with codeigniter maybe i'm not fully understood yet looks like my experience about 1% or 5% :D :D
Controller/Guests.php :
<?php
class Guests extends CI_Controller {
public function index($page = 'guests')
{
if ( ! file_exists(APPPATH.'/views/main/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page);
$data['pagedesc'] = 'Guests List';
$this->load->view('include/header', $data);
$this->load->model('guests_model');
$data['records'] = $this->guests_model->getAll();
$this->load->view('main/guests', $data);
$this->load->view('include/footer', $data);
}
public function addnewguest($page = 'Add New Guest')
{
if ( ! file_exists(APPPATH.'/views/main/addnewguest.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page);
$data['pagedesc'] = 'Please Fill Informations Below';
$this->load->view('include/header', $data); // loaded the header
$this->load->model('guests_model'); // loaded the model
$this->load->view('main/addnewguest', $data); // loaded add new form
if($this->input->post('createnew')) { // if click add send information to addnewguestpost() function in the guest_model file
$this->guests_model->addnewguestfunc();
}
$this->load->view('include/footer', $data); // loaded the footer
}
}
views/main/addnewguest.php
<form class="form-horizontal" action="<?php echo site_url('guests/addnewguest'); ?>" method="post">
<div class="form-group">
<label for="guestname" class="col-sm-2 control-label">Guest Name</label>
<div class="col-sm-3">
<input type="text" name="guestname" class="form-control" id="guestname" placeholder="Guest Name ...">
</div>
</div>
<div class="form-group">
<label for="guestemail" class="col-sm-2 control-label">Guest Email</label>
<div class="col-sm-3">
<input type="text" name="guestemail" class="form-control" id="guestemail" placeholder="Guest Email ..">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" name="createnew">Add</button>
</div>
</div>
</form>
models/guests_model.php
<?php
class Guests_model extends CI_Model {
public function getAll() {
$query = $this->db->get('guests');
if($query->num_rows() > 0){
return $query->result();
} else {
return FALSE;
}
}
public function addnewguestfunc() {
$data = array(
'guestname' => $this->input->post('guestname'),
'guestemail' => $this->input->post('guestemail')
);
$this->db->insert('guests', $data);
}
}
Solved !!
forgot change button to input because i copied it from bootstrap basic templates for practicing ..
views/main/addnewguest.php after change button to input :
<form class="form-horizontal" action="<?php echo site_url('guests/addnewguest'); ?>" method="post">
<div class="form-group">
<label for="guestname" class="col-sm-2 control-label">Guest Name</label>
<div class="col-sm-3">
<input type="text" name="guestname" class="form-control" id="guestname" placeholder="Guest Name ...">
</div>
</div>
<div class="form-group">
<label for="guestemail" class="col-sm-2 control-label">Guest Email</label>
<div class="col-sm-3">
<input type="text" name="guestemail" class="form-control" id="guestemail" placeholder="Guest Email ..">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" name="createnew">Add</input>
</div>
</div>
</form>
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);