Php 如何在每个线程中获取用户名(Laravel)?
我不想得到创建自己线程的用户的名字。就像迈克尔写了一篇关于食物的文章。所以在食物线的底部应该是迈克尔的名字 我已经为此编写了代码,但它实际上不起作用。也许你们中的某个人能发现错误 我有两个模型。线程模型和用户模型 线程模型:Php 如何在每个线程中获取用户名(Laravel)?,php,multithreading,laravel,Php,Multithreading,Laravel,我不想得到创建自己线程的用户的名字。就像迈克尔写了一篇关于食物的文章。所以在食物线的底部应该是迈克尔的名字 我已经为此编写了代码,但它实际上不起作用。也许你们中的某个人能发现错误 我有两个模型。线程模型和用户模型 线程模型: <?php namespace App\Models\Thread; use Illuminate\Database\Eloquent\Model; use App\User; class Thread extends Model { public $ta
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
use App\User;
class Thread extends Model {
public $table = 'thread';
public $fillable = [
'thread',
'content',
'user_id'
];
public function userthread() {
return $this->belongsTo('User','user_id', 'id');
在我的html中:
{{$threaduser->name}}
我收到的错误消息是:
未定义的属性:illumb\Database\elounce\Collection::$name(视图:/var/www/laravel/logs/resources/views/test/show.blade.php)
我希望有人能在那里帮助我 将其更改为
{{$threaduser->userthread->name}}
将Thread类中的userthread()函数更改为
public function userthread() {
return $this->belongsTo('App\User','user_id', 'id');
}
get()
@foreach ($threadusers as $threaduser)
{{ $threaduser->userthread->name }}
@endforeach
如果每个用户只有一个线程,则使用first
而不是get
当然,这取决于您想做什么。我做了,但现在我收到了以下错误消息:类“User”未找到我做了,但现在我收到了与以前相同的错误:未定义属性:illumb\Database\elounce\Collection::$userthread(在我的html中)如果使用get(),则需要迭代您的集合。所以构建foreach循环来实现这一点。我这样做了,但现在它给了我线程下数据库中的每个用户:/你到底做了什么?我使用了你给我的foreach循环。现在我只是让线程中的每个用户都写,但是你想要什么呢。您希望如何回复beSo您告诉我,$threadusers
是用户的集合,还是我遗漏了什么?
public function userthread() {
return $this->belongsTo('App\User','user_id', 'id');
}
@foreach ($threadusers as $threaduser)
{{ $threaduser->userthread->name }}
@endforeach