Laravel create custom method to get form data - php

How do I make custom method to get form data? I want this method same with Laravel update method with parameters request and id. I try this but get error.
In controller
public function updatePassword(Request $request, int $id) {
dd($request->all());
}
In route
Route::post('staffs/{id}/upassword', 'Admin\StaffController#updatePassword')->name('admin.staffs.upassword');
In blade file
<form method="post" accept-charset="utf-8" action="{{ action('Admin\StaffController#updatePassword', ['id' => $staff_id]) }}">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password">New Password</label>
<input class="form-control" name="password" type="password">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password_confirmation">Confirm New Password</label>
<input class="form-control" name="password_confirmation" type="password">
</div>
</div>
</div>
<input class="btn btn-primary" type="submit">
</form>
I am using Laravel 5.4.

here are some stuff to fix:
First in the tag you can set the action to :
action="route('admin.staffs.upassword', $staff_id)" since it's
easier to write and since you already gave the route a name, so why
not using it ;)
Second add {{csrf_field() }} right before your form closing tag
</form>

what error are you getting? the error is probably because you are not using {{csrf_field()}} after the form declaration, it is needed so that laravel can validate the request. if you want to get the data from the form you can use:
$request->get('inputname');

Related

Laravel form submission doesn't goes to the right route?

I have installed the latest laravel. I Have made this simple form. I want to create post but when I submit it goes to localhost/post which is the wrong URL . The actual URL is http://localhost/laravel_practice/'
Form
<form method="post" action="/post">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Enter Title Here">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="body" class="form-control" placeholder="Enter the body"></textarea>
</div>
<div class="form-group">
<input type="submit" name="sumit" class="btn btn-primary" value="Publish">
</div>
My Routes
Route::get('/' ,'PostController#index');
Route::get('/posts/create', 'PostController#create');
Route::post('/post','PostController#store');
Your short fix is to use action="/laravel_practice/post" or action="/laravel_practice/public/post" depending on what url you want to go.
However, it is a bad practice. You should use route name. To do that give any name to the route like below,
Route::post('/post','PostController#store')->name('post.store');
Then in view you should use,
<form method="post" action="{{ route('post.store') }}">
To read more about named route you can go through this document.

Laravel testing phpunit press NotFoundHttpException

I'm trying to setup phpunit tests for a project with Laravel 5.1.40 (LTS), php 5.6.28, and phpunit 4.8.27. I'm sorry if this issue has been solved before, but I couldn't find anything.
public function testAdminLogin()
{
$this->visit('/auth/login')
->type('email#address.com', 'email')
->type('1234567890', 'password')
->press('Login');
}
There seem to be an issue with press('STRING') with both <button> and <input> as submit buttons. Below is the error message I receive.
1) ExampleTest::testAdminLogin
A request to [http://localhost/auth/login] failed. Received status code [500].
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:165
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:63
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:85
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:684
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:671
C:\xampp\htdocs\project\tests\ExampleTest.php:52
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
However, when I change the <button> tag to an <a> tag, add an id to it, and replace the press(STRING) function with the click(ID) function, the test passes. I could change the <button> to an <a>, but that would only a temporary fix, and future cases might not allow the tag change.
Below is the HTML form with the <button> tag.
<form action="/auth/login" method="POST" class="form-horizontal">
<div class="form-group">
<label for="email" class="col-sm-4 control-label">E-Mail</label>
<div class="col-sm-6">
<input type="email" name="email" class="form-control" value="{{ old('email') }}" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-4 control-label">Password</label>
<div class="col-sm-6">
<input type="password" name="password" class="form-control" autocomplete="off">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-6">
<button type="submit" class="btn btn-default btn-login">Login</button>
</div>
</div>
</form>
You said you define auth routes manually. In this case you should have POST route for sending login form:
Route::post('auth/login', ....
It works in a href because it sends GET request for which you have route. Form sends POST request by default.

Laravel: Passing data through a controller results in MethodNotAllowedHttpException

I have seen this post, however I don't believe it is relevant to my issue because I believe I am correctly passing post data through a post route.
Here is the relevant route code:
Route::get('/pass', 'PageController#pass');
Route::post('/pass/{request}',['uses' => 'PageController#passController']);
I would like to have one controller method for the 'pass' page, but to isolate the issue I have separated them.
Here are the relevant methods in PageController.php:
public function pass(){
return view('pass')->with(array(
'title'=>'Create A Pass'
));
}
public function passRequest($request){
$data['request'] = $request;
$validator = Validator::make($request->all(), [
'studentID' => 'required|max:255',
'teacherID' => 'required|max:255',
'destination' => 'required|max:255',
]);
if ($validator->fails()) {
return redirect('/')
->withInput()
->withErrors($validator);
}
$pass = new Pass;
$pass->student = DB::table('users')->where('studentID', $request->studentID)->first()->id;
$pass->teacher = DB::table('users')->where('teacherID', $request->teacherID)->first()->id;
$pass->destination = $request->destination;
$pass->save();
return view('home')->with(array(
'title'=>'Home',
'success'=>'null'
));
}
I used the method stated here in order to pass data to the controller. If this is bad practice/obsolete I'm open to any suggestions.
This is the form in the 'pass' page responsible for sending the post data
<form action="{{ url('pass') }}" method="POST" class="form-horizontal">
{!! csrf_field() !!}
<fieldset>
<!-- Text input-->
<div class="container">
<div class="form-group">
<label class="col-md-4 control-label" for="studentID">Student ID</label>
<div class="col-md-3">
<input id="studentID" name="studentID" type="text" class="form-control input-md">
</div>
</div>
</div>
<!-- Text input-->
<div class="container">
<div class="form-group">
<label class="col-md-4 control-label" for="teacherID">Teacher ID</label>
<div class="col-md-3">
<input id="teacherID" name="teacherID" type="text" class="form-control input-md">
</div>
</div>
</div>
<!-- Text input-->
<div class="container">
<div class="form-group">
<label class="col-md-4 control-label" for="destination">Destination</label>
<div class="col-md-3">
<input id="destination" name="destination" type="text" class="form-control input-md">
</div>
</div>
</div>
<div class="container">
<div class="form-group">
<div class="col-sm-offset-4 col-sm-6">
<button type="submit" class="btn btn-default">
<i class="fa fa-check"></i> Create Pass
</button>
</div>
</div>
</div>
</fieldset>
</form>
On submission of this form I get the MethodNotAllowedHttpException Exception.
If a stack trace of the error would be helpful, please let me know. If there are any suggestions on style, I'm open to that as well.
This form tag will generate a POST request to the URL /pass:
<form action="{{ url('pass') }}" method="POST" class="form-horizontal">
Your routes file does not allow that. It only allows GET requests to that url, but POST requests to /pass/{request}.
Not sure if its just a copy/paste mistake, but your POST route is set up to call PageController#passController method, but the method you shared from your controller is named passRequest. Those will need to match also.
In addition to what Jeff Lambert pointed out, you should not put the {request} variable in the route.
You should remove that and have laravel inject the Request object for you.
Import the Request class if you haven't already at the top of the class.
use Illuminate\Http\Request;
And your function should look like the following...
public function passRequest(Request $request)
{
...
}
If you have additional parameters to pass through the URL, then you may add them to the route, and add the arguments to the method after Request $request. Laravel will figure out what to do with it.
try this one...
Route::post('/pass/post','PageController#passController')->name('post_insert');
in your html form change to ...
<form action="{{ route('post_insert') }}" method="POST" class="form-horizontal">
change it also ...
public function passRequest(Illuminate\Http\Request $request){
....

No query results for model [App\WhatTodoModel]

I am newbie in laravel and I try to insert a data form a form having foreign key by using hide such as the code is mention below:-
<form class="form-horizontal" role="form" action="/WhatTodo/store" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="task_id" value=" {{$what->task_id}}">
<input type="hidden" name="work_id" value="{{$what->work_id}}">
<div class="form-group">
<label class="control-label col-sm-2" for="name"> Name</label>
<div class="col-sm-5">
{!!Form::select('name',$name)!!}
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="work">work:</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="work" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" value="Submit">
</div>
</div>
</form>
I have the controller with function:-
public function create($id)
{
$what=WhatTodoModel::findorFail($id);
$name=WOrk::lists('name','name');
return view('what/create',compact('what','name'));
}
You haven't really told us what your issue is or what error you're getting, but my guess given the current question is:
Assuming you're trying to implement a resource route and resourceful controller, the create method is used to show a form to create a new object, not edit an existing one. The create method does not take any parameters, therefore $id will be blank and WhatTodoModel::findorFail($id); will throw an exception.
If you want to edit an existing record, you do that using the edit action.
For creating a new record, create shows the form, store saves the record.
For editing an existing record, edit shows the form, update saves the record.

laravel not loading my form section?

im trying to build a user login and registration form and this is my route :
Route::get('/register', function()
{
return View::make('register');
});
Route::get('/register', function()
{
$user = new User;
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();
$username = Input::get('username');
return View::make('registered')->with('username',$username);
});
and this is my html :
<div class="container">
{{ Form::open(array('url' => 'register', 'class' => 'form-horizontal')) }}
<fieldset>
<!-- Form Name -->
<legend>Form Name</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="username"></label>
<div class="col-md-4">
<input id="username" name="username" type="text" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="password"></label>
<div class="col-md-4">
<input id="password" name="password" type="password" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Appended checkbox -->
<div class="form-group">
<label class="col-md-4 control-label" for="appendedcheckbox"> </label>
<div class="col-md-4">
<div class="input-group">
<input id="appendedcheckbox" name="appendedcheckbox" class="form-control" type="text" placeholder="">
<span class="input-group-addon">
<input type="checkbox">
</span>
</div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"> </label>
<div class="col-md-4">
<button id="submit" name="submit" class="btn btn-inverse"> </button>
</div>
</div>
</fieldset>
</div>
few problems :
1.
my form does not loads and i see just
the last button for submitting the form and : ' you have registered in $username ' which i design to loads AFTER user submitted
2.my localhost:8000 loaded laravel first page one time but when i began to work on the project i just receiving blank white page and currently accessing my file like this : http://localhost/vendor/bin/crm/public/register
3.
is hashing in laravel secure enough? or should i do something else ?
4.
my way of doing this is alright or there is a better way for login and reg using laravel ?
You have two routes responding to get requests on /register. Change the second one to Route::post(...) and I would also change both to just register. There isn't a need to prepend a slash onto your routes.
Hashing in Laravel is secure and shouldn't be something you have to worry about.
There really isn't a "right" way of doing things, it really depends on how the rest of your app works, how complicated it is, and how easy it should be to maintain. If it were me though, I would have a LoginController with a method for showing the view and a method for creating the user and have those methods respond to the request rather than putting everything right in your routes.php file.
You are also missing a {{ Form::close() }} at the end of your view as well.

Categories