How to use cursor() with associated data in Laravel?

Asked

Viewed 45 times

0

$arrayCarros = [];    

$model = new Carros();

$carros = $model->with('montadoras')->cursor();

foreach($carros as $carros) {
    array_push($arrayCarros, $carro->toArray());
}

Running the above code, I get the following output:

        [
            [
                'id' => 1,
                'modelo' => 'Fit',
                'ano' => 2015
            ]
        ]

However, if I run the code below:

$arrayCarros = [];    

$model = new Carros();

$carros = $model->with('montadoras')->toArray();

I get the following output:

        [
            [
                'id' => 1,
                'modelo' => 'Fit',
                'ano' => 2015,
                'montadoras' => [
                    'id' => 1,
                    'montadora' => 'Honda'
                ]
            ]
        ]

Is there any way to return the associated data together to function cursor of Laravel?

  • From what I could perceive it only carries by calling the method of relationship, and this is not good, it is better to work with get, all, I found nothing conclusive even on Laravel’s website, which has only one small example.

  • 1

    @Virgilionovic - I live up to your words. 'cursor()' is an extremely interesting and useful feature, especially when we are working with large amounts of data. It will be a shame not to be able to use this resource and associated data set. I will continue searching.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.