在我的 Laravel 應用程式中,我創建了 url:如“www.example.com/rent/property-for-rent/45”,但在這里,我想從最后一個中洗掉 id 并將 id 與我的第二個引數連接起來,即“出租物業”,如:“www.example.com/rent/property-for-rent-45”。
在 Controller 函式中獲取 3 個引數
public function single_properties(Request $request, $property_purpose, $slug, $id)
在 Blade 檔案中
{{url(strtolower($property->property_purpose).'/'.$property->property_slug)}}
在 Web.php 檔案中
Route::get('{property_purpose}/{slug}/{id}', 'PropertiesController@single_properties');
我試圖從函式、刀片和路由中減少引數,但對我不起作用。誰能幫幫我,我被困在里面很糟糕,
uj5u.com熱心網友回復:
你可以簡單地這樣做:
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,
]);
}
在你的刀片呼叫中 {{ $new_url }}
uj5u.com熱心網友回復:
你可以用一種簡單的方式來做。因為網址是這樣的:-
www.example.com/rent/property-for-rent-45
路由的 URL 結構類似于:/rent/slug-id 表示 id 與 slug 連接。所以 laravel 只會把它當作完整的字串作為 slug。我們將定義它,稍后在控制器中將從 slug 中提取 id。
所以在 routes/ web.php 你可以定義如下:-
在 Web.php 檔案中 Route::get('{property_purpose}/{slug}','PropertiesController@single_properties');
在 Blade 檔案中
{{url(strtolower($property->property_purpose).'/'.$property->property_slug-$property->property_id)}}
在控制器檔案中,您定義如下:-
在這里,您必須使用已知的 PHP 函式決議 Slug 變數。與資訊一樣,您知道 id 最后出現在 - 符號之后。所以你可以爆炸 slug 變數并在最后一個之后獲取 id -
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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/365270.html
上一篇:Laravel基于布爾引數的eloquent呼叫關系
下一篇:將Row中的專案垂直居中
