M
M
mrdragon90002019-05-17 21:34:12
Laravel
mrdragon9000, 2019-05-17 21:34:12

Laravel how to override object property?

There is this simple code:

$offer = Offer::with([
    'countries' => function ($query) {
        $query->select('offer_countries.id', 'offer_countries.abbr');
    }
])->findOrFail($id);

Let's say I want to make an array from the $offer->countries collection only from the id-shniks of each country and override this property. How to do it?
The variant below does not work, $offer->countries remains unchanged during the output:
$offer->countries = $offer->countries->map(function($country) {
    return $country->id;
});
return $offer;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2019-05-17
@Sanasol

dump() before and after map?
Is it really unchanged?
maybe there is something wrong after the return?
map can be changed to:
$offer->countries->pluck('id');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question