I'm trying to show the username that's part of each comment block. Currently I've got it to show Article + Comment and the body of the comment, but in my table I've assigned user_id to each comment. I can get it to show the user_id, but I'm unsure on how to look for the username assigned to that user_id.
view:
@foreach($article->comments as $comment)
{{$comment->user->username}}
{{$comment->body}}
@endforeach
Model Articles:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Articles extends Model
{
public function comments(){
return $this->hasMany('App\comments')->orderBy('id', 'DESC');
}
}
Model Comments:
namespace App;
use Illuminate\Database\Eloquent\Model;
class comments extends Model
{
public function user(){
return $this->hasOne('App\users');
}
}
Articles Controller:
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]);
}
}
Error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.comments_id' in 'where clause' (SQL: select * from
userswhereusers.comments_id= 16 and `u ▶"