在Laravel 5.6中使用资源控制器

时间:2018-06-24 07:47:59

标签: php laravel

我在Laravel 5.6中使用资源控制器。我正在关注this教程。我在这里val start = Color.DKGRAY val mid = Color.MAGENTA val end = Color.BLUE //content.background is set as a GradientDrawable in layout xml file val gradient = content.background as GradientDrawable val evaluator = ArgbEvaluator() val animator = TimeAnimator.ofFloat(0.0f, 1.0f) animator.duration = 1500 animator.repeatCount = ValueAnimator.INFINITE animator.repeatMode = ValueAnimator.REVERSE animator.addUpdateListener { val fraction = it.animatedFraction val newStart = evaluator.evaluate(fraction, start, end) as Int val newMid = evaluator.evaluate(fraction, mid, start) as Int val newEnd = evaluator.evaluate(fraction, end, mid) as Int gradient.colors = intArrayOf(newStart, newMid, newEnd) } animator.start()

在我的代码中,我正在使用Resource controller uses the Route model binding, This means that you dont need to fetch the specified task by the id. Laravel will do it for you. $task variable which is passed into the show() method is passed to the view via compact method.中的以下代码。

Controller

在这里,我得到的是整个 /** * Display the specified resource. * * @param \App\sura $sura * @return \Illuminate\Http\Response */ public function show(Sura $sura) { return $sura; } 对象,而不是Sura

为什么我要获取整个对象而不是id?问题出在哪里?

1 个答案:

答案 0 :(得分:2)

https://laravel.com/docs/5.6/routing#route-model-binding 当依赖项注入模型

public function show(Sura $sura)
{
    return $sura; // it is instance of Sura 
}

要获取ID,请使用此

public function show($suraId)
{
    dd($suraId);// return integer number
}