← Précédent · Retour au README · Suivant →
Créer les fichiers
php artisan make:feed Instagram
Remplir le flux
Dans la classe générée, remplacez extends Feed par extends InstagramFeedPreset :
<?php
declare(strict_types=1);
namespace App\Feeds;
use App\Models\Product;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use DragonCode\LaravelFeed\Presets\InstagramFeedPreset;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class InstagramFeed extends InstagramFeedPreset
{
public function builder(): Builder
{
return Product::query();
}
public function item(Model $model): FeedItem
{
return parent::item($model)
->title($model->title)
->description($model->description)
->brand($model->brand) // By default, null
->url($model->url)
->price(price: $model->price, salePrice: $model->price) // By default, salePrice = price
->image($model->images[0])
->images($model->images) // By default, null
->availability($model->quantity > 0 ? 'in stock' : 'out of stock') // By default, 'in stock'
->status($model->quantity > 0 ? 'active' : 'inactive') // By default, 'active'
->condition('new') // By default, 'new'
->group(12345) // By default, null
->googleCategory(123) // By default, null
->facebookCategory(456) // By default, null
->additional([
'g:foo' => 'Some foo',
'g:bar' => 'Some bar',
'g:baz' => [
'@attributes' => ['qwe' => 'rty'],
'@value' => 'Some baz',
],
'@g:arrayable' => [
'a',
'b',
'c',
],
]);
}
public function filename(): string
{
return 'instagram.xml';
}
}
Activer
Vérifiez le fichier d’opération ou de migration généré, puis exécutez la commande appropriée :
# For Laravel Deploy Operations
php artisan operations
# For Laravel Migrations
php artisan migrate
Générer le flux
Générez le flux avec la commande suivante :
php artisan feed:generate
Résultat
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>Laravel</title>
<link>https://example.com</link>
<offers>
<item>
<g:id>1</g:id>
<g:title><![CDATA[Some 1]]></g:title>
<g:description><![CDATA[Some description 1]]></g:description>
<g:link>https://example.com/products/ea-voluptatum-fuga-odio-expedita</g:link>
<g:image_link>https://via.placeholder.com/640x480.png/008877?text=repudiandae</g:image_link>
<g:additional_image_link>https://via.placeholder.com/640x480.png/008877?text=repudiandae</g:additional_image_link>
<g:brand>The Best</g:brand>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price>100</g:price>
<g:sale_price>100</g:sale_price>
<g:item_group_id>12345</g:item_group_id>
<g:status>active</g:status>
<g:google_product_category>123</g:google_product_category>
<g:fb_product_category>456</g:fb_product_category>
<g:foo>Some foo</g:foo>
<g:bar>Some bar</g:bar>
<g:baz qwe="rty">Some baz</g:baz>
<g:arrayable>a</g:arrayable>
<g:arrayable>b</g:arrayable>
<g:arrayable>c</g:arrayable>
</item>
<item>
<g:id>2</g:id>
<g:title><![CDATA[Some 2]]></g:title>
<g:description><![CDATA[Some description 2]]></g:description>
<g:link>https://example.com/products/dolores-rerum-ut-consequatur-in</g:link>
<g:image_link>https://via.placeholder.com/640x480.png/009966?text=beatae</g:image_link>
<g:additional_image_link>https://via.placeholder.com/640x480.png/009966?text=beatae</g:additional_image_link>
<g:additional_image_link>https://via.placeholder.com/640x480.png/000011?text=deleniti</g:additional_image_link>
<g:additional_image_link>https://via.placeholder.com/640x480.png/009999?text=voluptates</g:additional_image_link>
<g:brand>The Best</g:brand>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price>250</g:price>
<g:sale_price>250</g:sale_price>
<g:item_group_id>12345</g:item_group_id>
<g:status>active</g:status>
<g:google_product_category>123</g:google_product_category>
<g:fb_product_category>456</g:fb_product_category>
<g:foo>Some foo</g:foo>
<g:bar>Some bar</g:bar>
<g:baz qwe="rty">Some baz</g:baz>
<g:arrayable>a</g:arrayable>
<g:arrayable>b</g:arrayable>
<g:arrayable>c</g:arrayable>
</item>
</offers>
</channel>
</rss>