1. Home
  2. Laravel
  3. Belongsto

Belongs to sets a single-to-single relationship between two Models. Use it for fetching a parent Model associated with your class. Pass a Model class you want to reference as an argument.

#laravel#model
// Set the relationship in a Model file

class SubItem extends Model {
    public function item() {
        return $this->belongsTo(Item::class); // SubItem belongs to an Item
    }
}

// Then, in a Controller, you can fetch the Item your Model belongs to.  
App\SubItem::first()->item;
copy
Full Laravel cheatsheet