Laravel wrong bind of update method with Request param - laravel

I've a problem with routing calling a "update" (PUT) method from a form with Laravel.
Routing doesn't work if I put Illuminate\Http\Request param in "update" because method "show" method is called instead, even if 'method' => 'put' is present in the form.
Removing Request param everything works fine.
Below my code.
web.php
Route::resource('profile', 'ProfileController');
ProfileController.php
public function update(Request $request, $id)
{
...
}
public function show($id)
{
...
}
index.blade.php
{!! Form::model($profile, array('action' => array('ProfileController#update', $profile->id), 'method' => 'put', 'class' => 'form-horizontal')) !!}
Could you help me to understand where what's wrong?
Thanks in advance.

Related

Laravel5.7 routing using Route:match not working

I'm using Laravel 5.7 I'm trying to route my function for get and post.
I want to load a view and post a form.
As I have studied
Route::match(['GET','POST'], '/', TestController#test);
Route::any('/', TestController#test);`
one of these should work.
But its not working for me,is there any other way or I'm doing something wrong?
UPDATE
Route to admin:
Route::match(['get','post'], 'cp/', 'AdminController#test');
Function in Admin controller:
public function test( Request $request){
$data=array();
if ($request->isMethod('POST')) {
echo "here it is";
exit;
}else{
echo "still in get!";
}
return view('admin/login', $data);
}
And my view in short is something like this:
<form action="{{ url('/cp') }}" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<form>
Can you try changing
Route::match(['GET','POST'], '/', TestController#test);
to
Route::match(['GET','POST'], '/', 'TestController#test');
OR
Route::any('/', TestController#test);
to
Route::any('/', 'TestController#test');
The second param should be wrapped in quotes!
UPDATE:
Your route match code should be something like this :
Route::match(array('GET', 'POST', 'PUT'), "/", array(
'uses' => 'Controller#index',
'as' => 'index'
));
Try this in you web.php
Route::match(['get', 'post'], '/testMethods', function ()
{
dd('its workong bro');
});
And hit the yourprojectname/testMethods in your web browser
Eg: http://localhost:8000/testMethods
From Illuminate\Contracts\Routing\Registrar.php
public function match($methods, $uri, $action);
Here is match function parameter list
Parameter One List of methods: Eg: get,post,put,patch
Parameter two url : Eg: /testMethods
Parameter three Method: Eg: TestController#test
Route::match(['get', 'post'], '/testMethods','TestController#test');
Well, by the end what I understand to use route::match I should specify function name without it it will not work.So when I changed it to Route::match(array('GET', 'POST', 'PUT'), "/login", array(
'uses' => 'AdminController#login',
'as' => 'login'
));
It resolves the issue. Thank for the help everyone!!

Passing Object between Models

I have a a couple of Models like so (some code removed for simplicity)
class Poll extends Model
{
public function poll()
{
return $this->hasOne('App\PollQuestion', 'pollId');
}
}
class PollQuestion extends Model
{
public function poll()
{
return $this->belongsTo('App\Poll');
}
}
I then have all my routes set up, for the PollQuestion they currently look like this
Route::model('polls.questions', 'PollQuestion');
Route::get('/admin/polls/{id}/addquestions', [
'as' => 'polls.questions.create',
'uses' => 'PollQuestionController#addQuestions'
]);
Route::post('/admin/polls/{id}/storequestions', [
'as' => 'polls.questions.store',
'uses' => 'PollQuestionController#storeQuestion'
]);
In my PollQuestionController, to see the questions view I have
public function addQuestions(Request $request)
{
$poll = Poll::where('id', '=', $request->id)->first();
return view('poll.admin.questions', compact('poll'));
}
Inside this view if I dump the poll e.g.
{{ dd($poll) }}
I can see what I expect to see. For the question form, I am doing
{!! Form::model(new App\PollQuestion, [
'route' => ['polls.questions.store', $poll]
]) !!}
So I presume that should pass my store function the Poll Object I previously dumped. However, in the store function, I do
public function storeQuestion(Request $request, Poll $poll) {
dd($poll);
}
And it shows Null. Why would this happen seeing that I am passing it the Poll Object I had previously dumped?
Thanks
You need use Route Model Binding correctly. According to Laravel Documentation
Route::model('poll-question', App\PollQuestion::class);
Route::get('/admin/polls/{poll-question}/addquestions', [
'as' => 'polls.questions.create',
'uses' => 'PollQuestionController#addQuestions'
]);
Route::post('/admin/polls/{poll-question}/storequestions', [
'as' => 'polls.questions.store',
'uses' => 'PollQuestionController#storeQuestion'
]);
Route::model define a variable in the route like a Model. This method searches the primary key passed in the route and retrieve the Model.

Passing a param containing a path to a controller

I'm trying to pass a variable who contains a path from a form to a controller function in Laravel 4, with the purpose of download an image. I tried a lot of things but nothing worked for me. If I don't pass the parameter in the route, I get a missing parameter error, and if I pass the parameter in the route, I get a NotFoundHttpException.
Here's my code:
View Form:
{{ Form::open(array('route' => array('download', $myPath))) }}
{{ Form::submit('Download', array('class' => 'generator')); }}
{{ Form::close() }}
Route:
Route::post('/download/{myPath}', array('uses' => 'ImagesController#download', 'as' => 'download'));
ImagesController function:
public function download($myPath){
return Response::download($myPath);
}
When I click the submit I'm obtaining a URL like this with NotFoundHttpException:
http://localhost/resizer/public/download/images/myimage.jpg
I don't understand what I'm missing.
You are actually posting two variables. Try this instead
Route::post('/download/{myFolder}/{myFile}', array('uses' => 'ImagesController#download', 'as' => 'download'));
and in your controller
public function download($myFolder, $myFile){
return Response::download($myFolder.'/'.$myFile);
}

Get the model ID when updating a resource

I have a form that will submit a Patch request to the controller's update method.
But the update method requires to have $id, as you can see below whenever I try that I get a
No query results for model [Item]. Since the update method did not receive the $id of the model
public function update($id)
{
$item = Item::findOrFail($id);
$update = Input::all();
// some codes to save changes
return Redirect::route('items.index');
}
Another thing is that whenever I submit the form, url turns into something like this:
mw.dev/items/%7Bitems%7D
Edit
routes.php
Route::resource('items','ItemsController');
ItemController
public function edit($id)
{
$item = Item::findOrFail($id);
return View::make('items.edit')->with('item',$item);
}
I have included the code on my edit.blade.php
{{Form::open(array('route' => 'items.update', 'method'=>'patch'))}}
{{Form::text('barcode', $item->barcode, array('placeholder' => 'barcode'))}}
{{Form::text('imei',$item->imei, array('placeholder' => 'imei'))}}
{{Form::text('item_name', $item->item_name, array('placeholder' => 'item name'))}}
{{Form::submit('edit')}}
{{Form::close()}}
You have to pass the model to your view and need to pass the id parameter when generating the form. Assume that you have a User model and it's available in the view. So you may generate the form action using something like this:
// Using Form::open method with route
Form::open(array('route' => array('route.name', $user->id)))
// Using Form::model method with route
Form::model($user, array('route' => array('route.name', $user->id), 'method' => 'patch'))
// Using Form::open method with action
Form::open(array('action' => array('Controller#update', $user->id), 'method' => 'patch'))
// Using Form::open method with url
Form::open(array('url' => 'something/update/' . $user->id, 'method' => 'patch'))
Check more on Opening A Form.
The URL
mw.dev/items/%7Bitems%7D
is most likely the url-encoded form of
mw.dev/items/{items}
I suppose there is a problem in the form submission or in the <form>'s action paremeter or in the Route::* declaration in routes.php.
This could also explain why you don't get any $id upon submission.

Laravel 4: pointing a form to a controller function

I can't understand, how to set up the Form action to direct to a function of a specific controller.
This is my blade code:
{{ Form::open(array('route'=>'user.search')) }}
But I get this error :
Unable to generate a URL for the named route "user.search" as such route does not exist.
the controller (UserController) has a function with this prototype
public function search(){ ... }
I have also tried to set up a route like this in route.php
Route::post('user/search', 'UserController#search');
What is wrong with this code?
You can do it like
{{ Form::open( array('url' => URL::to('user/search')) ) }}
Because you don't have a name for the route. To define a name for the route, use following syntax,
Route::post('user/search', array( 'as' => 'userSearch', 'uses' => 'UserController#search' ));
So, you can use the route by it's name, as
{{ Form::open( array('route' => 'userSearch') ) }} // 'search' method will be invoked
Also, you can directly use the action of a controller as
{{ Form::open( array('action' => 'UserController#search') ) }}
Check Routing and Form.

Resources