I would like to use auth in my login system with two roles:
admin
user.
When I redirect the route to user, it fails.
My Controller:
public function profile($id)
{
$santri = Santri::find($id);
return view('santri.profile', ['santri'=>$santri]);
}
My Route:
Route::group(['middleware' => ['auth', 'checkRole:admin,user']], function () {
Route::get('/santri/{id}/profile', 'SantriController#profile')->name('profiluser');
});
How I check the role:
{
$santri = Santri::all();
if(in_array($request->user()->role,$roles))
{
return $next($request);
}
return redirect()->route('profiluser', $santri);
}
Error:
Missing required parameters for [Route: profiluser] [URI: santri/{id}/profile].
The error message:
Missing required parameters for [Route: profiluser] [URI: santri/{id}/profile].
tells you that you are missing the parameter for this route: profiluser
As you can see here you do not call the route with the correct parameter, you are trying to pass the whole object instead of the id, so instead of this:
return redirect()->route('profiluser', $santri);
Do this:
return redirect()->route('profiluser', $santri->id);
But since you are already passing the whole object you could also do this, lets call it method B.
Here you want to find the model using the passed id:
public function profile($id)
{
$santri = Santri::find($id);
return view('santri.profile', ['santri'=>$santri]);
}
But since you already pass the whole object you could do this:
public function profile(Santri $santri)
{
return view('santri.profile', ['santri' => $santri]);
}
or this, which looks cleaner in my mind:
public function profile(Santri $santri)
{
return view('santri.profile', compact('santri'));
}
You need to pass $santry->id instead of just $santry. Change the line to:
return redirect()->route('profiluser', [$santri->id]);
According to your route:
Route::get('/santri/{id}/profile', 'SantriController#profile')->name('profiluser');
You must pass the user id, like this:
return redirect()->route('profiluser', ['id' => $request->user()->id]);
Related
Update:
This line of code in the frontend was the culprit:
<inertia-link v-if="options.edit" :href="'/admin/gallery/edit/1'">
I had to change it to:
<inertia-link v-if="options.edit" :href="'/admin/gallery/1/edit'">
to make it comply with the laravel resource format for edit, provided by #Babak.
Original Post:
How would I transform this route in web.php:
Route::get('/admin/gallery/edit/{id}', function ($id) {
$data = Gallery::find($id);
return inertia('backend/cms-gallery-edit', ['data' => $data]);
});
to a resource route with its resource controller function:
Route::resource('/admin/gallery', GalleryController::class);
GalleryController.php:
public function edit($id)
{
$data = Gallery::find($id);
// assign id to end of route
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
Edit:
I've tried both approaches of #Babak's answer, which work for index and create routes but the edit route still throws a 404. It is the only route encompassing an id.
web.php:
Route::resource('/admin/gallery', GalleryController::class)->only('index', 'create', 'edit');
GalleryController.php:
public function edit($gallery)
{
$data = Gallery::find($gallery);
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
Inertia passes the id from the frontend via href:
<inertia-link v-if="options.edit" :href="'/admin/gallery/edit/1'">
Browser shows:
GET http://127.0.0.1:8000/admin/gallery/edit/1 404 (Not Found)
There is a fixed structure for laravel resource route method, you can see full list here. For edit page, it will generate something like '/admin/gallery/{gallery}/edit'
You can write it like below:
In your web.php file:
Route::resource('/admin/gallery', GalleryController::class)->only('edit');
And in your controller, name of the resource must be the same as your function's parameter.
public function edit($gallery)
{
$data = Gallery::find($gallery);
// assign id to end of route
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
Or, you can customize it using parameter method. Refer to here
Route::resource('/admin/gallery', GalleryController::class)->only('edit')->parameters([
'gallery' => 'id'
]);
And your controller
public function edit($id)
{
$data = Gallery::find($id);
// assign id to end of route
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
I have this route declared on laravel:
Route::get('pages/{page}/{slug}', 'Common\Pages\CustomPageController#show')
->middleware(['web', 'prerenderIfCrawler']);
This route works fine and works if you make requests to:
https://example.com/pages/1/test-page
https://example.com/pages/2/other-page
https://example.com/pages/3/url-test
The problem is that I need a more friendly url as well as.
https://example.com/test-page
https://example.com/other-page
https://example.com/url-test
I want remove the suffix called pages, The numbers for the pages will never change and will be static for each one.
I've tried to make static routes for each one but can't get it to work.
Route::get('other-page', array('as' => 'other-page', function() {
return App::make('Common\Pages\CustomPageController')->show(2);
}))->middleware(['web', 'prerenderIfCrawler']);
I would appreciate a little help.
You could always get the URL segment in the Controller and use that to know what page you are on. If you don't want to do that you could pass extra information in the 'action' to specify the page:
Route::middleware(['web', 'prerenderIfCrawler'])->group(function () {
Route::get('test-page', [
'uses' => 'Common\Pages\CustomPageController#show',
'page' => 'test-page',
]);
...
});
Then you can get this extra information in the Controller:
public function show(Request $request)
{
$page = $request->route()->getAction('page');
...
}
If you knew all the pages you can use a route parameter with a regex constraint to restrict it to only those page names:
Route::get('{page:slug}', ...)->where('page', 'test-page|other-page|...');
public function show(Page $page)
{
...
}
You could just make use of a wildcard to catch your routes like this:
Route::get('/{slug}', 'Common\Pages\CustomPageController#show')
->middleware(['web', 'prerenderIfCrawler']);
Then in your controller:
public function show($slug)
{
$page = Page::where('slug', $slug)->first();
// ...
}
Just be careful with where you place the route. It should be at the end of your routes otherwise it will catch all the request of your app.
// ...
// my other routes
// ...
Route::get('/{slug}', ...);
By the way, if you want to bind your page models using the slug attribute do this:
Route::get('/{page:slug}', 'Common\Pages\CustomPageController#show')->//...
^^^^^^^^^
Then in your controller:
public function show(Page $page)
{ ^^^^^^^^^^
// ...
}
Check this section of the docs.
My goal is to pass object from current function to other function. As you can see the codes below, I have provided a multiple return approach I've done in the controller section. I have commented each of its error. I am not sure why that is happened although some of the approaches are from the voted answer in some other questions.
Array Content [ dump($data = $request->all()); ]
Controller
public function supply_status(Request $request, $data)
{
dump($data);
}
public function supply_redirect(Request $request)
{
$data = $request->all();
//option 1
return redirect()->route('supply_status', compact('data')); // "Array to string conversion" error
//option 2
return redirect()->route('supply_status', ['data' => $data]); // "Array to string conversion" error
//option 3
return redirect()->route('supply_status')->with('data', $data); // Missing required parameters for [Route: supply_status] [URI: supply_status/{data}].
//option 4
return redirect()->action('TestController#supply_status')->with('data', $data); // Missing required parameters for [Route: supply_status] [URI: supply_status/{data}].
}
Route
Route::get('supply_status/{data}', 'TestController#supply_status')->name('supply_status');
For this situation you really should use the Session class
Session::put('customer_data', $request->all());
Session::save();
redirect()->route('foobar');
in foobar
if($data = Session::get('customer_data')) {
dump($data);
}
You could alternatively also do: redirect()->with(['customer_data' => $request->all()]) and then fetch it the same way with Session::get('customer_data') or session()->get('customer_data')
You can use the PHP function serialize and put the result in a GET or POST parameter:
http://php.net/manual/fr/function.serialize.php
Then you have to unserialize your data :
http://php.net/manual/fr/function.unserialize.php
But this will not be safe as the user can modify that data. You should persist its on session or something else, depending of your needs to do this operation.
return redirect()->route('supply_status', ['data' => serialize($data)]);
dump(unserialize($data));
I have a route of GET type with some parameters. For example
Route::get('/my-route/{id}',array('uses'=>'myController#myAction'));
I want to check value of the parameter id and if this id=1 then redirect to another route else continue with it. What I am doing is like this
Route::get('/my-route/{id}',function($id){
if($id==1){
return Redirect::to(URL::route('my-another-route'));
}
else{
//What should I do here so my route works as before.
}
});
In else part I want my routes to myController#myAction along with parameters.
Thanks
You can do it as:
Route::get('/my-route/{id}',function($id){
if($id==1){
return Redirect::to(URL::route('my-another-route'));
}
else{
return app()->call(myController::class, ['id' => $id], 'myAction');
}
});
The easiest way to get this to work and route to your controller is put your Route back to the way it was and then the if statement to the top of your controller condition:
public function myAction() {
if ($id == 1) {
return Redirect::to(URL::route('my-another-route'));
}
to the top of your controller method.
Also, if you're only using uses => 'Controller#method' on with your route you can just do:
Route::get('/my-route/{id}','myController#myAction');
Hope this helps!
I need to pass an additional parameter($uid) from my index.blade.php to my edit.blade.php by clicking on a button.
My index.blade.php:
Edit
My FlyersController:
public function edit($id, $uid)
{
return view('backend.flyers.edit')->withUid($uid);
}
With the code above I get an error: "Missing argument 2 for App\Http\Controllers\FlyersController::edit()"
What am I doing wrong here?
The error is not throwing from the action method. It is coming from route for that URL.
Check the URL for passing argument to the the controller.
If this is the your desire URL localhost:8000/backend/flyers/10/edit?%24uid=1 then the second argument is in $request variable not in controller function argument.
You should pass an array into action() helper:
action('FlyersController#edit', ['id' => Auth::user()->id, 'uid' => 1])
Ok,
the only way I can solve this is by using the following in My FlyersController:
public function edit(Request $request, $id)
{
return view('backend.flyers.edit')->withRequest($request);
}
and access then the uid with {{request->uid}} in my view.
If anybody has a better solution for this, let me know.
Use this code
return view('backend.flyers.edit', ['var1' => $var1, 'var2' => $var2]);
That will pass two or more variables to your view