Laravel Controller method not found in GET method - php

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

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

Missing argument 2 for App\Http\Controllers\UserController::store()

Controller
public function store( Request $request,$id)
{
$new = Car::find($id);
$new->status = $request ->input('field');
$new->save();
redirect('home');
}
View
#foreach($users as $user)
#foreach($user->cars as $users)
{!! Form::open( ['route' => 'user.store', 'method'=>'post','id'=> '$users->id']) !!}
<th scope="row">1</th>
<td>{!! Form::label($cars->name)!!}<td>
<td>{!! Form::label($cars->age)!!}<td>
<td>{{Form::select($cars->status, ['R' => 'Requested', 'C' => 'Coming', ['name' => 'field']])}} // name is worong but I dont know the alternative
<td>{!! Form::submit('Update Profile', ['class' => 'btn btn-primary']) !!}</td>
{{ Form::close() }}
Route
Route::Resource('user', 'UserController');
Problem
Trying to save the selected value from status in Car model. but I get an error about the parameters. Can someone show me where my mistake is? I'm new to Laravel.
You can't pass additional data to store action this way.
You can look at this route by using php artisan route:list command. As you can see, it doesn't expect and doesn't pass any data.
So, you need to pass ID in hidden input:
{!! Form::hidden('userId', $user->id) !!}
And get data in controller with $request->userId
Don't forget to remove $id from store() and $users->id from Form::open()
Also, correct syntax (with fixed typos) for Form::open() is:
{!! Form::open(['method' => 'post', 'route' => 'user.store']) !!}

Passing PHP Variable from GET to current form if isset laravel 5

Ok, I've searched the last couple of days and haven't been able to find an answer that made sense. I'm positive it's doable but not sure if I'm in over my head. I'm new to Laravel 5 and also pretty new to PHP. I'm trying to send a inputted string from the GET['quote'] part of my (laravel collective) form to the same form that is updated with a new view below it, that utilizes the GET variable. Basically keeping whatever someone types in the searchbox after refresh.
Normally I would just echo out the needed variable if it's "set" then leave it empty if it isn't in the 'value' = "" section of the textbox. In Laravel with this form I'm not able to do that due to the rendering of the child view in the parent view(parent first, then child), if I understand correctly. Is there any work around? I'm about ready to just do the old echo within the form if I can't find a solution.
Thanks in advance! This site has helped me with a lot of my questions over the last few months.
Current Form on a Parent View.
{{ Form::open(array('url' => 'search', 'method' => 'get')) }}
{!! Form::text('query', '', [
'class' => "form-table",
'placeholder' => "Search Keywords",
'value' => "{{ $query }}"
]) !!}</td>
{!! Form::submit('Submit') !!}
{{ Form::close() }}
Code below form is to pass the textbox string to another view and show it below this form. I don't know any JS so I'm kind of fumbling about here probably trying to make something work that just wont work.
#yield('child')
Code below here is from my controller.
public function getSearch(){
$title = "Page Title";
$query = "some string"; //This was set to $_GET['query'] but was failing.
return view('pages.search')->with("title", $title)->with("query", $query);
Working Code: For anyone that runs into this issue
Form Code: value = "" was in the wrong place on my original form as well so I've placed the $query into the proper form array below.
{{ Form::open(array('url' => 'search', 'method' => 'get')) }}
{{ Form::text('query', $query, [
'class' => "form-table",
'placeholder' => "Search Keywords",
])
}}
{{ Form::submit('Submit') }}
{{ Form::close() }}
Pages Controller.
Added to the top of my controller below namespace.
use Illuminate\Http\Request;
Controller:
public function getSearch(Request $request){
$title = "Search";
$query = $request->input("query", "");
return view('pages.search', ["title" => $title, "query" => $query]);
}
You were close, but missing a couple of key parts. First you need to pass in the Request to the controller to access, well, the request.
I would highly recommend reading up on this part of the docs before you get stuck into Laravel too much. Responses and requests are the heart of all things back end:
https://laravel.com/docs/5.1/requests
https://laravel.com/docs/5.1/responses
Controller
use Illuminate\Http\Request;
public function getSearch(Request $request)
{
$title = "Page Title";
$query = $request->input("query", ""); // get the 'query' string, or default to an empty string
return view('pages.search', [
'title' => $title,
'query' => $query,
]);
}
View
{!! Form::open(['url' => 'search', 'method' => 'get']); !!}
{!! Form::text('query', $query, [
'class' => "form-table",
'placeholder' => "Search Keywords"
]);
!!}
{!! Form::submit('Submit'); !!}
{!! Form::close(); !!}
First check if query has any value. If not it will return an empty string as the second argument of input field is set to ''. Then it will search through the database for the given keyword.
Controller:
public function getSearch(Request $request)
{
$title = "Page Title";
if(Input::has('query')){
$query = Input::get('query');
}
$data = DB::table('table_name')->where('column_name','like','%'.$query.'%');
// or if you use model you can use this
$data = ModalName::all()->where('column_name','like','%'.$query.'%');
return view('pages.search', compact('title','data'));
}
Blade:
{!! Form::open(array('url' => 'search', 'method' => 'get')) !!}
{!! Form::text('query', '', [
'class' => "form-table",
'placeholder' => "Search Keywords",
]) !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
// To view data add the table below search form
<table>
<tr>
<th>col1</th>
<th>col2</th>
<th>col3</th>
</tr>
#foreach($data as $d)
<tr>
<td>{{ $d->column_one }}</td>
<td>{{ $d->column_two }}</td>
<td>{{ $d->column_three }}</td>
</tr>
#endforeach
</table>

laravel route doesn't exist though it exits

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

Categories