Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass resources to view?

I am newbie in laravel. I want to know how can I pass the data result from the resource collection to view. I saw in tutorial if I have not uses resource then I can simply return like this to view.

return view('articles.index')->with('posts', $posts);

I was practicing using resource collection, I am not sure how to return to view.

public function index()
{
    $articles = Article::all();
    return ArticleResource::collection($articles);
}

I have tried using following which returns data to the view but I am not being able to show that data.

return view('articles.index')->with('articles', ArticleResource::collection($articles));
like image 420
user1687891 Avatar asked Dec 20 '25 03:12

user1687891


1 Answers

'article' => (new ArticleResource($article))->resolve(),

Consider that this way it become an array.

like image 55
nulele Avatar answered Dec 22 '25 16:12

nulele



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!