I'm trying to update an edit to a post without using the resource. I tried parsing the variable from my Form to my route using {id} but it gets ignored. This is the form I'm trying to post from.
{!! Form:: open(['action'=> ['ManageBooksController#updateBook', $book->id], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('Book_NAME', 'Name')}}
{{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::hidden('_method', PUT)}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
This is my route
Route::put('manageBooks', 'ManageBooksController#updateBook');
This is my method in my controller
public function updateBook(Request $request, $id)
{
$this->validate($request, ['Book_NAME' => 'required']);
$books = Books::find($id);
$books->Book_NAME =$request->input('Book_NAME');
$books->save();
return redirect('/manageBook')->with('success', 'Book Edited');
}
Your route is expecting a PATCH. Try updating your route to:
Route::post('/manageBooks/{id}', 'ManageBooksController#updateBook');
Or include the Laravel's #method('PATCH') within your form.
Also, your controller names don't match :)
Consider changing sequence of the arguments to the function:
public function updateBook($id, Request $request) // Notice the sequence of the arguments
{
......
}
If you want to use Route, you have to specifics like this
{!! Form:: open(['route'=> ['manage_book', $book->id], 'method' => 'POST']) !!}
In your route, you may need to name it properly
Route::post('/manageBooks/{id}', array('as'=>'manage_book','uses'=>'ManageBooksController#updateBook'));
Hope it helps.
Update without using resource :
your route :
Route::get('/manageBooks', 'ManageBooksController#whateverer')->name('manageBooks');
Route::post('/manageBooks/{id}/edit', 'ManageBooksController#updateBook')->name('updateBook');
your blade:
{!! Form:: open(['route'=> ['updateBook', $book->id], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('Book_NAME', 'Name')}}
{{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::hidden('id', $book->id)}} //hidden field is not required
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
your controller:
public function updateBook(Request $request, $id)
{
$this->validate($request, ['Book_NAME' => 'required']);
$books = Books::where('id',$id)->update(['Book_NAME'=>$request->Book_NAME]);
return redirect()->route('manageBooks')->with('success', 'Book Edited');
}
In the end I added another hidden field where i parse the ID through of the post I'm editing. I also changed the find method to take the request variable pointing to the ID.
My Form:
{{Form::hidden('Book_ID', $book->Book_ID)}}
{{Form::hidden('_method', PUT)}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
My function:
public function updateBook(Request $request)
{
$this->validate($request, ['Book_NAME' => 'required']);
$books = Books::find($request->Book_ID);
$books->Book_NAME =$request->input('Book_NAME');
$books->save();
return redirect('/manageBook')->with('success', 'Book Edited');
}
Change your method POST to PUT in your from first,
{!! Form:: open(['action'=> ['ManageBooksController#updateBook', $book->id],'method' => 'PUT']) !!}
<div class="form-group">
{{Form::label('Book_NAME', 'Name')}}
{{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
Then You Have to Pass a parameter in your route, as your method expecting $id
Route::put('manageBooks/{id}/update', 'ManageBooksController#updateBook');
Related
I'm new to Laravel. I've created a form and trying to delete one of the post. I just want to know how opening a form from Laravel collectives works.
Currently, this is how my form looks like in show.blade.php
{!! Form::open(['action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
{!! Form::close() !!}
The above form gives me the error Action PostsController#destroy not defined.
But when I add 'url' => 'posts/' in Form i.e.
{!! Form::open(['url' => 'posts/', 'action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
The above error disappears.
Below is the destroy() function in PostsController
class PostsController extends Controller
{
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect('/posts')->with('success','Post removed');
}
}
web.php
// --- Posts Routing
Route::get('/posts', [App\Http\Controllers\PostsController::class, 'index'])->name('posts');
MY QUESTIONS
I'm redirecting in the destroy() function in PostsController, why url is necessary with action in Form from Laravel
collectives to avoid the above error?
When to use 'url' and when to use action?
I've laravel 4.2.10.
I have a search bar in view:
{!! Form::open(['name' => 'myForm',
'method' => 'GET',
'action' => 'AreaController#search',
'files' => true,
'onsubmit' => "return validateForm()"])
!!}
{!! Form::submit('جستجو', ['class' => 'btn btn-info']) !!}
{!! Form::close() !!}
And in route file, web.php:
Route::get('area/search/', 'AreaController#search')->name('area.search');
AreaController:
public function search(Request $request) {
return " it is working" ;
}
But when I click on the button, the browser is showing a blank page. When I use POST method it is working, but if I change to GET method, it is not working.
Thank you.
For Post Method, you need to add CSRF Token To A Form
{!! Form::open(['method' => 'POST']) !!} <--------- Change to POST method
{!! Form::token() !!} <----------- Add this line
{!! Form::submit('جستجو', ['class' => 'btn btn-info']) !!}
{!! Form::close() !!}
Route::post('area/search/', 'areacontroller#search')->name('area.search');
OR
If you don't want to add CSRF Token To A Form, You can add that to the route file.
Attaching The CSRF Filter To A Route
Route::post('profile', array('before' => 'csrf', function()
{
//
}));
For More Details, you can refer to https://laravel.com/docs/4.2/html
I am learning Laravel 5.7.15.
I am trying to update data in Laravel. When I update client comment, I get MethodNotAllowedHttpException.
I have already looked at the other posts related to this error but still now get it fixed, please help me.
Laravel drives me crazy.
Here is my html
{!! Form::open(['url' => '/client_report/'.$id.'/edit', 'class' => 'form-horizontal group-border-dashed col-lg-6' ]) !!}
{{ csrf_field() }}
<div class="form-group">
{{Form::text("Comment",$client->client_comments, array('id'=>'comment' 'class' => 'form-control', 'disabled' => 'disabled', 'placeholder'=>'Client Comments')) }}
<p>{{Form::submit('Submit',['class'=>'btn btn-space btn-success'}}</p>
</div>
and Route has
Route::get('/client_report/{id}/{edit}',function($id) {
return view('clientEdit')
->with('id',$id);
})->middleware('auth');
Route::post('/client/submit/{id}/edit', ['uses' => 'clientController#editClient']);
and Controller has
class clientController extends Controller {
function editClient(Request $request, $id) {
$client = Client::find($id);
$client->comment = $request->get('comment');
$client->save();
}
}
Any help will be greatly appreciated.
You are hitting the wrong url.
In your html you are using
Form::open(['url' => '/client_report/'.$id.'/edit' ...
But your update route is
Route::post('/client/submit/{id}/edit' ...
Change the URL in your form, also make sure to make a POST request instead of GET.
Updating a resource should have PUT/PATCH route according to restful convention.
PS: current laravel version is 7.x, I would recommend you learn laravel 6.x at least, and HTML From Collectives (as far as I remember that's what they are called) are deprecated. You should not use deprecated tech.
I think the url you're passing here is wrong.
{!! Form::open(['url' => '/client_report/'.$id.'/edit', 'class' => 'form-horizontal group-border-dashed col-lg-6' ]) !!}
This above method is for edit, while you click on submit button it should redirect to /client/submit/{id}/edit this url.
Make you form url as below.
{!! Form::open(['url' => '/client/submit/'.$id.'/edit', 'class' => 'form-horizontal group-border-dashed col-lg-6' ]) !!}
01. first change your router method to PUT
Route::put('/client/submit/update/{id}', ['uses' => 'clientController#editClient']);
02. change your form
{!! Form::open(['action' => ['clientController#editClient', $id ],'method' => 'POST', 'class' => 'form-horizontal group-border-dashed col-lg-6' ]) !!}
{{Form::text("Comment",$client->client_comments, array('id'=>'comment' 'class' => 'form-control', 'disabled' => 'disabled', 'placeholder'=>'Client Comments')) }}
{{ Form::hidden('_method', 'PUT')}}
{{ Form::submit('submit', [ 'class' => 'btn btn-primary m-t-15 m-b-15'])}}
{!! Form::close() !!}
Change the route to:
Route::match(['put', 'patch'], '/client/submit/{id}', 'clientController#editClient');
And the form to:
{!! Form::open(['url' => '/client_report/'.$id, 'class' => 'form-horizontal group-border-dashed col-lg-6' ]) !!}
{{ csrf_field() }}
#method('PUT')
...
https://laravel.com/docs/master/routing#form-method-spoofing
I'm using LaravelCollective form to delete an employee:
{!! Form::open(['method' => 'DELETE', 'action' => ['EmployeesController#destroy', $employee->id, $company->id]]) !!}
<div class="form-group">
{!! Form::submit('Remove employee', ['class'=>'btn btn-danger']) !!}
</div>
{!! Form::close() !!}
and want to pass 2 arguments: employee id and company id.
My route:
Route::delete('/employees/{employee}/{company}', 'EmployeesController#destroy');
My controller function:
public function destroy($id, $companyId)
{
Employee::find($id)->delete();
if($companyId == 0)
return redirect('/employees');
else
return redirect('/companies/' . $companyId . "/edit");
}
I'm getting an error that I'm passing only 1 parameter. Where is the problem?
Try this instead:
// named route
Route::delete('employees/{employee_id}/{catetory_id}', 'EmployeesController#destroy')->name('employees.destroy');
// form using named route
{!! Form::open(['method' => 'DELETE', 'route' => ['employees.destroy', $employee->id, $company->id]]) !!}
<div class="form-group">
{!! Form::submit('Remove employee', ['class'=>'btn btn-danger']) !!}
</div>
{!! Form::close() !!}
You have to send the params in an array, try something like this.
{!! Form::open(['method' => 'DELETE', 'action' => ['EmployeesController#destroy', [$employee->id, $company->id] ]]) !!}
<div class="form-group">
{!! Form::submit('Remove employee', ['class'=>'btn btn-danger']) !!}
</div>
{!! Form::close() !!}
The variable name should match the route parameter. Try something like this in your controller:
public function destroy($employee, $company)
{
Employee::find($employee)->delete();
if($company == 0)
return redirect('/employees');
else
return redirect('/companies/' . $company . "/edit");
}
}
In the admin area of my app, I want to have a form by which an admin can find a project by its id and show the project details there. What is the best approach to implement this?
This is what I have tried:
//route
Route::get('admin/projects/{project_id}', 'AdminController#showProject');
//form
{!! Form::open(['action' => 'AdminController#showProject', 'method' => 'get']) !!}
{!! Form::label('project_id', 'Project Id', ['class' => 'control-label']) !!}
{!! Form::text('project_id', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'form-control']) !!}
{!! Form::close() !!}
//controller method
public function showProject(Request $request)
{
$project=Project::find($request->get('project_id'));
return view('admin.projects.showProject', compact('project'));
}
It almost worked but there is little problem. After retrieving the requested project, the ULR is like this:
admin/projects/%7Bproject_id%7D?project_id=5
I want it be like this one:
admin/projects/5
How can I solve this problem?
Create the following routes:
Route::get('admin/projects', 'AdminController#getProject');
Route::post('admin/projects', 'AdminController#postProject');
Route::get('admin/projects/{project_id}', 'AdminController#showProject');
In your getProject function you return a view that show the form where the user can enter an ID. (The one you already have):
{!! Form::open(['action' => 'AdminController#postProject', 'method' => 'post']) !!}
{!! Form::label('project_id', 'Project Id', ['class' => 'control-label']) !!}
{!! Form::text('project_id', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'form-control']) !!}
{!! Form::close() !!}
In your postProject function you just send a redirect to admin/project/{project_id} URL:
public function postProject(Request $request)
{
return redirect('admin/projects/' . $request->project_id);
}
In your showProject function you just retrieve the record and return a view with the information:
public function showProject($ProjectID)
{
$project=Project::find($ProjectID);
return view('admin.projects.showProject'),
->with('Project', compact('project'));
}
Try changing your controller method to
public function showProject($project_id)
{
$project=Project::find($project_id);
return view('admin.projects.showProject', compact('project'));
}
You don't need to use request on get route.