D
D
Dimon3x2019-09-17 18:39:03
Laravel
Dimon3x, 2019-09-17 18:39:03

How can I find out which model a sample belongs to?

Now the array includes those fields that are in the table (id, text).
Is it possible to somehow supplement this array, for each item, in order to
know which table this refers to?
Post::all()

#perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:2 [▼
        "id" => 1
        "text" => "Общие положения"
      ]
      #original: array:2 [▶]
      #changes: []
      #casts: []

I want to load all this from different models into one select for html and then I have to somehow filter which model this option belongs to

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin B., 2019-09-17
@Kostik_1993

$collection->map(function ($item, $key) {
      $item->setAttribute('table', $item->getTable());
});

public function getTableAttribute()
  {
    return $this->getTable();
  }

A
Alex Wells, 2019-09-19
@Alex_Wells

Why merge something and then filter? You can not transfer two (or how many you have there) different collections or what?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question