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.
Related
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.
This is my pubf in my Controller which handles the request:
public function addPermissionsToRoleHandler($rid, Request $request)
{
$role = Role::find($rid);
dd($request->permissions);
foreach($request->permissions as $perm)
{
$permission = Permission::find($perm->id);
$role->attachPermission($permission);
}
return redirect()->route('showSpecificRole', $rid);
}
This is how I defined my multiple select-box:
{!! Form::select('permissions[]', $permissions, null, array('class' => 'selectpicker show-tick', 'data-live-search' => 'true', 'id' => 'permission_select', 'multiple' => true)) !!}
Why is my dd() returning null? $request->permission is empty. dd($request) only has token_.
I don't have the rep to comment so am posting as an answer. I am not sure, but I think it may be because the Request needs to be the first argument ?
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);
}
I have a general question.
I have a search form in larvel which returns results form the database.
in these i have an input field to enter a price if price is == 0
what my problem is when i enter price and submit it returns to the search page without my previous search results i.e it doesn't refresh the same page with results and the newly updated field etc.
form in view
{{ Form::open(['action' => 'price_input'])->with($gyms) }}
{{ Form::text('enter_price', null, ['class' => 'form-control', 'size' => '50', 'id' => 'enter_price', 'autocomplete' => 'on', 'runat' => 'server', 'required' => 'required', 'placeholder' => 'enter price!', 'style' => 'margin-bottom: 0px!important;']) }}
{{ Form::submit('Search', ['class' => 'btn btn- primary', 'style' => 'margin-left: 10px;']) }}
{{ Form::close() }}
route
Route::post('/', [ //not used yet
'as' => 'price_input',
'uses' => 'PagesController#priceUpdate'
]);
Model
public function priceUpdate($gyms)
{
if (Input::has('enter_price'))
{
$price = Input::get('enter_price');
Gym::updatePrice($price);
return Redirect::back()->withInput();
}
Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gym);
}
not bothering with model as that works fine.
any ideas guys?
Thanks for your answer,
i have changed my controller to this
public function priceUpdate($gyms)
{
if (Input::has('enter_price'))
{
$price = Input::get('enter_price');
Gym::updatePrice($price);
$gyms = Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gyms);
}
$gyms = Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gyms);
}
but when i run it i get
Missing argument 1 for PagesController::priceUpdate()
with the $gyms being passed into the method.
if i take out the $gyms that goes away but not sure if its still being passed with session or not, sorry im a novice.
orignally i had a search box which when run returns
return View::make('pages.home')->with($data);
what is the difference between that and
return View::make('pages.home')->with($data);
when i do the above line it returns to the search page with no search options from before update the form, any ideas?
Currently, you are just retrieving an existing session and doing nothing with it. You need to do:
$gyms = Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gyms);
Or
return Redirect::to('pages.home')->with('gyms', Session::get('gyms'));
Then you can access the gyms in the view with $gyms.
Alternatively, you could access Session::get('gyms') in the view as well.
Also, not sure if it's just the way you pasted it here, but you have an unnecessary space before the ->with. Just wanted to make sure that's not part of the issue, too!
First my master.blade file include menu bar using this
Include Blade file menu.blade.php
#include('menu')
But Finally I realize to send some data from db to menubar, then I create controller, Controller name is MenuController, then I create route "admin-menu". Now I want to include that link to my master blade. how to do that thank you,
To pass data to a view from either a route closure or a controller you do one of the following:
$now = \Carbon\Carbon::now();
View::make('my-view', ['name' => 'Alex', 'date' => $now]); // pass data into View:::make()
View::make('my-view')->with(['name' => 'Alex', 'date' => $now]); // pass data into View#with()
View::make('my-view')->withName('Alex')->withDate($now); // use fluent View#with()
So you'd just use them in the View::make() call as you presumably already are:
// in a route closure
Route::get('some-route', function () {
return View::make('menu', ['name' => 'Alex']);
});
// in a controller
public function someRoute()
{
return View::make('menu', ['name' => 'Alex']);
}
Interestingly, in a lot of frameworks/templating systems, if you wanted to include a partial, you'd pass the data you want to be available in that partial in the partial call, but Laravel doesn't quite do this. For example in a made-up system you may have something like this:
// in controller:
$this->render('home', ['name' => 'Alex', 'age' => 30]);
// home.php
<?php echo $name; ?>
<?php echo $this->partial('home-age', ['age' => $age]); ?>
// home-age.php
<?php echo $age; ?>
But in Laravel, all current view variables are automatically included into partials for you. Now I tend to like to specify the variables anyway (Blade does allow you to do this as above), and obviously it can be used to override a view variable:
// route:
return View::make('home', ['name' => 'Alex', 'age' => 30, 'gender' => 'male']);
// home.blade.php
{{ $name }}
#include('home-extra', ['age' => 20])
// home-extra.blade.php
{{ $age }}
{{ $gender }}
The above code would output:
Alex
20
male
So the age is overridden in the #include, but the un-overridden gender is just passed along. Hopefully that makes sense.