I'm trying to get records belonging to other records. Here's a breakdown of the problem.
I have the following article records and would like to get the users who have articles with the status (0):
$articles = Article::whereStatus(0)->get();
The Article model looks like this:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Article extends Model
{
protected $fillable = ['user_id', 'title', 'content', 'status'];
public function user()
{
return $this->belongsTo('AppUser', 'user_id');
}
}
I understand I can iterate through the articles records then fetch the respective user records but might this be inefficient? i.e.
foreach($articles as $article)
{
$user = User::find($article->user_id);
}
Is there an alternative method to get those users records with the articles?
question from:
https://stackoverflow.com/questions/65848137/get-records-belonging-to-other-records 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…