how to hide id from URL laravel 7? - laravel

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

Related

how to display data based on specific id in laravel?

I am new to laravel. I want to send data as id of existing data. Id comes from products.blade I send via href tag as shown below to gallery page. I have tried to find a way through other sites but it still doesn't work
<a class="btn btn-success" href="/dashboard/galleries/{{ $product->id }}"><i class="ri-image-add-line text-white"></i></a>
then i create a route like this
Route::resource('/dashboard/galleries', DashboardGalleryController::class)->middleware('admin')->shallow();
in the controller gallery, I made like this
public function index($id)
{
$gallery = Gallery::where('products_id', $id)->get();
return view('dashboard.galleries.index', compact('gallery'));
}
then inside the gallery page, I want to display a table to add images based on that id.
dashboard/galleries/index.blade.php
<h1>{{ $gallery->id }}</h1>
should i add data inside foreach or outside?
A restful resource controller sets up some default routes for you and even names them.
and ur case the logic should be inside show() function not index()
check this issue it will help u same issue solved here

Attachment in Laravel many-to-many relation

I'm trying to do my first attachment/ detachment. I'm using two tables, user, event, and its pivot, event_user. For this one, I needed to do a button to subscribe the user to an event, so my teacher told me to use an and route it to the method subscribe. At this moment, the error that comes up is.
Route [subscribe] not defined.
Blade
<a href="{{ route('subscribe', $event->event_id) }}"
class="btn btn-primary btn-lg">Subscribe</a>
Route
Route::get('/subscribe', [SubscribeController::class, 'index']);
SubscribeController
public function index($id)
{
$user = Auth::user();
$user->events->attach($id);
return view('index');
}
I tried putting URL instead of the route in the , and it goes to /subscribe, but comes to an error that says -> Too few arguments to function 0 passed and exactly 1 expected.
I did a dd() in the component to see if the event id was the correct one, and it was. Also, apart from these errors, how can I route a method without changing the route? Can I do it using the indexController (because it's in the index where these events are located)?
First, in order to reference the route by name, you need to give it the name subscribe.
Docs: https://laravel.com/docs/master/routing#named-routes
Second, you want to add that id route parameter that you are trying to use in your controller.
Docs: https://laravel.com/docs/master/routing#required-parameters
So your route should end up looking like this:
Route::get('/subscribe/{id}', [SubscribeController::class, 'index'])
->name('subscribe');
And then you'll want to call it like this:
<a href="{{ route('subscribe', ['id' => $event->event_id]) }}"
Firstly in your blade view file you are trying to generate url using route helper like this
<a href="{{ route('subscribe', $event->event_id) }}"
class="btn btn-primary btn-lg">Subscribe</a>`
The way you call the route helper we can supposed this
You have already define a route in your web.php file which is named subscribe
and that route have paramater
But in your web.php file you have this route
Route::get('/subscribe', [SubscribeController::class, 'index']);
This route doesn't have any name.
And it doesn't expect any parameter
To fix that you should only change the way you have define you route in the web.php file like this
Route::get('/subscribe/{id}', [SubscribeController::class, 'index'])
->name('subscribe');
With this route we define:
id as a required parameter
And the route is named subscribe

Laravel : How to hide url parameter?

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.

Most efficient way to get a specific model from a collection in BLADE in Laravel

Hi im developing a site using Laravel and have some site data id like to keep track of such as facebook url, instagram url, address, phone number, etc. This information for this model is stored as a name and value in the DB, so example name:facebook and value:[facebook url here]. I know that i can retrieve all the models and get a collection, pass it to my view and loop through them, but id like a little more control to get specific models to use it where i need them on specific parts of the page. This is what I currently have it:
I have a model SiteMeta which i pass an instance to my view:
$site_meta = new \App\SiteMeta();
return view($page_string)->with('site_meta',$site_meta);
And in my SiteMeta Model:
public static function get($name)
{
return SiteMeta::where('name', '=', $name)->firstOrFail();
}
And then in my view im getting my specific model by its name:
<a target="_blank" href="{{$site_meta::get('Facebook')->value}}">
<i class="fa fa-facebook-official data-icon" aria-hidden="true"></i>
</a>
So my question is, is this the most efficient way to do it? Im doing this in various places on the contact page to display information like phone number and address and other site information which i feel like is a bit overload as it make a request to the database each time im doing sitemeta::get('name');
Is there a better way to get a specific one from the collection in BLADE? Thanks
I would pass it as an array from your controller method:
$site_meta = \App\SiteMeta::lists('value', 'name');
return view($page_string, compact('site_meta');
On your blade template you could do this:
$site_meta['Facebook'];

Laravel Sub-menu Within View

Hi I am very new to Laravel and MVC frameworks in general and am looking to create a list of links (in a view within a template) that links to some content. I am using this to display a list of nine people and to display their profile description when the link is clicked on. I have created a model of what the page looks like at http://i.imgur.com/8XhI2Ba.png. The portion that I am concerned with is in blue. Is there a way to route these links to something like /about/link2 or /about?link2 while maintaining the same exact page structure but modifying the ‘link content’ section (on the right of the link menu) to show the specific link's content? I would greatly appreciate it if someone could point me in the right direction, as I have literally no clue where to go with this!
There are a couple ways you can go about doing this.
Templates
Create your route.
Im assuming a lot about your app here but hopefully you get the picture. If you need help with anything in particular, be sure to update your question with the code youve tried so it will be easier to help you.
Route::get('about/{page}', function($page)
{
$profile = Profile::where('name', $page)->first();
return View::make('about')->with('profile', $profile);
});
Modify Template.blade.php
Put this line where you wish for About.blade.php to appear.
#yield('content')
Create your view which will extend your template
#extends('Template')
#section('content')
<h2>User Profile</h2>
<ul>
<li>Name: {{ $profile->name }}</li>
<li>Last Updated: {{ $profile->updated_at }}</li>
</ul>
#stop
AJAX
This solution will utilize AJAX to grab the data from the server and output it on the page.
Route for initial page view
Route::get('about', function($page)
{
$profiles = Profile::all();
return View::make('about')->with('profiles', $profiles);
});
Feel free to follow the same templating structure as before but this time we need to add some javascript into the template to handle the AJAX. Will also need to id everything which needs to be dynamically set so we can easily set it with jquery.
#extends('Template')
#section('content')
<h2>Links</h2>
#foreach($profiles as $profile)
{{ $profile->name }}
#endforeach
<h2>User Profile</h2>
<ul>
<li>Name: <span id="profile_name">{{ $profile->name }}</span></li>
<li>Last Updated: <span id="profile_updated_at">{{ $profile->updated_at }}</span></li>
</ul>
<script>
function setProfile(a)
{
$.ajax({
method: 'get',
url: 'getProfile',
dataType: 'json',
data: {
profile: $(a).data('id')
},
success: function(profile) {
$('#profile_name').html(profile.name);
$('#profile_updated_at').html(profile.updated_at);
},
error: function() {
alert('Error loading data.');
}
});
}
</script>
#stop
Route to handle the AJAX request
Route::get('getProfile', function()
{
$profile_id = Input::get('profile');
$profile = Profile::find($profile_id);
return $profile->toJson();
});
Now, the page should not have to reload and only the profile information should be updated.
Making some assumptions here as no code posted and assuming you're using the latest version of Laravel, Laravel 5.
Lets say you have a table in your database named users and you have a Model named Users (Laravel 5 comes with the Users model as default, see app/Users.php). The users will be the base of our data for the links.
Firstly, you want to register a route so you can access the page to view some information. You can do this in the routes file. The routes file can be found here: app/Http/routes.php.
To register a route add the following code:
Route::get('users', ['uses' => 'UserController#index']);
Now what this route does is whenever we hit the URL http://your-app-name/public/users (URL might be different depending on how you have your app set up, i.e. you may not have to include public) in our web browser it will respond by running the index method on the UserController.
To respond to that route you can set up your UserController as so:
<?php namespace App\Http\Controllers;
class UserController extends Controller {
public function index()
{
}
}
Controllers should be stored in app/Http/Controllers/.
Now lets flesh out the index method:
public function index()
{
// grab our users
$users = App\Users::all();
// return a view with the users data
return view('users.index')->with('users');
}
This grabs the users from the database and loads up a view passing the users data.
Here's what your view could look like:
<!DOCTYPE html>
<html>
<head>
<title>Users Page</title>
</head>
<body>
#foreach($users as $user)
<a href="{{ URL::route('user', ['id' => $user->id]) }}">
{{ $user->username }}
</a>
#endforeach
</body>
</html>
The view code will loop through each user from the $users data we passed to the view and create a link to their user page which is different for each user based on their id (their unique identifier in the DB)
Due to the way I've named it, this would be found in app/views/users/index.blade.php - if you save files ending in blade.php you can use Laravel's templating language, blade.
Now you need to finally set up another route to respond to a user page, for example http://your-app-name/public/user/22.
Route::get('user/{id}', ['uses' => 'UserController#show']);
Then add the show method to UserController
public function show($id)
{
// this will dump out the user information
return \App\User::find($id);
}
Hope that helps a little! Wrote most of it off the top of my head so let me know if you get any errors via comment.
This question is very bare, and it is difficult to actually help your situation without you showing any code. Just to point you in the right direction though, here is what you would need.
A Model called People, this is how you will access your data.
A controller. In this controller you will do the following
Get the ID of the profile you want from the functions parameters
Find that persons information e.g. People::find($person_id);
return the profile view with that persons data e.g. return view('profile')->with('person', $person);
In your view you can then use that data on that page e.g. {{ $person->name }}
For the page that needs to display the links to the people you would have a method in your controller which..
Get all the people data e.g. People::all();
Return a view with that data return view('all-people')->with('people', $people);
You will then need a route to access an individual person. The route will need to pass the persons ID into a controller method e.g.
Route::get('get-person/{id}',
[ 'as' => 'get-person',
'uses' => 'PeopleController#getPerson' ]);
You can then use this route in your view to get the links to each person
#foreach($people as $person)
{{$person->name}}
#endforeach
This would produce the list of links you want.

Resources