Laravel : How to hide url parameter? - laravel

Here the scenario is I want to pass a variable which will be send from one page to another and in next page it's gonna store through a form. So I have passed the variable from first page to second page through the URL. But I want to hide the parameter in the URL. How do I do it?
Here is my route :
Route::get('/registration/{course_id}',[
'uses'=>'AppController#getregistration',
'as'=>'registration'
]);
And Controller :
public function getregistration($course_id)
{
return view('index')->with('course_id',$course_id);
}
And first page this is how I send the value to first page:
<li> A </li>

Post Method
Route
Route::post('/registration',['uses'=>'AppController#getregistration','as'=>'registration']);
View
{!!Form::open(array('url' => '/registration')) !!}
{!! Form::hidden('course_id', '1') !!}
{!! Form::submit('registration') !!}
{!! Form::close() !!}
Controller
public function getregistration(Request $request)
{
$course_id = $request->input('course_id');
return view('index')->with('course_id',$course_id);
}
Get method
use encryption method, it will show encrypted id in url
View
<li> A </li>
Controller
public function getregistration($course_id)
{
$course_id = Crypt::decrypt($course_id);
return view('index')->with('course_id',$course_id);
}

here is no way you hide parameter in url, rather then you convert parameter value encrypt or hash is up to you,
other-way is save value in session first, then call the value from session without define parameter in url.
because laravel route only working to pattern of url /string /id, post get. dynamic value you must be writing / getting using pattern method.
Thanks.

You cannot hide a parameter in URL. If you don't want to show the ID then try using SLUG. I hope you understand what is a SLUG. If you don't then here it is. If you course title is My new Course Title then its slug would be my-new-course-title. And make sure it is unique like an ID in the table. It is also good for SEO, readable and looks good.

Related

How do I get data from the Route and show it in a html page?

I am trying get a value from the URL for example : websitename.com/dash/namehere
I have route :
Route::get('/dash/{id}', 'DashController#index' , function($id) {
return 'dash'.$id;
});
Then I have a section on the page I am trying to display as a title. I am currently using this:
#if (\Request::is('dash/1'))
<div class="pagetitle">[clientname]</div>
#endif
This works but I am thinking there is a better way to approach this. I am very new to this.
I want to get the id from the url / site.com/dash/ {id} and use it in the HTML page as a Title header.
Use {{ request()->route('id') }} in your blade to get the "id" parameter from the dash route.

Is there any way to show post from permalink in laravel?

Normally we show single post using below method.
This is the route <a href="{{ action('TestController#index',$post ->slug) }}">
and Route::get('/test/{slug}','TestController#index');
This is the controller method
public function index(Blog $slug)
{
return $slug;
}
But I don't need like that. I have generated permalink and save it into database in slug field. Now i want to show post from this permalink. See my table
How can i do t?
just put the slug column in href.
title of post
I hope be useful

how to hide id from URL laravel 7?

i have this url :
http://127.0.0.1:8000/deliverer/4
i want it like this
http://127.0.0.1:8000/deliverer/
this is my function :
public function show($id)
{
// $userhash->hashids->encode($id);
$user=User::find($id);
return view('deliverer.profile')->with('user',$user);
}
and this is my route
Route::get('deliverer/{id}', 'deliverer\DelivererController#show')->name('profile');
and this in view
<a href="http://127.0.0.1:8000/deliverer/{{ Auth::user()->id }}" >
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
Assuming, that what you're trying to accomplish is to view the current authenticated user's profile, then you should follow the steps below:
First you have to modify the route and remove the {id} from the URL, like this:
Route::get('deliverer', 'deliverer\DelivererController#show')->name('profile');
Then, inside the controller you have to remove the $id param from the show() method and change the method to get the id from the authenticated user.
public function show($id)
{
// $userhash->hashids->encode($id);
$user = \Auth::user();
return view('deliverer.profile')->with('user',$user);
}
And of course, you have to remove the Auth::user()->id() from the view route, and perhaps use the named route instead of hardcoding it, like so:
<a href="{{ route('profile') }}">
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
I'm assuming you're just trying to hide id's so users can guess the next number or try to pull up records they shouldn't.
Have you looked into UUID's? Example here: (https://dev.to/wilburpowery/easily-use-uuids-in-laravel-45be) That might be a solution.
Also, if you are worried that someone might tamper with URL to pull records, you should look into securing up your models. Do a check to see if the user should have access to that particular record. Many ways to accomplish that.
You can use token or configure a middleware, or as you did you can hash or crypt the id and make the verification after the call
The url will be like that :
http://127.0.0.1:8000/deliverer/?id=aze45a8sd54q

Route Exception with a post Method

I have a MethodNotAllowedHttpException with a button for a post method , it's really strange because i made the same process with a lot of others things in the project but with this route don't want to work . Where i made a mistake ?
thanks a lot in advance friends :)
Here my route :
Route::post('licencies_to_update/{id}', 'LicencieController#Renouveller')->name('licencie.renouveller');
Here my button in my blade view :
{!! link_to_route('licencie.renouveller', 'Effectuer le Renouvellement' , [$licencie->id], ['class' => 'btn btn-primary']) !!}
Here the begin of my controller :
public function Renouveller(Request $request, $id)
{
$licencie = Licencies::findOrFail($id);
dd($licencie);
....
Use:
Route::get('licencies_to_update/{id}', 'LicencieController#Renouveller')
->name('licencie.renouveller');
instead of POST method. Because in the link of your button you are not requesting the url with a POST method but GET method. Besides, you are not doing anything related to POST variable. You are passing a simple variable id in your route param. So, no need to use POST param here.
The link you are creating is an anchor to the route you provided, but the link is a GET request, while you specify in your routes file that you want POST requests on that url.
Either create a form or change the method that route accepts (or let it also accept GET requests)
Edit:
Change your route to
Route::get('licencies_to_update/{id}', 'LicencieController#Renouveller')->name('licencie.renouveller');
to have the expected result the fastest!

Laravel - get id from the right database record

I have laravel project. When I click my view button, I want to see full description of my record. But I don't know how to pass the right id. My database table is called - csstable.
I have model:
<?php
class CssTable extends Eloquent
{
protected $table = 'csstable';
}
View button on each post (I get all of my posts from database, so each of them have id):
<div class="view">
<a href="{{ action('CssController#show') }}" ></a>
</div>
CssController with this show function:
public function show()
{
$csstable = CssTable::all();
return View::make('cssposts/right_post', compact('csstable'));
}
My Route:
Route::get('/css/id_of_right_post', 'CssController#show' );
Right_post, where I want information from description column from row, with id, that i clicked (In this field, I see just last record's description:
<h1 style="color:#fff">{{ $css->description }}</h1>
I have tried to put something like this
public function show($id)
{
$csstable = CssTable::find($id);
return View::make('cssposts/right_post', compact('csstable'));
}
But then there is an error - missing 1 argument in show function. So I want to know, how to pass correct id!
The way to do this involves three steps. First let's go for the route:
Route::get('css/{id}', 'CssController#show');
The {id} there means it's a matching parameter - it'll match a full URI segment (basically anything between slashes) and use that to pass into he method passed. So on to the controller:
class CssController
{
public function show($id)
{
$csstable = CssTable::findOrFail($id);
return View::make('cssposts/view', compact('csstable));
}
}
That controller method accepts a (required) single parameter. You can call it whatever you want, but here I'm going for id as it's an ID for a model. Finally, the last part of the puzzle is how to link to such a route:
// view button for each csstable
<div class="view">
{{ link_to_action('CssController#show', $item->title, array($item->getKey())) }}
</div>
As you can see, I'm using the link_to_action helper, but your method with <a href="{{{ action('CssController#show', array($item->getKey())) }}}"> will work too. After the controller action name, you pass an array that contains all of the parameters in the URI to fill in (in order). In our case we have one parameter, to it's an array with one item. I think in these cases you could also use a string and Laravel will turn it into an array with one element for you. I prefer to be explicit.
Hopefully that's helped you work out how to use the parameter-based routing system in Laravel.

Resources