我有一个刀片中的id,我想访问另一列。我有$article->country_id,用于访问外键country的id。我如何使用id找到记录,然后访问国家/地区->名称我已经做了下面的事情,但没有像我解释的那样奏效。
这是刀片
@foreach($articles as $article)
<div class="col-md-12">
کشور:<h4 class ="post-title">{{route('article.whichCountry',$article->country_id)}}</h4>
</div>
@endforeach
这里是web.php
Route::get('article/whichCountry/{country}',[
'uses' => 'ArticleController@whichCountry',
'as' => 'article.whichCountry'
]);
这里是Article Controller中的哪个国家
public function whichCountry($id){
$country = country::where('id',$id)->first();
return $country->name;
}
关键是它不调用函数whichCountry并打印:http://127.0.0.1:8000/article/whichCountry/10
非常感谢您的提前回复!
您的路由访问需要如下所示,为{country}
参数给定值。
{{ route('article.whichCountry',['country' => $article->country_id]) }}
其次,如果Id是主键,则可以执行以下操作,而不是控制器方法中的where
Country::find($country);
此外,为填充的参数值更改如下函数定义
public function whichCountry($country){
实现这一点非常简单。首先检查您的Controller
是否正确获取了id。然后使用find
方法
你的路线应该是这样的
{{route('article.whichCountry',['id'=>$article->country_id])}}
在你的控制器中,你必须像这样使用
public function whichCountry($id){
$country = country::where('id',$id)->first();
return $country->name;
}
在您的web.php中,看起来像这个
Route::get('article/whichCountry/{id}',[
'uses' => 'ArticleController@whichCountry',
'as' => 'article.whichCountry'
]);