There is a system in which teachers upload articles that have to be previously accepted by an admin.
I need to show only teachers with "Articles" that are approved and public, that list of teachers needs to be ordered by the name that is in "User"
This is the query I made, but it ignores orderBy():
$designers = Professor::whereHas('user',function($query){
return $query->whereHas('papers',function($query2){
return $query2->where('private', '=', 0)->where("approved","=",1);
});
})>orderBy('name','asc')->paginate(20);
MODELS
user
class User extends Authenticatable
{
use CrudTrait;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'last_name', 'mobile', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
protected $table = 'users';
// protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
// protected $fillable = [];
// protected $hidden = [];
// protected $dates = [];
public function professor()
{
return $this->hasOne('App\Models\professor');
}
public function papers()
{
return $this->hasMany('App\Models\paper');
}
}
Professor
class Professor extends Authenticatable
{
use CrudTrait;
protected $guarded = ['id'];
protected $table = 'professors';
// protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
// protected $fillable = [];
// protected $hidden = [];
// protected $dates = [];
public function user()
{
return $this->belongsTo('App\Models\User');
}
}
Paper
class Paper extends Model
{
use CrudTrait;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'papers';
// protected $primaryKey = 'id';
// public $timestamps = false;
protected $guarded = ['id'];
// protected $fillable = [];
// protected $hidden = [];
// protected $dates = [];
public function user()
{
return $this->belongsTo('App\Models\User');
}
}
I consider that since you have the query it is incorrect, because at least I understand it like this:
User
that have at least oneprofessor
linkedUser
with at least onepaper
where
to filter the papersname
of the entityUser
Consultation proposal