namespace App\Exports;
use App\Supplier;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
class SuppliersExport implements FromQuery
{
use Exportable;
public function query()
{
// since you need the last one, not the first
return Supplier::query()->where('user_id', auth()->id())->last();
}
}
You can also use ->latest()->first() to order the suppliers in descending order and get the first item, in case the last is not the newest one.
@NAKOV - Apologies, my mistake I did mean to write ->last() above.
Unfortunately, using ->last() gives me an undefined method exception BadMethodCallException Call to undefined method Illuminate\Database\Eloquent\Builder::last().
It seems to strip off everything after the where() clause, so the exported CSV is returning all suppliers where the user_id is equal to the auth ID, ignoring latest and first?
If your exporting a collection you need to update your class
use Maatwebsite\Excel\Concerns\FromCollection;
class SuppliersExport implements FromCollection
{
public function collection()
{
return Supplier::query()->where('user_id', auth()->id())->latest()->first();
}
}