Building a REST API with Laravel? Standardise your responses with JSend

Shalvah
Shalvah
Jun 21, 2017 · 1 min read
$post = Post::create($details);
$response = ["status" => "success", "data" => $post->toArray()];
return response(json_encode($response), 200, ["Content-Type" => "application/json"]);
$post = Post::create($details);
return jsend_success($post);
return jsend_error($errorMessage);
$data = ["title" => "title is required"];
return jsend_fail($data);

Shalvah

Written by

Shalvah

Let’s break the code down. Also write @ dev.to/shalvah

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade