eloquent - Returning the first model from a hasMany relationship in Laravel - Stack Overflow

PHOTO EMBED

Fri Sep 06 2024 00:11:25 GMT+0000 (Coordinated Universal Time)

Saved by @xsirlalo #php

// your relationship
public function books() {
    return $this->hasMany('App\Models\Book');
}

// Get the first inserted child model
public function first_book() {
   return $this->hasOne('App\Models\Book')->oldestOfMany();

}

// Get the last inserted child model
public function last_book() {
   return $this->hasOne('App\Models\Book')->latestOfMany();

}

content_copyCOPY

https://stackoverflow.com/questions/44524888/returning-the-first-model-from-a-hasmany-relationship-in-laravel