对象没有实现`Spatie \ Feed \ Feedable`

时间:2018-01-04 07:19:41

标签: php laravel-5 rss spatie facebook-rss

我得到的对象没有实现Spatie\Feed\Feedable错误并且在

  

Error.log文件:LOG.error:对象没有实现   Spatie \ Feed \ Feedable {"例外":{" subject":null}}

我的NewsItem.php文件代码为:

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Feed\Feedable;
use Spatie\Feed\FeedItem;
use Carbon;
use DB;

class NewsItem extends Model implements Feedable
{

    public function toFeedItem()
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'summary' => $this->summary,
            'updated' => $this->updated_at,
            'link' => $this->link,
            'author' => $this->author,
        ];
    }

    // app/NewsItem.php

    public static function getFeedItems()
    {
        $query = DB::table('news')
                ->select('news.news_news_iid as id', 'pulse.city_pulse_title as title', 'news.news_description as summary', 'pulse.city_pulse_modified_on as updated_at', 'alias.url_alias_name as link', 'news.news_author_name as author')
                ->Join('city_pulse as pulse', 'pulse.city_pulse_city_pulse_iid', '=', 'news.news_news_id')
                ->Join('url_alias as alias', 'pulse.city_pulse_city_pulse_iid', '=', 'alias.url_alias_content_iid')
                ->where('alias.url_alias_content_type_iid', 9)
                ->whereIn('news.news_news_iid', [788, 785, 771, 802, 814])
                ->get()->toArray();
        foreach ($query as $key => $value) {
            $query[$key]->created_at = \Carbon::parse($value->updated_at)->format('dS F Y H:i a');
            $query[$key]->link = 'http://ichangemycity.com/' . $value->link;
        }
        return $query;
    }
}

我的Feed.php文件代码是

<?php
return [
    'feeds' => [
        'main' => [
            /*
             * Here you can specify which class and method will return
             * the items that should appear in the feed. For example:
             * 'App\Model@getAllFeedItems'
             *
             * You can also pass an argument to that method:
             * ['App\Model@getAllFeedItems', 'argument']
             */
            'items' => 'App\Models\NewsItem@getFeedItems',
            /*
             * The feed will be available on this url.
             */
            'url' => '/feed',
            'title' => 'All newsitems on mysite.com',
        ],
    ],
];

Screenshot of error

任何人都可以帮助我。

提前谢谢。 :)

1 个答案:

答案 0 :(得分:0)

您的getFeedItems应返回包含实现FeedItem界面的项目/实例的地图,而不是array,也不会stdclass。换句话说,您应该使用eloquent而不是DB查询。

public static function getFeedItems()
{
    return static::whereHas('urlAlias', function ($q) {
       $q->where('url_alias_content_type_iid', 9);
    })->->whereIn('news_news_iid', [788, 785, 771, 802, 814])->get();
}

由于上面的方法已经实现了它,假设已经定义了relationships,上面的例子就是你应该写的东西。虽然没有提供join表,但雄辩为您提供了更多的优势,除了它更短的写作,当然更具可读性。 Eloquent还可以mutate属性/列,就好像您仍需要as别名之类的内容。

如果您对利用雄辩的关系毫无兴趣,我认为应该可以使用猴子补丁。

public static function getFeedItems()
{

    $query = DB::table('news')
            ->select('news.news_news_iid as id', 'pulse.city_pulse_title as title', 'news.news_description as summary', 'pulse.city_pulse_modified_on as updated_at', 'alias.url_alias_name as link', 'news.news_author_name as author')
            ->Join('city_pulse as pulse', 'pulse.city_pulse_city_pulse_iid', '=', 'news.news_news_id')
            ->Join('url_alias as alias', 'pulse.city_pulse_city_pulse_iid', '=', 'alias.url_alias_content_iid')
            ->where('alias.url_alias_content_type_iid', 9)
            ->whereIn('news.news_news_iid', [788, 785, 771, 802, 814])
            ->get()->toArray();

    $items = collect(); // create new collection

    foreach ($query as $key => $value) {
        $query[$key]->created_at = \Carbon::parse($value->updated_at)->format('dS F Y H:i a');
        $query[$key]->link = 'http://ichangemycity.com/' . $value->link;

        $instance = new static; // create new NewsItem model class

        foreach($value as $attribute => $_value) {
            $instance->{$attribute} = $_value; // copy available attribute/column and its value
        }

        $instance->exists = true; // tell this model is already exists, forcely
        $items[$key] = $instance; // assign this model to the collection
    }

    return $items; // return the items containing this model class (which implements FeedItem interface)
}

TL; DR:你应该使用雄辩和雄辩的关系。