Controller Method not getting called - laravel

I am using Laravel 5.0
I have a method in MyController
public function myMethod($id) {
dd($id);
}
The routes.php file
Route::post('path1/{obj-id}/path2', 'MyController#myMethod');
Route::resource('path1', 'MyController');
In the view file, I am calling the method through a form on submit
{!! Form::open(['action' => ['MyController#myMethod', $myObject->id]]) !!}
Now the problem is, every time I click on Submit I get a 404 error. The URL in the address bar changes to path1/1/path2 as expected, but I get 404.
Where am I going wrong?

I got the solution myself.
Turns out one cannot have a dash (-) inside {} in one's routes.
My route in routes.php was initially
Route::post('path1/{obj-id}/path2', 'MyController#myMethod');
I changed it to
Route::post('path1/{id}/path2', 'MyController#myMethod');
and now everything works fine.
Sorry for missing the - in the original question. Thank you all who tried to help.

You can give your route a name and use this name in form
Route::post('path1/{id}/path2', [
'as' => 'myroute', 'uses' => 'MyController#myMethod'
]);
Now use it at your form like this
Form::open(array('route' => array('myroute', $myObject->id)))

Instead of using {!! Form::open(['action' => ['MyController#myMethod', $myObject->id]]) !!}, you need to use any of these:
{!! Form::open(['url' => 'path1/'.$myObject->id.'/path2']) !!}
Or, if you use Name routes i.e. mymethod.update, you can do it easily with that:
{!! Form::open(['routes' => ['mymethod.update', $myObject->id]]) !!}
BTW, if you really want to use 'action', you need to change your url like this in your routes.php file:
Route::post('path1/path2/{id}', 'MyController#myMethod');
Route::resource('path1', 'MyController');
Hope this help!

The Problem is with your route .
Route::post('path1/{id}', 'MyController#myMethod');
And in View, You missed the Post method . Change your view to below
{!! Form::open(['action' => ['MyController#myMethod', $myObject->id, 'method' => 'post']]) !!}
Hope this helps !

Related

How can i solve the auto souce code in ckeditor from Laravel 5.8?

When i use ckeditor in laravel from my localhost and every post i made, that comes with source code and how can i stop this.
I am new!
{{Form::label('body', 'body')}}
{{Form::textarea('body', '', ['id' => 'article-ckeditor','class' => 'form-control', 'placeholder' => 'Body Text'])}}
When i save my body like : This is Good and nice but actually This is Good and nice this text coming with source code.
Hy you can use the {!! $name->name !!} insted of {{ $name->name }}

Laravel - HasMany CRUD - Update

I need some help, please,
I have two tables: carriers and sell
Carrier CRUD is working. Sell CRUD is also working, except Update method.
In order to update, i need carrierId and sell id, right?
This is my CarrierSellController:
And this is my carriers/sell/edit.blade.php
The model binding is working:
Form action has /edit at the end of the url. It's oke?
If i hit submit button, i get this error:
Try this one
{!! Form::model($sell,['url'=>url('carriers/'.$sell->carrier->id.'/sell/'.$sell->id),'method'=>'patch']) !!}
and in the Sell model add
public function carrier()
{
return $this->belongsTo(Carrier::class);
}
make sure the method is PUT
add this to your form
{{ method_field('put') }}
Why don't you if you want to be absolutely sure just try declaring the route yourself like:
Route::put('carriers/{carrierId}/sell/{id}', [ 'as' => 'carriers.sell.update', 'uses' => 'CarrierSellController#update' ]);
And then in your view have this:
{!! Form::model($sell, ['method' => 'PUT', 'route' => ['carriers.sell.update', 'carrierId' => $sell->carrier->id, 'id' => $sell->id]) !!}

Passing a param containing a path to a controller

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

How to pass validation errors to view without redirect in Laravel

How can i pass validation errors directly to a View without a redirect in Laravel?
I don't want to do that:
return Redirect::back()->withErrors($validator)->withInput();
but rather:
Input::flash(); // for repopulating fields
Validator::flash(); // this doesn't exist
return View::make('fragments/login_ajax');
Or another more concise direct generation of a view without a redirect. Optimal would be something like that, but that doesn't work:
return View::make('fragments/login_ajax')->withErrors($validator)->withInput();
The reason is, that this is the answer to an ajax based login. Redirecting to another method only used to display the same view deems unneccessary.
Cheers
Though agreeing it is a bad idea to do this, you can use View::share() to share the errors to all the views.
\View::share('errors', $errors);
Use below solution.
Controller:
$validator = Validator::make($request->all(),
[
'class_id' => 'required|numeric|not_in:0',
'batch_id' => 'required|numeric|not_in:0',
'student_id' => 'required|numeric|not_in:0'
]);
$this->viewData['errors'] = $validator->errors();
View File:
{!! Form::label('class_id','Student Class') !!}
{!! Form::select('class_id', array(), 0, ['class' => 'form-control', 'id' =>
'class_id']) !!}{!! $errors->first('class_id', '<p class="help-block">:message</p>')!!}
Real answer:
this is not an issue that is worth spending time on. It is an architecture problem.
Possible Solution to your situation (not recommended):
Make auth a new route and have it return json of errors/messages. Then have JS deal with the errors / messages / logic.

form model binding and file upload

I got an answer previously to my question about how to create edit/update details form in laravel, and the answer was utilizing Form::model(modelName). Nevertheless, the page which is updating is the same page handling the insertion. I have the following form opening:
{{ Form::open(array('action' => 'SomeController#basic','files' => true)) }}
how can I change this to use the form::model instead? I also have an upload of file in same page (profile pic).
Laravel 4 or 5 Form <-> Model binding
Maybe is a "bit" late for the original poster, but for those who search for a solution, this can be used as a later reference.
The L4 solution is:
{{ Form::open($object, array('method' => 'PATCH', 'action' => array('SomeController#update', $object->id),'files' => true)) }}
For Laravel 5, you would use:
{!! Form::model($object, ['method' => 'PATCH', 'action' => ['ObjectsController#update', $object->id], 'files' => true]) !!}
Home this helps!
Form model binding can be done by declaring form open as
{{ Form::model($user, array('action' => 'SomeController#post', 'files'=> true)) }}
You pass the model to the view for example here $user is the model.
$user = User::find(1);
return View::make('editUser')->with(compact('user'));
For file upload, if the $user is isset you could display the image. Along with the upload button. When update is clicked you can check
if(Input::has('file'))
then move the file and upload the path in the database. May be there is a different solution for this.
Is this what you were looking for?

Resources