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.
Related
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 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');
I'm learning Laravel, and I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car, which is identified with the modelesc variable. This form sends the data to a "orders" table. But I keep getting this error "Action App\Http\Controllers\orders not defined." on catalog.blade.php
This is the code that I have made.
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders($modelesc) {
$cars = DB::select('select * from cars where Model = modelesc');
return view('orders', compact('cars'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'orders', 'method' => 'GET')) !!}
{!! Form::hidden(modelesc, $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
#endforeach
Orders.blade.php
{!! Form::open(array('action' => 'index', 'method' => 'POST')) !!}
{!! Form::text(Model, $modelesc) !!}
{!! Form::hidden(users_id, Auth::user()->id) !!}
{!! Form::hidden(Fabrication_date, date(Y-m-d)) !!}
{!! Form::select('colour', [
#foreach($colours as $colour)
'$colour->Colour' => '$colour->Colour'
#endforeach
]) !!}
{!! Form::hidden(Order_status_id, '1' !!}
{!! Form::close() !!}
This is the structure of the table 'orders'. The _id come from other tables, and I want to fill some values of the forms with this id's.
id,
users_id,
Model,
Fabrication_date,
Colour_id,
Order_status_id
Laravel does not know where to find the action orders, because you did not specify a controller (see the thrown exception: it is looking for some class named orders in the namespace App\Http\Controllers\). To do so, you just need to replace
{!! Form::open(array('action' => 'orders', 'method' => 'GET')) !!}
with
{!! Form::open(array('action' => 'CarController#orders', 'method' => 'GET')) !!}
If you want to use a route instead (e.g. you have defined a route named orders which links to a Controller action) you need to replace action with route in the Form::open method:
{!! Form::open(array('route' => 'orders', 'method' => 'GET')) !!}.
This is my route:
Route::post('admins.login', 'AdminsController#login');
This is my form
{{ Form::open(array('route' => 'admins.login', 'class' => 'loginClass', 'method' => 'post')) }}
This is the exception:
Route [admins.login] not defined.
The method:
public function login(){
echo "Save Time";exit;
}
Edit
I already tried making / instead of . in all situations
Route definition for a named route:
Route::post('admins/login', array('uses' => 'AdminsController#login', 'as' => 'admins.login'));
Form:
{{ Form::open(array('route' => 'admins.login', 'class' => 'loginClass')) }}