← Previous · Back to README · Next →
Create feeds
The most convenient way to create the necessary classes is to use the Laravel Idea plugin for PhpStorm.


To create a feed class, use the make:feed console command:
php artisan make:feed <name>
This will create a feed file. For example, app/Feeds/UserFeed.php.
This console command can also create the item and info classes along with the feed class. To do this, use the --item and --info options.
# Create a feed class and info class
php artisan make:feed <name> --info
# Create a feed class and item class
php artisan make:feed <name> --item
# Create a feed class, item class, and info class
php artisan make:feed <name> --item --info
Shortcuts are also available:
# Create a feed class and item class
php artisan make:feed <name> -t
# Create a feed class and info class
php artisan make:feed <name> -i
# Create a feed class, item class, and info class
php artisan make:feed <name> -it
When creating a feed, an operation or a migration will also be created to add it to the database.
If the project uses Laravel Deploy Operations, then an operation class will be created; otherwise, a migration class will be created.
This is required to add and manage feed information in the database.
Populating feeds
Feed
Populate the main feed class. For example:
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);
}
}
Feed item
Populate the feed item class. For example:
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,
];
}
}
Optional values
To omit a field when its value is unavailable, return an instance of DragonCode\LaravelFeed\Data\OptionalData as its value:
namespace App\Feeds\Items;
use DragonCode\LaravelFeed\Data\OptionalData;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
class UserFeedItem extends FeedItem
{
public function toArray(): array
{
return [
'id' => $this->model->id,
'name' => $this->model->name,
'cityId' => $this->model->city?->id ?? new OptionalData,
'createdAt' => $this->model->created_at,
'updatedAt' => $this->model->updated_at ?? new OptionalData,
];
}
}
Optional fields are removed from JSON and JSON Lines objects and from XML and RSS elements, including regular nested fields. CSV keeps the column position and writes an empty field.
If your application uses Spatie Laravel Data, you can return Spatie\LaravelData\Optional instead. Both optional value classes are handled automatically.
use Spatie\LaravelData\Optional;
'updatedAt' => $this->model->updated_at ?? Optional::create(),
Separate output files
Generating separate files for different business conditions is an advanced output scenario. Read the separate output files guide, then use the multiple targeted feed files recipe as a complete example.
File format
You can find the list of available formats on the supported formats page.
You can generate feeds in different formats. A feed class can be correctly exported only to the format for which it is intended. This is due to the use of directives and other format specifics.
To apply the required format, simply override the $format property in the feed class:
namespace App\Feeds;
use DragonCode\LaravelFeed\Enums\FeedFormatEnum;
use DragonCode\LaravelFeed\Feeds\Feed;
use Illuminate\Database\Eloquent\Builder;
class UserFeed extends Feed
{
protected FeedFormatEnum $format = FeedFormatEnum::Json;
public function builder(): Builder
{
//
}
}