I have animals table which has relation with rats,cats and dogs.
Animal Table
ID | Date of Birth | Gender |
---|---|---|
1 | 23-10-2022 | Male |
2 | 23-11-2022 | Female |
3 | 13-10-2022 | Male |
4 | 14-11-2022 | Female |
5 | 01-10-2022 | Male |
6 | 02-11-2022 | Female |
Rats Table
ID | Date of Weaned | Animal ID | Col1 | Col2 |
---|---|---|---|---|
1 | 12-11-2022 | 1 | 3 | 5 |
2 | 12-12-2022 | 2 | 4 | 6 |
Cats Table
ID | Date of Weaned | Animal ID | C1 | C2 |
---|---|---|---|---|
1 | 12-01-2023 | 3 | st1 | rt1 |
2 | 12-01-2023 | 4 | st2 | rt2 |
Dogs Table
ID | Date of Weaned | Animal ID | P1 | P2 |
---|---|---|---|---|
1 | 01-01-2023 | 5 | p1 | p3 |
2 | 02-01-2023 | 6 | p2 | p4 |
How to fetch all the Animals sorted by weaned date?
My Modal looks like
class Animal extends Model
{
use HasFactory;
protected $fillable = [
'date_of_birth',
'gender'
];
public function rats()
{
//return $this->belongsTo('App\Models\Weaning');
return $this->hasOne(Rats::class,'animal_id','id');
}
public function cats()
{
//return $this->belongsTo('App\Models\Weaning');
return $this->hasOne(Cats::class,'animal_id','id');
}
public function dogs()
{
//return $this->belongsTo('App\Models\Weaning');
return $this->hasOne(Dogs::class,'animal_id','id');
}
}
Is my Relation correct?
You need to sign in to view this answers
Leave feedback about this