【发布时间】:2016-06-22 04:45:52
【问题描述】:
我不想让创建的用户的名字是自己的线程。就像迈克尔做了一个关于食物的话题。所以在食物线的底部应该是迈克尔的名字。
我已经为此编写了代码,但它并没有真正起作用。也许你们中的某个人可以找到错误。
我有两个模型。一个线程模型和一个用户模型。
线程模型:
<?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');
用户模型:
<?php
namespace App;
use ...
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function threaduser() {
return $this->hasMany('App\Models\Thread\Thread','user_id', 'id');
}
}
现在是控制器方法,我正在尝试获取名称:
public function show($id)
{
$thread = Thread::query()->findOrFail($id);
$threaduser = Thread::where('user_id', Auth::user()->id)->with('userthread')->get();
return view('test.show', [
'thread' => $thread,
'threaduser' => $threaduser
]);
}
在我的 html 中:
{{$threaduser->name}}
我得到的错误信息是:
未定义属性:Illuminate\Database\Eloquent\Collection::$name(查看:/var/www/laravel/logs/resources/views/test/show.blade.php)
我希望有人可以帮助我。
【问题讨论】:
标签: php multithreading laravel