Skip to main content
Version: 1.x

← Previous · Back to README · Next →

Elements

Root element​

Formats: XML, RSS, JSON

tip

By default, the name of the root element is derived from the base name of the feed class.

For example, for the App\Feeds\FooBarFeed class, the name is foo_bar.

To add a root element and/or its attributes, override the root method:

namespace App\Feeds;

use App\Models\User;
use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use Illuminate\Database\Eloquent\Builder;

use function now;

class RootElementFeed extends Feed
{
public function builder(): Builder
{
return User::query();
}

public function root(): ElementData
{
return new ElementData(
name : 'foo',
attributes: [
'count' => $this->builder()->count(),

'generated_at' => now(),
],
);
}
}

To disable the addition of the root element, specify its name as empty — null or "".

This code will result in:

<?xml version="1.0" encoding="UTF-8"?>
<foo count="2" generated_at="2025-09-04T04:08:12+00:00">

<user>
<id>1</id>
<name>User 1</name>
</user>
<user>
<id>2</id>
<name>User 2</name>
</user>

</foo>

Information​

Formats: XML, RSS, JSON, JSONL, CSV

To add information to the beginning of the root element (if present) or without it, override the info method in the feed class and create an object that extends the DragonCode\LaravelFeed\Feeds\Info\FeedInfo class:

namespace App\Feeds;

use App\Feeds\Info\InfoMethodFeedInfo;
use App\Models\User;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
use Illuminate\Database\Eloquent\Builder;

class InfoMethodFeed extends Feed
{
public function builder(): Builder
{
return User::query();
}

public function info(): FeedInfo
{
return new InfoMethodFeedInfo;
}
}
namespace App\Feeds\Info;

use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;

use function config;

class InfoMethodFeedInfo extends FeedInfo
{
public function toArray(): array
{
return [
'company' => config('app.name'),
'url' => config('app.url'),
];
}
}

This code will result in:

<?xml version="1.0" encoding="UTF-8"?>
<info_method>

<company>Laravel</company>
<url>https://example.com</url>

<user>
<id>1</id>
<name>User 1</name>
</user>
<user>
<id>2</id>
<name>User 2</name>
</user>

</info_method>

If you need to change the order in which the root element is output relative to the information block, set the beforeInfo parameter in the root method:

namespace App\Feeds;

use App\Feeds\Info\InfoMethodFeedInfo;
use App\Models\User;
use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
use Illuminate\Database\Eloquent\Builder;

class InfoMethodBeforeFalseTest extends Feed
{
public function builder(): Builder
{
return User::query();
}

public function root(): ElementData
{
return new ElementData(
name : 'info_method',
beforeInfo: false
);
}

public function info(): FeedInfo
{
return new InfoMethodFeedInfo;
}
}

This code will result in:

<?xml version="1.0" encoding="UTF-8"?>
<company>Laravel</company>
<url>https://example.com</url>

<info_method>

<user>
<id>1</id>
<name>User 1</name>
</user>
<user>
<id>2</id>
<name>User 2</name>
</user>

</info_method>

Formats: XML, RSS, JSON

To change the header and footer, override the header and footer methods:

namespace App\Feeds;

use App\Models\User;
use DragonCode\LaravelFeed\Feeds\Feed;
use Illuminate\Database\Eloquent\Builder;

class HeaderFooterFeed extends Feed
{
public function builder(): Builder
{
return User::query();
}

public function header(): string
{
return '<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">';
}

public function footer(): string
{
return "\n<g:footer>This is a custom footer element</g:footer>";
}
}

This code will result in:

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<header_footer>

<user>
<id>1</id>
<name>User 1</name>
</user>
<user>
<id>2</id>
<name>User 2</name>
</user>

</header_footer>

<g:footer>This is a custom footer element</g:footer>

Attributes​

Formats: XML, RSS

namespace App\Feeds;

use App\Feeds\Items\AttributeFeedItem;
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 AttributeFeed extends Feed
{
public function builder(): Builder
{
return User::query();
}

public function item(Model $model): FeedItem
{
return new AttributeFeedItem($model);
}
}
namespace App\Feeds\Items;

use DragonCode\LaravelFeed\Feeds\Items\FeedItem;

class AttributeFeedItem extends FeedItem
{
public function attributes(): array
{
return [
'created_at' => $this->model->created_at,
];
}
}

This code will result in:

<?xml version="1.0" encoding="UTF-8"?>
<attribute>

<user created_at="2025-09-04T04:08:12+00:00">
<id>1</id>
<name>User 1</name>
</user>
<user created_at="2025-09-04T04:08:12+00:00">
<id>2</id>
<name>User 2</name>
</user>

</attribute>

Split files​

Formats: XML, RSS, JSON, JSONL, CSV

If necessary, you can limit the number of lines output to a file, as well as the number of files themselves. To do this, use the perFile and maxFiles functions.

namespace App\Feeds;

use App\Models\User;
use DragonCode\LaravelFeed\Feeds\Feed;
use Illuminate\Database\Eloquent\Builder;

class AttributeFeed extends Feed
{
public function builder(): Builder
{
return User::query();
}

public function perFile(): int
{
return 100;
}

public function maxFiles(): int
{
return 10;
}
}

The methods work independently of each other.

When splitting, a sequential number will be added to the file names. For example:

  • attribute-1.xml
  • attribute-2.xml
  • attribute-3.xml

See Also​