jeudi 10 mai 2018

Filter, Sort and Paginate after Fractal Transform

I'm using Fractal transformer in Laravel 5.6

I have a News Model and User Model. News model has a foreign key user_id. when I want to show list of News I transform the data like below:

public function transform(News $news)
{
    return [
        'identifier' => (int) $news->id,
        'title' => (string) $news->title,
        'content' => (string) $news->content,
        'user' => (String) $news->user->f_name . " " . $news->user->l_name,
        'userType' => (String) $news->user->user_type,
        'likeCount' => (int) $news->likes,
    ];
}

And transform function is:

protected function transformData($data, NewsTransformer $transformer) {
    $transformation = fractal($data, new $transformer);
    return $transformation->toArray();
}

Now the problem is that when I transform the list of news it returns array instead of collection and after transformation I cant use operations like filtering the data by userType or sorting the data by user.

So how can I sort, filter and paginate the results by new fields after transformation?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2G2HAaV
via IFTTT

Aucun commentaire:

Enregistrer un commentaire