2017-08-07 37 views
2

嗨,我真的是新手在Laravel。所以我真的需要一些幫助,我需要從$ report-> reportable_type中獲取最後一個單詞,並從reportable_type獲取App/Models/Store和App/Models/Product。所以我想爆炸斜線(/)並得到最後一個詞它意味着商店或產品。php - 如何使用laravel從斜線(/)爆炸並得到硬漢

我的控制器中的代碼其工作,但它使用print_r,我不想使用print_r。

這是我的控制器

public function toLink($id) 
{ 
    $report = $this->reportRepository->findWithoutFail($id); 

    //get Store Name 
    $name = Store::where('id','=',$report->reportable_id)->pluck('name')->all(); 
    $storename = $name[0]; 

    //get Store ID 
    $idstore = Store::where('id','=',$report->reportable_id)->pluck('id')->all(); 
    $storeid = $idstore[0]; 

    if(empty($report)) 
    { 
     Flash::error('Report not found'); 
     return redirect(route('reports.index')); 
    } 

    $report_type = $report->reportable_type; 
    print_r (explode("/",strrpos($report_type, '/') + 1),$report_type); 
    return redirect(env('FRONTEND_URL') ."/".str_slug($reportable_type)."/$storeid/".str_slug($storename)); 


} 
+3

[請不要發佈您的代碼爲圖像。(// meta.stackoverflow.com/q/285551) –

+0

友情提醒:避免張貼代碼圖像](https://開頭元。 stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285554#285554)。相反,將代碼文本複製並粘貼到問題主體中。 –

回答

4

的Laravel輔助函數class_basename做到了這一點。

$type = class_basename($report->reportable_type); 

更新:
如果你真的想使用爆炸功能,您可以使用Laravel last助手獲取數組的最後一個元素。

$type = last(explode('/', $report->reportable_type)); 
+0

你能告訴我,class_basename是否從每個斜線爆炸並採取最後的措辭?應用程序/模型/商店和應用程序/模型/產品,我想要得到的最後一個詞它意味着商店和產品,但感謝您的幫助 –

+0

@SahatRiyanto點擊鏈接,你會看到助手的描述:'class_basename '返回給定類的類名,並刪除類的名稱空間。另外,文檔中有一個例子。 –

+0

@Jerodev亞那多數工作:DDDD,但你可以給我另一個答案與使用laravel爆炸功能?非常感謝您的幫助:D –