How to make json : previous, next on detail page

Published 7 months ago by mipusy

I want to previous and next links on detail page. My project is on laravel 5.5, vue.js.

ArticlesController.php

public function show($id){

        $article = articles::with('tags')->find($id);

        $previous = articles::where('id', '<', $article->id)->orderBy('id','desc')->first();
        $next = articles::where('id', '>', $article->id)->orderBy('id','desc')->first();

        return Response()->json($article->with('previous', $previous)->with('next', $next));

    }

Result is "{}". It doesn't work.

How should I write it?

Best Answer (As Selected By mipusy)
Dry7

@mipusy

return Response()->json([
    'article' => $article,
    'previous' => $previous,
    'next' => $next
]);
Dry7
Dry7
7 months ago (151,880 XP)

@mipusy

return Response()->json([
    'article' => $article,
    'previous' => $previous,
    'next' => $next
]);
mipusy

Thank you. It does work.

Please sign in or create an account to participate in this conversation.