vendredi 7 décembre 2018

Laravel Nova - Two "One-To-Many Relationships"

Community,

I want to have two selects from one table in Nova. It should be possible to select one Location in Laravel Nova as start_location and select one Location as end_location.

I've got an Activities Model/Table with a start_location and an end_location.

Furthermore I've got a Location Model/Table.

Activities - Table
---------------------------------------------------
   id   |   start_location_id   |  end_location_id
---------------------------------------------------       
   1    |   1                   |  1
   2    |   1                   |  2


Location - Table
---------------------------------------------------
   id   |   city
---------------------------------------------------
   1    |   Berlin
   2    |   Paris

Problem

How can I setup a "multiple" one-to-many relationship here?

My challenge is that I'm not sure how to map a location to activities.start_location_id AND activities.end_location_id.

Especially, how to select the two Locations in Laravels Nova.

Possible solution?

Activities.php (Model)

public function start_location() {
    return $this->hasOne(Location::class);
}

public function end_location() {
    return $this->hasOne(Location::class);
}

Location.php (Model)

public function start_location() {
    return $this->hasMany(Activities::class, start_location_id);
}

public function end_location() {
    return $this->hasMany(Activities::class, end_location_id);
}

Location.php - Nova Resource

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),
        Text::make('City'),
        BelongsToMany::make('Activities', 'location_start'), // ?
        BelongsToMany::make('Activities', 'location_end')    // ?
    ];
}

Activities.php - Nova Resource

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),
        Text::make('Title'),
        Trix::make('Body'),
        HasOne::make('Location', 'location_start'), // ?
        HasOne::make('Location', 'location_end'),   // ?
    ];
}

Maybe there is a more easier approach?

Thank you in advance!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2E8wB2v
via IFTTT

Aucun commentaire:

Enregistrer un commentaire