简体   繁体   English

如何访问刀片中的另一列

[英]how can I access to another column in blade

I have the id in a blade and I want to access to another column.我在刀片中有 ID,我想访问另一列。 I have $article->country_id which gives access to the id of the foreign key country .我有 $article->country_id 可以访问外键country的 id 。 How can I find the record using id and then get access to country->name.如何使用 id 找到记录,然后访问 country->name。 I have done the things below but it doesn't work as I have explained.我已经完成了下面的事情,但它并没有像我解释的那样工作。

Here is the blade这是刀片

@foreach($articles as $article)
<div class="col-md-12">
کشور:<h4 class ="post-title">{{route('article.whichCountry',$article->country_id)}}</h4>
</div>
@endforeach

here is web.php这是web.php

Route::get('article/whichCountry/{country}',[
    'uses' => 'ArticleController@whichCountry',
    'as' => 'article.whichCountry'
]);

and here is whichCountry in ArticleController这是ArticleController 中的 whichCountry

public function whichCountry($id){
        $country = country::where('id',$id)->first();
        return $country->name;
    }

The point is that it doesn't call the function whichCountry and prints: http://127.0.0.1:8000/article/whichCountry/10关键是它没有调用函数whichCountry并打印: http : //127.0.0.1 : 8000/article/whichCountry /10

Thank you very much for your response in advance!非常感谢您提前回复!

Your route access needs to be like below, giving value for {country} parameter.您的路由访问需要如下所示,为{country}参数提供值。

{{ route('article.whichCountry',['country' => $article->country_id]) }}

Second, instead of where in your controller method, if Id is the primary key, you can just do:其次,如果Id 是主键,而不是控制器方法中的where可以执行以下操作:

Country::find($country);

Also, change your function definition like below for the parameter value filling另外,更改您的函数定义,如下所示以填充参数值

public function whichCountry($country){

This is very simple to implement this.实现这一点非常简单。 First check your Controller is getting the id properly.首先检查您的Controller是否正确获取了 id。 Then use find method然后使用find方法

Your route should be like this你的路线应该是这样的

{{route('article.whichCountry',['id'=>$article->country_id])}}

And in your controller you have to use like this在你的控制器中你必须像这样使用

public function whichCountry($id){
    $country = country::where('id',$id)->first();
    return $country->name;
}

In your web.php looks like this在你的web.php看起来像这样

Route::get('article/whichCountry/{id}',[
    'uses' => 'ArticleController@whichCountry',
    'as' => 'article.whichCountry'
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM