Currently i'm working on a project to manage my cost on fuel.
Now i try to pass 2 parameters in a Form::open() which sadly doesn't work.
The reason why i think i need to pass 2 parameters at once is because my url is Sitename/car/{id}/tank/{id}
What am i doing wrong?
edit.blade.php
Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('TankController#update', array($aid, $id))))
Problem Code
'action' => array('TankController#update', array($aid, $id)
-Results in the following error:
Parameter "tank" for route "car.{id}.tank.update" must match "[^/]++" ("" given) to generate a corresponding URL.
TankController.php
public function edit($id, $tid)
{
$tank = Tank::find($tid);
if(!$tank) return Redirect::action('TankController#index');
return View::make('Tank.edit', $tank)->with('aid', $id);
}
public function update($id, $tid)
{
$validation = Validator::make(Input::all(), Tank::$rules);
if($validation->passes()){
$tank = Tank::find($tid);
$tank->kmstand = Input::get('kmstand');
$tank->volume = Input::get('volume');
$tank->prijstankbeurt = Input::get('prijstankbeurt');
$tank->datumtank = Input::get('datumtank');
$tank->save();
return Redirect::action('TankController#index', $id)->with('success', 'Tankbeurt succesvol aangepast');
} else return Redirect::action('TankController#edit', $id)->withErrors($validation);
}
Route.php
Route::resource('car', 'CarController');
Route::resource('car/{id}/tank', 'TankController');
Route::controller('/', 'UserController');
-Url Structure
SITENAME/car/2/tank/2/edit
I've also looked into the api documents but found nothing.
http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html
Thanks in advance
Try this:
Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('TankController#update', $aid, $id)))
This is the best solution
Form::open (array ('method'=>'put', 'route'=>array($routeName [,params...]))
An example:
{{ Form::open(array('route' => array('admin.myroute', $object->id))) }}
https://github.com/laravel/framework/issues/491
Try to use in your blade:
For Laravel 5.1
{!! Form::open([
'route' => ['car.tank.update', $car->id, $tank->id],
'method' => 'put',
'class' => 'form-horizontal'
])
!!}
Related
I want to save some form data, and I'm Getting error.
The Error
Action App\Http\Controllers\Admin\ConcursoController#store not defined. (0)
My Form
{!! Form::open(['action'=>'Admin\ConcursoController#store', 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('company','Entidade')}}
{{Form::text('company','',['class' => 'form-control', 'placeholder' => 'Nome da entidade aquĆ..'])}}
</div>
{{Form::submit('submeter', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
My Route
$this->group(['middleware' => ['auth:admin'], 'namespace' => 'Admin', 'prefix' => 'admin'], function(){
$this->get('/', 'AdminController#index')->name('admin.home');
$this->resource('concursos', 'ConcursoController');
});
Controller index Method
public function index()
{
$concursos = Concurso::all();
$title = 'Concursos';
return view('admin.concursos.index',compact('title'))->with('concursos',$concursos);
}
Controller Create method
public function create()
{
return view('admin.concursos.create');
}
Controller Store Method
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
]);
//Criar concurso
$concurso = new Concurso;
$concurso->title = $request->input('title');
$concurso->body = $request->input('body');
$concurso->save();
return redirect('/admin/concursos')->with('Success', 'Concurso Adicionado');
}
Laravel version 5.7.14
Probably check this file: App\Http\Controllers\Admin\ConcursoController and see if you have a function/method called 'store'. The error is pretty straightforward that script can't find that function.
replace your form
['route' => ['concursos.store']
like
{!! Form::model($transactions, ['route' => ['transaction.store'], 'method' => 'POST','class'=>"form-horizontal"]) !!}
There is a route:
Route::get('message/{id}/edit', ['uses' => 'HomeController#edit', 'as' => 'message.edit'])->where(['id' => '[0-9]+']);
Code in my HomeController:
public function edit($id) {
$data = [
'title' => 'page',
'pagetitle' => 'page',
'message' => Message::find($id)
];
return view('pages.messages.edit', $data);
}
And a view:
#extends('index')
#section('content')
{!! Form::open(['route' => 'message.edit']) !!}
#include('_common._form')
{!! Form::close() !!}
#stop
And I get this error:
ErrorException in UrlGenerationException.php line 17:
Missing required parameters for [Route: message.edit] [URI: message/{id}/edit]. (View: C:\OpenServer\domains\laravel\book\resources\views\pages\messages\edit.blade.php)
Where is the problem?
I tried to remove form from code, it works well. So it means that something wrong with the form.
You need to pass an ID, because your route demands it.
It should be like the following:
{!! Form::open(['route' => ['message.edit', $message]]) !!}
I have weird route error in Laravel. Stucked fixing it.
Here is my action (it is in simplified version).
public function ibf() {
$existing_board_id = Input::get('existing_board_id');
...
return Redirect::route('board.index')->with('success', 'Board is successfully imported.');
}
Form, here $board is new Model, not saved one, but with pre-defined values.
{{ Form::model($board, ['route' => 'board.ibf', 'method' => 'POST', 'class' => 'form-horizontal']) }}
routes.php
Route::group(array('prefix' => 'board'), function() {
Route::post('/ibf/', array(
'as' => 'board.ibf',
'uses' => 'BoardController#ibf'
));}
Error:
protected function methodNotAllowed(array $others)
{
throw new MethodNotAllowedHttpException($others);
}
Everything looks simple, but I really dont understand the problem.
my problem is that when i submit my form it pass through the get method.
The workflow is form submit -> get -> post and it should be form submit -> post.
I need to have a condition in my get method to validate de array is not null
My code:
Routes
Route::get('/pre-register-birth',array('as'=>'pre-register-birth', 'uses'=>'UserController#preRegisterBirthData'));
Route::post('/pre-register-birth', 'UserController#preRegisterBirthDataPost');
View
{{ Form::open(array('method' => 'POST', 'action' => 'UserController#preRegisterBirthDataPost',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
Controller
public function preRegisterBirthData()
{
$user = Session::get('user');
if ($user)
return View::make('user/pre-register-birth')->with('tempUser', $user);
else
return Redirect::Route('pre-register-get');
}
public function preRegisterBirthDataPost()
{
$validator = Validator::make(Input::all(),
array(
'birthplace' => 'required|max:100',
'birthdate' => 'required|max:100|date|date_format:Y-m-d'
)
);
if ($validator->fails()) {
return Redirect::Route('pre-register-birth')
->withErrors($validator)
->withInput();
} else {
$user = array(
'email' => Input::get('email'),
'pass' => Input::get('pass'),
'name' => Input::get('name'),
'surname' => Input::get('surname'),
'birthplace' => Input::get('birthplace'),
'birthdate' => Input::get('birthdate'),
'hourKnow' => Input::get('hourKnow'),
'dataCorrect' => Input::get('dataCorrect'),
'news' => Input::get('news'),
);
return Redirect::Route('pre-register-terms')->with('user', $user);
}
}
I think I've seen this issue before. Laravel for whatever odd reason doesn't like the action => ... So, change your form declaration from a to b:
// A
{{ Form::open(array('method' => 'POST', 'action' => 'UserController#preRegisterBirthDataPost',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
// B
{{ Form::open(array('method' => 'POST', 'url' => 'pre-register-birth',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
Notice I changed action => ... to url => ... It's a small change, but it might solve it. However, you may need to add in:
Route::post('/pre-register-birth', array('as'=> 'pre-register-birth', 'uses' => 'UserController#preRegisterBirthDataPost'));
So it recognizes the named route.
Hope this helps!
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')) }}