Moum
Moum

Reputation: 93

How to remove item from relationship

I have a model with a hasOne relationship like this .

public function geoTournee()
    {
        return $this->hasOne(GeoTournee::class, 'id_tournee','id_tournee');
    }

I have loaded the relationship and want to remove an item from the collection.

Here is the result after , i want get only itineraire content and delete geo_tounee and other fields.

"geo_tournee": {
            "id_geo_tournee": 46,
            "itineraire": [
                {
                    "lat": 8000,
                    "lon": 3000,
                    "date": "2020:04:21 16:58:23"
                },
                {
                    "lat": 8000,
                    "lon": 3000,
                    "date": "2020:04:21 16:58:23"
                },
                {
                    "lat": 8000,
                    "lon": 3000,
                    "date": "2020:04:21 16:58:23"
                },
                {
                    "lat": 5000,
                    "lon": 9000,
                    "date": "2020:04:21 16:58:23"
                },
                {
                    "lat": 5000,
                    "lon": 9000,
                    "date": "2020:04:21 16:58:23"
                },
                {
                    "lat": 5000,
                    "lon": 9000,
                    "date": "2020:04:21 16:58:23"
                }
            ],
            "id_tournee": 33,
            "created_at": "2020-04-23 18:10:32",
            "updated_at": "2020-04-23 22:26:53"

Any idea to do that !

Upvotes: 0

Views: 112

Answers (1)

Dipak Mewada
Dipak Mewada

Reputation: 365

You need to do it as below::

return $this->hasOne(GeoTournee::class, 'id_tournee','id_tournee')->select(['id_tournee', 'itineraire']);

You can also try eager loading:

$user = App\User::with(['geoTournee' => function ($query) {
    $query->select('itineraire');
}])->get();

Or below

App\User::with('geoTournee:itineraire')->get();

Upvotes: 1

Related Questions