Laravel Route Redirect with URL Parameter - php

I am trying to generate a unique id and then redirect to a different route with id in the url parameter. But I am getting error as :
"Route [sequences] not defined."
Here is my route defined:
Route::get('/sequences_create','SequencesController#create');
Route::get('/sequences/{id}', 'SequencesController#show');
Here is the create function on the sequences controller:
public function create()
{
$uniq = 'seq'. uniqid();
$seq = new Sequences;
$seq->id = $uniq;
$seq->user_id = auth()->user()->id;
$seq->name = 'New Sequence'; //temp name
$seq->save();
return redirect()->route('sequences', ['id' => $uniq]);
}

you have not set a name for your route by name() method.
try it:
Route::get('/sequences_create','SequencesController#create')->name('sequances.create');
Route::get('/sequences/{id}', 'SequencesController#show')->name('sequances.show');
then:
public function create()
{
//...
return redirect()->route('sequences.show', ['id' => $uniq]);
}

you need to name your route.
Route::get('/sequences_create','SequencesController#create')->name('sequences.create');
Route::get('/sequences/{id}', 'SequencesController#show')->name('sequences.show');
Then change your redirect to:
return redirect()->route('sequences.show', ['id' => $uniq]);

Related

How to show id in Resource Routes url?

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

How to use variables in routes in laravel?

I'm trying to build a application in laravel 5.3 in which I get the variable from request method and then trying to pass that variable in a redirect to the routes. I want to use this variable in my view so that I can be able to display the value of variable. I'm currently doing this:
In my controller I'm getting the request like this:
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member' => $member);
}
Now I've following in my routes:
Route::get('/member/memberinfo', 'MemberController#memberinfo')->with('member', $member);
Now in MemberController I want to use $member variable and display this into my view:
public function memberinfo()
{
return view('member.memberinfo', ['member' => $member]);
}
But I'm getting an error in the routes files
Call to undefined method Illuminate\Routing\Route::with()
Help me out, how can I achieve this.
When you're using redirect()->with(), you're saving data to the session. So to get data from the session in controller or even view you can use session() helper:
$member = session('member'); // In controller.
{{ session('member')['xyz'] }} // In view.
Alternatively, you could pass variables as string parameters.
Redirect:
return redirect('member/memberinfo/xyz/abc')
Route:
Route::get('/member/memberinfo/{xyz}/{abc}', 'MemberController#memberinfo');
Controller:
public function memberinfo($xyz, $abc)
{
return view('member.memberinfo', compact('xyz', 'abc'));
}
You can use like this:
route:
Route::get('/member/memberinfo', 'MemberController#memberinfo')
and the redirect:
return redirect('member/memberinfo')->with('member', $member);
You need to replace => with ,
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member', $member); // => needs to be replaced with ,
}
Hope this works!
Replace line
return redirect('member/memberinfo')->with('member' => $member);
to
return redirect('member/memberinfo')->with('member', $member);
......

How can I get segment for current page in Laravel 5?

I have URL like this /project/1
How can I get param 1
I need it variable in another controller for another route....
here is examle:
route 1:
Route::get('project/{id}',array(
'as' => 'projectID',
'uses' => 'FirstController#someMethod'
));
route 2:
Route::post('another/route',array(
'as' => 'another',
'uses' => 'SecondController#anotherMethod'
));
I need to get inside anotherMethod id param from project/{id}... I tried like this return Request::segment(2); but it will return just segments from this route: another/route...
Any solution?
You can try this:
Controller:
public function index(Request $request){
return $request->segment(2); //set the segment number it depends on you
}
public function someMethod(Request $request)
{
// If you know the segment number
$id = $request->segment(2);
// If you know the parameter name
$id = $request->route('id');
// If you only know it's the last segment
$segments = $request->segments();
$id = array_pop($segments);
}

how can I pass an extra parameter to a controller in laravel

I have this:
Route::get('/product/{id}', 'PagesController#display');
And in my PagesController I have this:
public function display($id) {
return View::make("details", ["id" => $id, "premium" => $premium]);
}
How can I pass the variable $premium to the controller method without inserting it in the url? In simple words I don't want this: www.mysite.com/product/false/125 (false is the value of $premium) but this: www.mysite.com/product/125. That's why I have just $id as parameter in the controller method and no also $premium. I want to pass that variable $premium in other way.
I've try some approaches like this one:
Route::get('/product/{id}', 'PagesController#display')->where("premium" => "false");
which didn't work.
You can use a closure route and call the action "manually":
Route::get('/product/{id}', function($id){
return app('PagesController')->callAction('display', [$id, false]);
});
And
public function display($id, $premium) {
return View::make("details", ["id" => $id, "premium" => $premium]);
}

URL Parameter Passing with Laravel

I want to visit a page like...
http://mysitelocaltion/user_name/user_id
This is just a virtual link, I have used a .htaccess -rewrite rule to internally pass "user_name" and "use_id" as get parameters for my actual page.
How do I achieve the same in Laravel?
Update:
This shall help (documentation)
Route::get('user/(:any)/task/(:num)', function ($username, $task_number) {
// $username will be replaced by the value of (:any)
// $task_number will be replaced by the integer in place of (:num)
$data = array(
'username' => $username,
'task' => $task_number
);
return View::make('tasks.for_user', $data);
});
Route::get('(:any)/(:any)', function($user_name, $user_id) {
echo $user_name;
});
Great to see you using Laravel!
You can add the following in your route
Route::get('user_name/{user_id}', 'YourControllerName#method_name');
In your controller you can access the value as follows
public function method_name(Request $request, $user_id){
echo $user_id;
$user = User::find($user_id);
return view('view_name')->with('user', $user);
}

Categories