M
M
mShpakov2018-08-27 14:31:02
Laravel
mShpakov, 2018-08-27 14:31:02

How to use pluck() at the request level?

Given:
- laravel 5.6
- a model (let's call it A) with a hasMany relationship (let's call it B) to another model. Which has a column structure - id | a_id | key | value
Problem:
Need to retrieve data along with related data via hasMany. And here the problem is that the data received through the connection must first be passed through pluck('value', 'key').
I make the request like this:

$a = A::with('B')->get();
dd($a);

Communication in model A is like this:
public function B()
    {
        return $this->hasMany(B::class);
    }

Is it possible to do this at the request level? Or is there only one option - after receiving the data, loop it and rewrite the data inside the connection via pluck ()?
PS naming is shortened to simplify the semantic load.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2018-08-27
@mShpakov

This cannot be done not only technically, but also in meaning - there must be models on both sides of the relationship.
You need to add the necessary fields to the selection and "after receiving, twist them in a cycle."

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question