我试图显示每个评论区块的用户名。 目前,我已经将它显示为文章+评论和评论的正文,但在我的表格中,我已经为每条评论分配了user_id。我可以让它显示user_id,但我不确定如何查找分配给该user_id的用户名。
视图:
select jsonb_array_elements(question->'suggestions')
from slides;
模特文章:
@foreach($article->comments as $comment)
{{$comment->user->username}}
{{$comment->body}}
@endforeach
模特评论:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Articles extends Model
{
public function comments(){
return $this->hasMany('App\comments')->orderBy('id', 'DESC');
}
}
文章控制者:
namespace App;
use Illuminate\Database\Eloquent\Model;
class comments extends Model
{
public function user(){
return $this->hasOne('App\users');
}
}
错误:
SQLSTATE [42S22]:找不到列:1054未知列 ' users.comments_id'在' where子句' (SQL:select * from
namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; use App\Articles; use App\Comments; use App\Users; class ArticlesController extends Controller { public function getAllArticles(){ $results = Articles::orderBy('id', 'DESC')->get(); return view('index', ['articles' => $results]); } }
其中users
。users
= 16和`u▶"
答案 0 :(得分:2)
您在0.4
班级中使用了无效关系。
使用comments
方法代替belongsTo
,如下所示
hasOne