Elements
Root element
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-04 04:08:12">
<user>
<id>1</id>
<name>Mr. Jaylin Schmitt</name>
</user>
<user>
<id>2</id>
<name>Brenden Murazik DVM</name>
</user>
</foo>
Information
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>Kaelyn Robel</name>
</user>
<user>
<id>2</id>
<name>Kailee Gutkowski</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>Dr. Marshall Jakubowski Sr.</name>
</user>
<user>
<id>2</id>
<name>Mafalda Trantow</name>
</user>
</info_method>
Header & footer
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>Carmella Prohaska IV</name>
</user>
<user>
<id>2</id>
<name>Emiliano Shields II</name>
</user>
</header_footer>
<g:footer>This is a custom footer element</g:footer>
Attributes
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-06T00:27:10+00:00">
<id>1</id>
<name>Katharina Jacobi</name>
</user>
<user created_at="2025-09-06T00:27:10+00:00">
<id>2</id>
<name>Kamron Lesch V</name>
</user>
</attribute>
05 September 2025