Laravel parameter issue - php

Missing argument 2 for App\Http\Controllers\Company\OrderController::store()
I got this error in my store controller, my form is passing 2 parameters but the second one is not found.
Route: Route::resource('order', 'OrderController');
$company gets converted to a model in the controller.
The form:
<form class="form-horizontal" role="form" method="POST" action="{{action('Company\OrderController#store', [$company,$orderid])}}">
{{ csrf_field() }}
<button type="submit" class="btn btn-primary">Accept</button>
</form>
Any ideas?
Thanks!

If you created store route with Route::resource() it doesn't expect any parameters and should look like this:
public function store(Request $request)
So, you need to pass data using hidden inputs, like:
{!! Form::hidden('data', 'some data') !!}
And then get data in controller with:
$data = $request->data;

You should specify the key-value pair like this:
['company_id' => $company->id, 'order_id' => $order->id]
So your form would look like:
<form action="{{ action('Company\OrderController#store', ['company_id' => $company->id, 'order_id' => $order->id]) }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-primary">Accept</button>
</form>
Hope this helps!

Related

gor error message when deleteing comment in Laravel project

working with Laravel 6 project and I have following CommentController,
public function update(Request $request, $id)
{
$comment = Comment::find($id);
$this->validate($request, array('comment' => 'required'));
$comment->comment = $request->comment;
$comment->save();
Session::flash('success','Comment Created');
return redirect()->route('posts.show', $comment->post->id);
}
public function delete($id)
{
$comment = Comment::find($id);
return view('comments.delete')->withComment($comment);
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$comment = Comment::find($id);
$post_id = $comment->post->id;
$comment->delete();
Session::flash('success','Deleted Comment');
return redirect()->route('posts.show', $post_id);
}
and My routes are as following
Route::delete('comments/{id}', ['uses' => 'CommentsController#destroy', 'as' => 'comments.destroy']);
Route::get('comments/{id}/delete', ['uses' => 'CommentsController#delete', 'as' => 'comments.delete']);
but when I try to delete comment got following validation error message
The comment field is required.
how could I fix this problem here?
edit.blade.php
#section('content')
<div class="col-sm-6">
<form action="{{ route('comments.destroy', $comment->id) }}" method="post">
#csrf
{{ method_field('PUT') }}
<button type="submit" class="btn btn-danger btn-block">Delete</button>
</form>
</div>
</div>
</div>
#endsection
Because HTML forms can't send PUT, PATCH and DELETE requests, we must "spoof" the request and include an input field that tells Laravel which type it is sending. You can read about that here.
Technically, this means inserting the following for your delete form.
<input type="hidden" name="_method" value="delete">
Laravel comes with different helpers that help you achieve this. In your update form, you have already done this by adding the method_field('PUT');. Here, you are instructing Laravel that this is a PUT request, and we need to do this too in your delete form.
The {{ method_field('PUT') }} simply needs to change to {{ method_field('DELETE') }} or you can use the built-in #method('delete')
Like this
#section('content')
<div class="col-sm-6">
<form action="{{ route('comments.destroy', $comment->id) }}" method="post">
#csrf
#method('delete')
<button type="submit" class="btn btn-danger btn-block">Delete</button>
</form>
</div>
</div>
</div>
#endsection
You can inspect your form HTML in the browser and notice the hidden input field added automatically.
$this->validate($request, array('comment' => 'required'));
In your blade view, if you are not posting a comment in your form, you will be facing this error The comment field is required
Make sure you are posting a comment, or any typo's.
Posting your blade view would help us alot more.

Resource route call to wrong method

I have created route with "resource". When I try to use delete method it always going to show method.
Route list
Route call
<a class="btn btn-danger" href="{{ route('languages.destroy', ['language' => $language->id]) }}">Delete</a>
Delete method
public function destroy($language){
$lang = Language::findOrFail($language);
$lang->delete();
session()->flash('flash_message', 'The language has been
removed!');
return redirect(route('languages.index'));
}
So how to fix it?
Thank you!
Since it goes to GET method because you are not deleting using form .
route('languages.destroy',['language' => $language->id])
the above route only generate url .so if you are using
delete
then it treat as get method.So you have to use
<form method="POST" action="{{ route('languages.destroy',['language' => $language->id]) }}">
#csrf
#method("delete")
<button type="submit">Delete</button>
</form>
In your blade:
<form action="{{ route('languages.destroy',$language->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>

Binding form action to controller method

I am trying to bind form action(which has id related to a model/table record) to controller method.
My web.php has
Route::post('/rejectControlTransfer/{id}', 'ControlTransferController#rejectControlTransfer')->name('controltransfers.rejectTransfer');
My form has
<form id='form_process_rejectControl' action="{{route('controltransfers.rejectTransfer', [$controlTransferId])}}" method="POST" style="display: inline;">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
And my controller has
public function rejectControlTransfer(Request $request, ControlTransfer $controlTransfer)
{
dd($controlTransfer->id);
}
I am trying to bind ControlTransfer $controlTransfer with the actual id passed so that when I try get value of $controlTransfer->id or $controlTransfer->name would give me their values.
Current I am not getting any value.
If you are using id in the route '/rejectControlTransfer/{id} then you can only access it through $id variable in your controller, which is a raw variable int.
Besides that, your action's 'route' function is not used correctly, you need to put 'id' as your key, like:
route('controltransfers.rejectTransfer', ['id' => $controlTransferId])
However, if your ControlTransfer is a model, you can use Model Binding. By:
Route::post('/rejectControlTransfer/{controlTransfer}', 'ControlTransferController#rejectControlTransfer')->name('controltransfers.rejectTransfer');
<form id='form_process_rejectControl' action="{{route('controltransfers.rejectTransfer', ['controlTransfer' => $controlTransferId])}}" method="POST" style="display: inline;">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
public function rejectControlTransfer(Request $request, ControlTransfer $controlTransfer)
{
dd($controlTransfer->id);
}
Disclaimer: Above code is untested.

Laravel Exception 405 MethodNotAllowed

I'm trying to create a new "Airborne" test in my program and getting a 405 MethodNotAllowed Exception.
Routes
Route::post('/testing/{id}/airbornes/create', [
'uses' => 'AirborneController#create'
]);
Controller
public function create(Request $request, $id)
{
$airborne = new Airborne;
$newairborne = $airborne->newAirborne($request, $id);
return redirect('/testing/' . $id . '/airbornes/' . $newairborne)->with(['id' => $id, 'airborneid' => $newairborne]);
}
View
<form class="sisform" role="form" method="POST" href="{{ URL::to('AirborneController#create', $id) }}">
{{ csrf_field() }}
{!! Form::token(); !!}
<button type="submit" name="submit" value="submit" class="btn btn-success">
<i class="fas fa-plus fa-sm"></i> Create
</button>
</form>
According to my knowledge forms don't have a href attribute. I think you suppose to write Action but wrote href.
Please specify action attribute in the form that you are trying to submit.
<form method="<POST or GET>" action="<to which URL you want to submit the form>">
in your case its
<form method="POST" ></form>
And action attribute is missing. If action attribute is missing or set to ""(Empty String), the form submits to itself (the same URL).
For example, you have defined the route to display the form as
Route::get('/airbornes/show', [
'uses' => 'AirborneController#show'
'as' => 'airborne.show'
]);
and then you submit a form without action attribute. It will submit the form to same route on which it currently is and it will look for post method with the same route but you dont have a same route with a POST method. so you are getting MethodNotAllowed Exception.
Either define the same route with post method or explicitly specify your action attribute of HTML form tag.
Let's say you have a route defined as following to submit the form to
Route::post('/airbornes/create', [
'uses' => 'AirborneController#create'
'as' => 'airborne.create'
]);
So your form tag should be like
<form method="POST" action="{{ route('airborne.create') }}">
//your HTML here
</form>
MethodNotAllowedHttpException signposts that your route isn't available for the HTTP request method specified. Perhaps either because it isn’t defined correctly, or it has a conflict with another similarly named route.
Named Routes
Consider using named routes to allow for the convenient generation of URLs or redirects. They can generally be much easier to maintain.
Route::post('/airborne/create/testing/{id}', [
'as' => 'airborne.create',
'uses' => 'AirborneController#create'
]);
Laravel Collective
Use Laravel Collective's Form:open tag and remove Form::token()
{!! Form::open(['route' => ['airborne.create', $id], 'method' =>'post']) !!}
<button type="submit" name="submit" value="submit" class="btn btn-success">
<i class="fas fa-plus fa-sm"></i> Create
</button>
{!! Form::close() !!}
dd() Helper Function
The dd function dumps the given variables and ends execution of the script. Double-check your Airborne class is returning the object or id you expect.
dd($newairborne)
List available routes
Always make sure your defined routes, views, and actions match up.
php artisan route:list --sort name
First of All
Form don't have href attribute, it has "action"
<form class="sisform" role="form" method="POST" action="{{ URL::to('AirborneController#create', $id) }}">
Secondly
If the above change doesn't work, you can make some changes like:
1. Route
Give your route a name as:
Route::post('/testing/{id}/airbornes/create', [
'uses' => 'AirborneController#create',
'as' => 'airborne.create', // <---------------
]);
2. View
Give route name with route() method in form action rather than URL::to() method:
<form class="sisform" role="form" method="POST" action="{{ route('airborne.create', $id) }}">

Add method 'put' to a url in laravel 4.2

I'm trying to change the status of a database record with just a button click so far I have this:
view
<td>
<a class="btn btn-small btn-warning" href="{{ URL::to('brands/'.$value->BrandID.'/archive') }}">Archive </a>
</td>
controller
public function archive($id)
{
$rules= array ('BrandName' =>'required | max:20',);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails())
{
return Redirect::to('brands.view')
->withErrors($validator);
} else {
DB::table('tbl_brands')->where('BrandID' , $id)
->update(
array
(
'Status' => 'Archived'
));
Session::flash('message','Successfully Archived!');
return Redirect::to('brandsview');
}
}
and the route
Route::put('brands/{id}/archive', array('as' => 'Brandarch', 'uses'=>'BrandsController#archive'));
and my error what method exception. I scrolled down a bit and saw that in the errors, the http request is 'get' which I know should be 'put' any ideas on how to properly execute this?
You will need to change your hyperlink to a submit form in a form with hidden field with name _method, only this way you can control HTTP method used.
For example:
<form action="{{ URL::to('brands/'.$value->BrandID.'/archive') }}" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" value="Archive">
</form>

Categories