skip to Main Content

I’m a beginner so I’m still struggling with this.

I need your help to convert this query to Laravel’s query
but apperently this one doen’t work even in PhpMyAdmin.

SELECT `candidats.id`
FROM `candidats`,
     `candidatures`
WHERE `candidats.id` = `candidatures.candidat_id`
ORDER By `candidatures.date_depot`;

What I want to do is to display all the candidates ordred by etat but this etat that belongs to candidatures table

In Candidat Model :

class Candidat extends Model
{
use HasFactory;
protected $table = 'candidats';
protected $fillable = [
    'id_service',
    'demande',
    'nom',
    'prenom',
    'email',
    'adresse',
    'date_naissance',
    ];

public function candidatures()
{
    return $this->hasMany(Candidature::class);
}}

In Candidature Model :

 class Candidature extends Model
  {
use HasFactory;
protected $table = 'candidatures';
protected $fillable = [
    'candidat_id',
    'date_depot',
    ];

public function candidat()
{
    return $this->belongsTo(Candidat::class, 'candidat_id');
}
}

Thank you in advance.

2

Answers


  1. Chosen as BEST ANSWER

    well I've tried

         $candidat = Candidat::join('candidatures','candidatures.candidat_id', '=', 'candidats.id')
                ->where('date_depot','=', $motcle)
                ->get();
    

    and it works


  2. Try to get like this

    $Candidat = Candidat::with(['candidatures' => function ($query) {
        $query->orderBy('date_depot', 'ASC');
    }]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search