laravel route doesn't exist though it exits - php

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')) }}

Related

Having hard time understanding Forms from laravel collectives

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.

Routing correctly in Laravel

I am learning Laravel 5.7.15.
I am trying to update data in Laravel. When I update client comment, I get MethodNotAllowedHttpException.
I have already looked at the other posts related to this error but still now get it fixed, please help me.
Laravel drives me crazy.
Here is my html
{!! Form::open(['url' => '/client_report/'.$id.'/edit', 'class' => 'form-horizontal group-border-dashed col-lg-6' ]) !!}
{{ csrf_field() }}
<div class="form-group">
{{Form::text("Comment",$client->client_comments, array('id'=>'comment' 'class' => 'form-control', 'disabled' => 'disabled', 'placeholder'=>'Client Comments')) }}
<p>{{Form::submit('Submit',['class'=>'btn btn-space btn-success'}}</p>
</div>
and Route has
Route::get('/client_report/{id}/{edit}',function($id) {
return view('clientEdit')
->with('id',$id);
})->middleware('auth');
Route::post('/client/submit/{id}/edit', ['uses' => 'clientController#editClient']);
and Controller has
class clientController extends Controller {
function editClient(Request $request, $id) {
$client = Client::find($id);
$client->comment = $request->get('comment');
$client->save();
}
}
Any help will be greatly appreciated.
You are hitting the wrong url.
In your html you are using
Form::open(['url' => '/client_report/'.$id.'/edit' ...
But your update route is
Route::post('/client/submit/{id}/edit' ...
Change the URL in your form, also make sure to make a POST request instead of GET.
Updating a resource should have PUT/PATCH route according to restful convention.
PS: current laravel version is 7.x, I would recommend you learn laravel 6.x at least, and HTML From Collectives (as far as I remember that's what they are called) are deprecated. You should not use deprecated tech.
I think the url you're passing here is wrong.
{!! Form::open(['url' => '/client_report/'.$id.'/edit', 'class' => 'form-horizontal group-border-dashed col-lg-6' ]) !!}
This above method is for edit, while you click on submit button it should redirect to /client/submit/{id}/edit this url.
Make you form url as below.
{!! Form::open(['url' => '/client/submit/'.$id.'/edit', 'class' => 'form-horizontal group-border-dashed col-lg-6' ]) !!}
01. first change your router method to PUT
Route::put('/client/submit/update/{id}', ['uses' => 'clientController#editClient']);
02. change your form
{!! Form::open(['action' => ['clientController#editClient', $id ],'method' => 'POST', 'class' => 'form-horizontal group-border-dashed col-lg-6' ]) !!}
{{Form::text("Comment",$client->client_comments, array('id'=>'comment' 'class' => 'form-control', 'disabled' => 'disabled', 'placeholder'=>'Client Comments')) }}
{{ Form::hidden('_method', 'PUT')}}
{{ Form::submit('submit', [ 'class' => 'btn btn-primary m-t-15 m-b-15'])}}
{!! Form::close() !!}
Change the route to:
Route::match(['put', 'patch'], '/client/submit/{id}', 'clientController#editClient');
And the form to:
{!! Form::open(['url' => '/client_report/'.$id, 'class' => 'form-horizontal group-border-dashed col-lg-6' ]) !!}
{{ csrf_field() }}
#method('PUT')
...
https://laravel.com/docs/master/routing#form-method-spoofing

Routing error in laravel 5.2

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]]) !!}

Laravel Controller method not found in GET method

after define route for edit fields and create function in controller i get this error: Controller method not found.
My form:
{{ Form::open(array('route' => array('linksPlugin.edit',$linkFields->id))) }}
...
{{ Form::close() }}
My Route:
Route::controller(
'linksPlugin','linksPluginManagmentController',
array(
...
'getEditLink' => 'linksPlugin.edit',
...
)
);
Controller Action:
public function getEditLink($id){
print_r($id);
}
You may want to set the form method to GET it is probably defaulting to POST:
{{ Form::open(array(
'route' => array('linksPlugin.edit', $linkFields->id),
'method' => 'GET')) }}

Laravel 4 Form passing 2 parameters

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'
])
!!}

Categories