Call controller with href on Laravel - php

I'm trying to call controller with href, but I'm getting error, I need pass a parameter.
Im doing like this
<i class="material-icons" title="Delete"></i>
Controller Code
public function destroy(Story $story)
{
$story = Story::find($id);
$story->delete();
return redirect('/stories')->with('success', 'Historic Removed');
}
Error Missing required parameters for Route: stories.destroy -> error

The link_to_action() helper generates an actual HTML link, which is an <a> tag. You're therefore already using it wrong.
However the error you're getting is likely not related to this.
The best way to link to routes is using the route() helper:
link
And the route definition:
Route::get('/someroute/{:param}', ['uses' => 'IndexController#index', 'as' => 'index.index']);
Note the as key, it assigns a name to this route. You can also call
Route::get(...)->name('index.index')
which yields the same result.

I might be wrong but in html you're passing an integer, in controller though, function is expecting an object of Story. Just change Story story to $id and it should be good.
Anyway, can't say much more without actual error.

Since you are accepting $story as model object so you don't have to use Story::find() and also you haven't define $id in your destroy method therefor Change your code to:
public function destroy(Story $story)
{
$story->delete();
return redirect('/stories')->with('success', 'Historic Removed');
}
Hope it helps.
Thanks

You should use it in this way: since according laravel explanation for function link_to_action first param will be controller function path, 2nd will be name and 3rd will be array of required params:
<i class="material-icons" title="Delete"></i>
You can also get help from here

Related

Passing data from blade to controller Laravel

I want to pass a object from the blade file to the controller file. The purpose is when the user click an edit button the user will get a form which is filled with the previous input data. I am using this code in the blade file:
Edit
But When I want to get the passed object from the controller's edit method I get a null. My Controller code is like this now:
public function edit(FeesType $feesType)
{
//
dump($feesType->name);
return view('feestype.edit',['feesType'=>$feesType]);
}
Here I have dump the $feesType object but I get a null. Please help me how can I solve this problem.
Thanks in advance
Route model binding works a bit different here is the documentation
What you need to do is have your route like this:
Route::get('feestype/{feesType}/edit', 'YourController#edit')->name('feestype.edit');
then in your view
Edit
-- EDIT
using a resource file:
Route::resource('feestype', 'YourController')
the link will be built the same as above:
{{ route('feestype.edit', $feesType) }}
you should change your Route to :
Route::put('feestype/{id}/edit', 'YourController#edit');
For update and edit you should use put not get.
Note that for this code:
Edit
first you should compact $feestype in YourController then use your code in blade.
Now the code in the blade file is
Edit
The controller file contains this code:
public function edit(FeesType $feesType)
{
//
$feesType = FeesType::find($feesType->id);
dump($feesType->name);
return view('feestype.edit',['feesType'=>$feesType]);
}
And here is my Route definition:
Route::resource('feestype','FeesTypesController');
And the browser shows this message:

Laravel. conflict with routes

I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.

Create with arguments [Laravel 5.2]

I want to use a argument in a create action, but when I try to access the action:
Missing argument 1 for App\Http\Controllers\AdsDiagController::create()
Here is the create action:
public function create($id){
$record = TestRecord::findOrFail($id);
return view("adsdiag.create", ["record" => $record]);
}
Here is the link to the action:
Create
And the route:
Route::resource('adsdiag', 'AdsDiagController');
I'm newbie in laravel, and I'm really confused with routes. I appreciate any help.
To solve your problem you should use in your route.php
Route::get('adsdiag/{id}/',AdsDiagController#create);
Reason
When you call Route::resource('adsdiag', 'AdsDiagController') it generates these routes
Route::get('adsdiag','AdsDiagController#index');
Route::post('adsdiag','AdsDiagController#store');
Route::get('adsdiag/create','AdsDiagController#create'); // you can see that create method doesn't have any arguments here.
Route::get('adsdiag/show/{id}','AdsDiagController#show');
Route::post('adsdiag/update','AdsDiagController#update');
Route::get('adsdiag/edit/{id}','AdsDiagController#edit');
Route::delete('adsdiag/destroy/{id}','AdsDiagController#destroy');
Since Route::get('adsdiag/{id}/',AdsDiagController#create); is not generated by Resourcce so you need to include in your route explicitly.
You need to pass the arguments to the action() helper method in an array, even if it's just one argument:
action('AdsDiagController#create', [$record->id])

How to use the request route parameter in Laravel 5 form request?

I am new to Laravel 5 and I am trying to use the new Form Request to validate all forms in my application.
Now I am stuck at a point where I need to DELETE a resource and I created a DeleteResourceRequest for just to use the authorize method.
The problem is that I need to find what id is being requested in the route parameter but I cannot see how to get that in to the authorize method.
I can use the id in the controller method like so:
public function destroy($id, DeletePivotRequest $request)
{
Resource::findOrFail($id);
}
But how to get this to work in the authorize method of the Form Request?
That's very simple, just use the route() method. Assuming your route parameter is called id:
public function authorize(){
$id = $this->route('id');
}
You can accessing a Route parameter Value via Illuminate\Http\Request instance
public function destroy($id, DeletePivotRequest $request)
{
if ($request->route('id'))
{
//
}
Resource::findOrFail($id);
}
Depending on how you defined the parameter in your routes.
For my case below, it would be: 'user' not 'id'
$id = $this->route('user');
Laravel 5.2, from within a controller:
use Route;
...
Route::current()->getParameter('id');
I've found this useful if you want to use the same controller method for more than one route with more than one URL parameter, and perhaps all parameters aren't always present or may appear in a different order...
i.e. getParameter('id')will give you the correct answer, regardless of {id}'s position in the URL.
See Laravel Docs: Accessing the Current Route
After testing the other solutions, seems not to work for laravel 8, but this below works
Route::getCurrentRoute()->id
assuming your route is
Route::post('something/{id}', ...)
I came here looking for an answer and kind of found it in the comments, so wanted to clarify for others using a resource route trying to use this in a form request
as mentioned by lukas in his comment:
Given a resource controller Route::resource('post', ...) the parameter you can use will be named post
This was usefull to me but not quite complete. It appears that the parameter will be the singular version of the last part of the resource stub.
In my case, the route was defined as $router->resource('inventory/manufacturers', 'API\Inventory\ManufacturersController');
And the parameter available was manufacturer (the singular version of the last part of the stub inventory/manufacturers)
you will get parameter id if you call
request()->route('id')
OR
$this->route('id')
if you're using resource routing, you need to call with the resource name
// eg: resource
Route::resource('users', App\Http\Controllers\UserController::class);
$this->route('user')
in Terminal write
php artisan route:list
to see what is your param name
Then use
$this->route('sphere') to get param

"Some mandatory parameters are missing" Laravel 4

I've having trouble passing off an object to my 'edit' view in Laravel 4. The URL is generated correctly "localhost/edit/1" however this is the returned error:
Some manadatory parameters are missing ("offer") to generate a URL for route "get edit/{offer}
My related routes.php snippet:
Route::get('edit/{offer}','OfferController#edit');
The OfferController#edit action:
public function edit(Offer $offer)
{
return View::make('edit',compact('offer'));
}
Just some extra detail, here's the snippet from the 'index' view that initiates the action:
Edit
I should also mention when I remove the Blade form in '/views/edit.blade.php' the view is created, including the header which specifies the $offer->id:
<h1>Edit Offer {{ $offer->id }}</h1>
What am I missing here?
You need to pass an array to action():
Edit
Your Edit function needs to be changed. You are passing id in link, but expects Instance of Offer in edit function. Assuming Offer is an Eloquent model,
public function edit($id)
{
$offer = Offer::find($id);
return View::make('edit',compact('offer'));
}
Hope this helps.

Categories