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]]) !!}
Related
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.
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"]) !!}
On successful form submission, I should be seeing 'in here' within the browser but I get re-directed to a page not found view:
Sorry, the page you are looking for could not be found.
My form control:
{!! Form::open(['url' => 'prize_draw_store', 'class' => 'wffm-form-module text-left']) !!}
{!! Form::label('fullname', 'Name:', array('class' => 'control-label')) !!}
{!! Form::text('fullname', null, array('required', 'class'=>'form-control text-box single-line', 'placeholder'=>'')) !!}
{!! Form::label('email_address', 'Email Address:', array('class' => 'control-label')) !!}
{!! Form::text('email_address', null, array('required', 'class'=>'form-control text-box single-line', 'placeholder'=>'')) !!}
<div class="form-submit-border">
{{ Form::submit('Submit', array('class' => 'btn btn-default')) }}
</div>
{!! Form::close() !!}
My routes are well-defined:
/routes/web.php
use App\Http\Controllers\PrizeDrawController;
use App\Http\Requests\PrizeDrawFormRequest;
Route::get('/',
['as' => 'prize_draw', 'uses' => 'PrizeDrawController#create']);
Route::post('/',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
PrizeDrawController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\PrizeDrawFormRequest;
class PrizeDrawController extends Controller
{
public function create() {
return view('front.pages.home');
}
public function store(PrizeDrawFormRequest $request) {
var_dump('in here');
return redirect('thankyou')->with('status', 'Form submitted!');
}
}
PrizeDrawFormRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PrizeDrawFormRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'email_address' => 'required|email',
'fullname' => 'required'
];
}
}
I'm expecting the code to flow like thus:
1. http://aacomp.local/prize_draw_store -- POST form submission
2. App\Http\Controllers\PrizeDrawController#store
3. PrizeDrawController::store(PrizeDrawFormRequest $request)
4. Redirect a user to thankyou page
We don't seem to reach step 3 though? I must be missing something basic -
are you able to help?
Thanks in advance.
The problem lies with:
Route::post('/',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
Changing this route to
Route::post('/foobar',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
and updating the form action:
{!! Form::open(['url' => 'foobar', ...
say - will get the desired results. Thanks to Darius. V for this.
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')) }}
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'
])
!!}