2017-08-07 35 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 –