Home > Mobile >  I want to remove id from URL in my laravel
I want to remove id from URL in my laravel

Time:11-18

In my Laravel application, I have created url: like "www.example.com/rent/property-for-rent/45" but here, I want to remove id from the last and concatenate id with my 2nd parameter that is, "property for rent" like : "www.example.com/rent/property-for-rent-45".

In the Controller function get 3 parameters

public function single_properties(Request $request, $property_purpose, $slug, $id)

In the Blade file

{{url(strtolower($property->property_purpose).'/'.$property->property_slug)}}

In the Web.php File

Route::get('{property_purpose}/{slug}/{id}', 'PropertiesController@single_properties');

I have tried to reduce parameter from the function, blade and route, but didn't work for me. can anyone please help me out, I am poorly trapped in it,

CodePudding user response:

you can simply do it like this:

public function singleProperties(Request $request, $property_purpose, $slug, $id)
{
    // old url: example.com/rent/property-for-rent/45
    // new url: example.com/rent/property-for-rent-45
    $newUrl = $property_purpose . '/' . $slug . '-' . $id;

    return view('your.view.blade', [
        'new_url' => $newUrl,
    ]);
}

And in your blade call {{ $new_url }}

CodePudding user response:

You can do in a simple way. As the URL is like this:-

www.example.com/rent/property-for-rent-45

Url structure for route will be like: /rent/slug-id Means id is concatenated with slug. so laravel will consider it as full string as slug only. We will define this and later in controller will extract id from the slug.

So in routes/ web.php you can define like:-

In the Web.php File Route::get('{property_purpose}/{slug}','PropertiesController@single_properties');

In the Blade file

{{url(strtolower($property->property_purpose).'/'.$property->property_slug-$property->property_id)}}

In Controller File You an define like:-

Here Slug variable you have to parse using known PHP function. As with the information, you know id is coming in last after - symbol. So you can explode slug variable and take the id coming last after -

public function singleProperties(Request $request, $property_purpose, $slug)
{

$slugarr=explode('-',$slug);
$id=$slugarr[count($slugarr) 1];// As index starts from 0, for getting last you have to this

//Now you got the $id you can fetch record you want this id

}
  • Related