Voyager 2
2.x
2.x
  • Introduction
  • Javascript
  • Media Manager
  • Plugins
  • Settings
  • bread
    • Actions
    • BREAD Builder
    • Layouts
    • Lists
    • Manipulate data
    • Multilanguage
    • Relationships
    • Validation
    • Views
  • contributing
    • Assets
    • CSS
    • Introduction
  • de
    • index
  • formfields
    • Checkboxes
    • Date & Time
    • Dynamic input
    • Formfields
    • Media Picker
    • Number
    • Password
    • Radios
    • Relationship
    • Repeater
    • Select
    • Simple array
    • Slider
    • Slug
    • Tags
    • Text
    • Toggle
  • getting-started
    • Installation
    • Prerequisites
    • Tips and tricks
    • What is Voyager
  • overriding
    • Overriding formfields
    • Icons
  • plugins
    • Assets
    • Components
    • Features
    • Filter
    • Plugin development
    • Language
    • Menu Items
    • Custom pages
    • Preferences
    • Routes
    • Settings
    • Widgets
Powered by GitBook
On this page

Was this helpful?

  1. bread

Relationships

PreviousMultilanguageNextValidation

Last updated 3 years ago

Was this helpful?

This page describes how to implement relationships in your model. For informations on how to use the relationship formfields visit !

Voyager currently supports the following relationship types:

  • Has one

  • Has many

  • Belongs to

  • Belongs to many

Morphing and Has many through relationships can be displayed when browsing but are not yet add/editable.

Defining the relationship

When defining the relationship in your model you have to provide the return type of the method, otherwise Voyager can not know about them:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class User extends Model
{
    public function belongsTo(): BelongsTo
    {
        return $this->belongsTo(...);
    }

    public function belongsToMany(): BelongsToMany
    {
        return $this->belongsToMany(...);
    }

    public function hasOne(): HasOne
    {
        return $this->hasOne(...);
    }

    public function hasMany(): HasMany
    {
        return $this->hasMany(...);
    }

}
this page