1

I'm using Laravel Excel to export data. I want to get all the reminders attached to a vet, which are accessed over a hasManyThrough relationship.

I have tried the following code

RemindersExport.php

public function collection()
{
    $vets = Vet::where('is_active', 1)->get();

    foreach($vets as $vet){
        $reminders = $vet->reminders();
    }

    return $reminders;
}

Controller

public function reminders()
{
    return Excel::download(new RemindersExport, 'reminders30days.xlsx');
}

I get the following message...

Method Illuminate\Database\Query\Builder::all does not exist.

2
  • Please elaborate your question, as I can see, where you used someModel::all() ? Commented Oct 19, 2018 at 18:16
  • are you trying to return Vets and Reminders together? If so I recommend this: Vet::where('is_active', 1)with->('reminders')->get(); Commented Oct 19, 2018 at 18:36

1 Answer 1

3

The problem is that you're using the collection method but you should use the query method and laravel-excel excecutes the query for you so you don't have to use the get method at the end of your query, first you sould use the FromQuery concern, after that you should replace your collection method with the query method, write your query and DON'T add the get method at the end of your query

use Maatwebsite\Excel\Concerns\FromQuery;

class VetExport implements FromQuery{
    public function query()
    {
        $vets = Vet::where('is_active', 1);

        foreach($vets as $vet){
            $reminders = $vet->reminders();
        }

        return $reminders;
    }
}

I hope this helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.