Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:如何在循环中显示其他表的值?_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel:如何在循环中显示其他表的值?

Php Laravel:如何在循环中显示其他表的值?,php,laravel,laravel-5,Php,Laravel,Laravel 5,因此,我有一段代码,它正在打印用户启动的各种主题/线程 它在Posts表上循环,该表包含与Posts相关的数据 在User列中,它将显示User\u id。但是我想访问User表并显示与User\u id匹配的User\u name列。如何做到这一点 <table border="2"> <tr> <th>Topic</th> <th>User</th> <th&

因此,我有一段代码,它正在打印用户启动的各种主题/线程

它在Posts表上循环,该表包含与Posts相关的数据

User列中,它将显示
User\u id
。但是我想访问User表并显示与
User\u id
匹配的
User\u name
列。如何做到这一点

<table border="2">
    <tr>
        <th>Topic</th>
        <th>User</th>
        <th>Replies</th>
        <th>Views</th>
        <th>Last Update</th>
    </tr>
    @foreach($topics as $topic)
        <tr>
            <td><a href="topics/{{$topic->id}}">{{$topic->topic_title}}</a></td>
            <td>{{$topic->id}}</td>
            <td>0</td>
            <td>0</td>
            <td>{{$topic->updated_at}}</td>
        </tr>
    @endforeach
</table>

您可以尝试以下方法:

@foreach($topics as $topic)
    @php $user = DB::table('User')->where('id', $topic->id)->first(); @endphp;
    <tr>
        <td><a href="topics/{{$topic->id}}">{{$topic->topic_title}}</a></td>          
        <td>{{ $user }}</td>
        <td>0</td>
        <td>0</td>
        <td>{{$topic->updated_at}}</td>
    </tr>
@endforeach
@foreach($topics作为$topic)
@php$user=DB::table('user')->where('id',$topic->id)->first()@endphp;
{{$user}}
0
0
{{$topic->updated_at}
@endforeach
在控制器用户模型中:

$topics = Topic::where('board_id', $id)->with('user')->get();
鉴于:

<td>{{ $topic->user->user_name }}</td>

在您的帖子中模型

public function user(){
     $this->belongsTo(User::class);
}
在你的刀片{{$topic->user->name}}

<table border="2">
<tr>
    <th>Topic</th>
    <th>User</th>
    <th>Replies</th>
    <th>Views</th>
    <th>Last Update</th>
</tr>
@foreach($topics as $topic)
    <tr>
        <td><a href="topics/{{$topic->id}}">{{$topic->topic_title}}</a></td>
        <td>{{$topic->user->name}}</td>
        <td>0</td>
        <td>0</td>
        <td>{{$topic->updated_at}}</td>
    </tr>
@endforeach

话题
使用者
答复
意见
最后更新
@foreach($topics作为$topic)
{{$topic->user->name}
0
0
{{$topic->updated_at}
@endforeach

您首先需要在模型中定义关系,如下所示:

class User extends Model
{
    public function post()
    {
        return $this->hasMany("App\Post");
    }
}

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo("App\User");
    }
}
@foreach($topics as $topic)
    <tr>
        <td>{{$topic->user->name}}</td>

    </tr>
@endforeach
然后在视图侧的循环中,您可以访问用户的数据,如下所示:

class User extends Model
{
    public function post()
    {
        return $this->hasMany("App\Post");
    }
}

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo("App\User");
    }
}
@foreach($topics as $topic)
    <tr>
        <td>{{$topic->user->name}}</td>

    </tr>
@endforeach
@foreach($topics作为$topic)
{{$topic->user->name}
@endforeach

在这种情况下,您不需要使用“with”函数。您只需获取主题,将其传递给查看,遍历并获取用户的数据

检查此项,如果控制器和型号代码之间的关系正确与否,请发布控制器和型号代码?@PrateikDarji已添加。@DeepakRawat您的主题表是否有任何对用户表的引用?@PrateikDarji它有一个与用户表的“用户id”列匹配的列名“用户id”。$user将打印不要只打印
user\u name
,这不是一种获取数据的好方法,数据应该加载到控制器本身。因为阿里使用了
->get()
而不是
->first()
,需要将
{{$user}
更改为
{$user->name}
,顺便说一句,在blade中使用查询不是一种好的做法,为此创建一个关系