Skip to main content

Getting started

Install the package, define an Eloquent query and item shape, then generate a feed file. The first working export takes about five minutes in an existing Laravel application.

Prerequisites

  • PHP 8.2 or a later 8.x release accepted by Composer
  • Laravel 11.x, 12.x, or 13.x
  • A configured database and filesystem disk

Install and prepare

composer require dragon-code/laravel-feeds
php artisan vendor:publish --tag="feeds"
php artisan migrate
php artisan make:feed User --item

The generator creates the feed and item classes. It also creates the database change that registers the feed. Run that generated migration before the first generation if it was created after the earlier migrate command.

Define the data source

Use an Eloquent builder so records can be read in chunks.

namespace App\Feeds;

use App\Feeds\Items\UserFeedItem;
use App\Models\User;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class UserFeed extends Feed
{
public function builder(): Builder
{
return User::query()
->whereNotNull('email_verified_at')
->where('created_at', '>', now()->subYear());
}

public function item(Model $model): FeedItem
{
return new UserFeedItem($model);
}
}

Define each item

namespace App\Feeds\Items;

use DragonCode\LaravelFeed\Feeds\Items\FeedItem;

/** @property-read \App\Models\User $model */
class UserFeedItem extends FeedItem
{
public function toArray(): array
{
return [
'name' => $this->model->name,
'email' => $this->model->email,
];
}
}

Generate the file

php artisan feed:generate

The command reports the status of each feed; normal console output does not include generated file paths. Feed classes use XML and the public disk by default. Change either value in the feed class.

Next steps