I am having some difficulty understanding what this code is doing. could someone explain it to me? I saw some people using it to redirect user to another page with it but I don't understand this part here "['id'=>$data3->id]) ".
Here is the full code: (from view page)
<a href="{!! route('user.upload.image', ['id'=>$data3->id]) !!}">
Controller (how data3 is being passed to view):
public function getInfo($id) {
$data3=UserImage::where('user_id',$id)->get();
return view('view',compact('data3'));
Route:
Route::get('/userUpload/{user}/create1','CreateController#create1')->name('user.upload.iamge');
Route::get('user/show/{id}','HomeController#getInfo')->name("user.show");
create1 controller:
public function create1(personal_info $user){
return view('create1')->withUser($user);
}
Based on your routes
Route::get('/userUpload/{user}/create1','CreateController#create1')->name('user.upload.iamge');
Route::get('user/show/{id}','HomeController#getInfo')->name("user.show");
The first route has a parameter user which must be passed to it anytime the route is called.
The second one also has an id parameter which must also be passed to it.
Passing the parameter values to the routes can be done in many ways. Eg.
By using the route name:
<a href="{!! route('user.upload.image', ['user'=>$data3->id]) !!}">
This method requires you to pass all the parameters as an array with the parameter name as the key of the array.
You can also call the route like:
<a href="/userUpload/{$data3->id}/create1">
Which requires nothing since the parameter has been hardcoded into the url.
Any time you accept parameters in your route, to pass them to your controller or route function, the must be listed on the order in which they are arranged.
So your getInfo passes the id parameter it received from the route to the controller
public function getInfo($id) {
$data3=UserImage::where('user_id',$id)->get();
return view('view',compact('data3'));
}
Related
I am trying to pass the 2 parameters to my index method in the controller but it does not pass and says 0 have been sent. I have checked if the variables can be shown on the view which it can so that isn't the problem.
In my blade file I have the following <a tag:
Is the circumstance: <strong>{{$variables->circumstance}}</strong> true?
web.php route:
Route::resource('attack', AttackController::class)->middleware('auth');
Controller:
public function index($debate, $argument){//}
Error message:
Too few arguments to function App\Http\Controllers\AttackController::index(), 0 passed in C:\Users\hakar\argupedia\Argupedia\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected
Blade File
Is the circumstance: <strong>{{$variables->circumstance}}</strong> true?
Route File
Route::group(['middleware' => 'auth'], function () {
Route::get('/attack/{debate}/{argument}', 'AttackController#schemes')->name('search');
});
Controller
public function schemes($debate, $argument){
dd($debate, $argument);
}
If you're only using one method in a controller, there is no need for a resource route, Better way would be to create a single route like this:
Route::get('attack/{debate}/{argument}', 'AttackController#index')->middleware('auth')->name('attack.index');
The arguments in the curly brackets are called route parameters. Now, in order to pass this parameters, you need to pass them like key => value arrays
Is the circumstance: <strong>{{$variables->circumstance}}</strong> true?
Now, you have access to these parameters in your controller:
public function index($debate, $argument){
dd($debate, $argument);
}
If both parameters are not required here, you can use optional parameters in your routes, by using ? mark, like this:
Route::get('attack/{debate?}/{argument?}', 'AttackController#index')->middleware('auth');
You can now pass only one parameter to your route, without crashing your application.
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
I'm having problems with getting the right language parameter in the url when submitting a form.
My application has two possible language parameters: de and en.
I've made a middleware called setLocale.php with this function:
public function handle($request, Closure $next)
{
$locale = $request->segment(1);
app()->setLocale($locale);
return $next($request);
}
this is the route in web.php:
Route::post('/{locale?}/ticket', 'TicketController#store')->name('validation');
and here is the form action:
<form method="post" action="{{ route('validation') }}">
When I want to submit the form and the input is validated you should get to the route I've shown you above.
But the url will only be: /ticket instead of en/ticket or de/ticket.
By the way, you get to the form site via links, so the language parameters are static like this:
Route::get('/en/index/{param1?}/{param2?}', 'TicketController#index')->name('index');
Route::get('/de/index/{param1?}/{param2?}', 'TicketController#index')->name('index');
How do I get the language parameter from the form page ('/en/index/[..]') in the url of the validation page, therefore in the form action?
Shouldn't the handle function in the setLocale.php middleware get me the language parameter when trying to submit the form?
If yes, how can I achieve passing it to the next page (via the form action)?
EDIT
It works fine when I edit the form action like this:
<form method="post" action="{{ route('validation', app()->getLocale()) }}">
Would this be a good solution?
As I understand, your problem is that the {locale} parameter has no value if you don't explicitly pass one to the route() function. You can use route binding (see "Customizing The Resolution Logic") to guarantee that it will always have a value, falling back on the current locale:
Route::bind('locale', function ($value) {
if (!$value) {
$value = app()->getLocale();
}
return $value;
});
This instructs Laravel to call this function whenever it sees a route parameter named locale and use its return value. And if there's no value for the parameter, it returns the current locale.
But be aware that this will affect all URL parameters named locale. If you have any route whose locale parameter should be allowed to be empty, you will have to rename in in one of the 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.
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.