-1

After many research I couldn't find the answer to my question. I have a model "Station" with two fields : "name" and "city_id".

In my Station view (in the backpack admin panel) I would like to display city's name (in the model City).

Station and City model are correctly link with hasMany() and belongTo() methods.

How can I set my column in my crud controller to display the name instead of the city id ?

Thank for your help !

2 Answers2

0

Station Model

<?php

[...] // Namespaces

Class Station {

    [...]

    public function city() {
        $this->belongsTo('App\City');
    }
}

City Model

<?php

[...] // Namespaces

class City {

    [...]

    public function station() {
        $this->hasMany('App\Station');
    }
}

Crud Controller

<?php

[...] // Namespaces

class CrudController {

    public function sampleFunction() {
        $stations = App\Station::with('city')->all();

        return view('stations', compact('stations'));
    }
}

View

Now you can loop the stations on your view like

<ul>
@foreach($stations as $station)
    <li>{{ $station->city->name }}</li>
@endforeach
</ul>
Saravanan Sampathkumar
  • 3,201
  • 1
  • 20
  • 46
0

You had wrong sight on backpack fields. Take a look at here.

You can build a custom view and use this for your other model.

Can't add field from different table in laravel-backpack CRUD

Community
  • 1
  • 1
Zugor
  • 870
  • 8
  • 17