Create restful Api using Laravel 5 with resourceful routes example

Create restful Api using Laravel 5 with resourceful routes example

Create restful API using Laravel 5 with resourceful routes example

It was hard for me to create rest API before but Laravel have developed with rest API concept.

In this tutorial, i will tell you how to create rest API in Laravel with resourceful routes for create, read, update and delete records.

REST known as Representational State Transfer. A REST API basically define set of method which handle request of client and generate responses via HTTP protocol such as GET and POST.

Here i write a rest api in Laravel which return in response of JSON format.

You can create rest APIs in minutes with Laravel.

If you want to quickly create your first test API with Laravel then open your routes.php and paste this code to see the response of API.

  1. Route::get('sample-restful-apis', function()
  2. {
  3. return array(
  4. 1 => "expertphp",
  5. 2 => "demo"
  6. );
  7. });

When you run your URL with above route in browser then you will see the response in JSON-encoded string.

Here is response of above route :

  1. {
  2. "1": "expertphp",
  3. "2": "demo"
  4. }

OK, let's start to create resourceful route :


  1. Route::group(array('prefix' => 'api'), function() {
  2. Route::resource('restful-apis','APIController');
  3. });

Here i add prefix api for all restful api that means all routes for rest api will be written within group with prefix api.

Note : don't forget to exclude all routes start with api in your /app/Http/Middleware/VerifyCsrfToken.php otherwise you will get error of TokenMismatchException you may also disable CSRF while working with cURL to post something.


  1. protected $except = [
  2. 'api/*'
  3. ];

Now i will create a APIController.php where i will handle all request generated by resourceful routes.


  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Product;
  6. class APIController extends Controller
  7. {
  8. public function index(Request $request)
  9. {
  10. $products = Product::paginate(5);
  11. return response(array(
  12. 'error' => false,
  13. 'products' =>$products->toArray(),
  14. ),200);
  15. }
  16. public function store(Request $request)
  17. {
  18. Product::create($request->all());
  19. return response(array(
  20. 'error' => false,
  21. 'message' =>'Product created successfully',
  22. ),200);
  23. }
  24. public function show($id)
  25. {
  26. $product = Product::find($id);
  27. return response(array(
  28. 'error' => false,
  29. 'product' =>$product,
  30. ),200);
  31. }
  32. public function update(Request $request, $id)
  33. {
  34. Product::find($id)->update($request->all());
  35. return response(array(
  36. 'error' => false,
  37. 'message' =>'Product updated successfully',
  38. ),200);
  39. }
  40. public function destroy($id)
  41. {
  42. Product::find($id)->delete();
  43. return response(array(
  44. 'error' => false,
  45. 'message' =>'Product deleted successfully',
  46. ),200);
  47. }
  48. }
  49. ?>

Now create a Product model for products table.

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Product extends Model
  5. {
  6. public $fillable = ['name','details'];
  7. }

Now i will show you response of each controller methods.

Response of index method

In this method i fetch product list.

  1. {
  2. "error": false,
  3. "status": 0,
  4. "products": {
  5. "total": 17,
  6. "per_page": 5,
  7. "current_page": 1,
  8. "last_page": 4,
  9. "next_page_url": "http://localhost/blog/public/restful-apis?page=2",
  10. "prev_page_url": null,
  11. "from": 1,
  12. "to": 5,
  13. "data": [
  14. {
  15. "id": 1,
  16. "name": "product1",
  17. "details": "details of product1",
  18. "created_at": "2016-06-27 10:51:30",
  19. "updated_at": "2016-06-28 12:15:04"
  20. },
  21. {
  22. "id": 2,
  23. "name": "ExpertPHP",
  24. "details": "ExpertPHP.in is a educational portal.",
  25. "created_at": "2016-06-24 10:53:57",
  26. "updated_at": "2016-06-24 10:54:48"
  27. },
  28. {
  29. "id": 7,
  30. "name": "Laravel Restful APIs",
  31. "details": "Expertise in Laravel",
  32. "created_at": "2016-06-24 11:20:32",
  33. "updated_at": "2016-06-28 12:15:37"
  34. },
  35. {
  36. "id": 8,
  37. "name": "product",
  38. "details": "demo product",
  39. "created_at": "2016-06-27 10:51:30",
  40. "updated_at": "2016-06-28 12:15:55"
  41. },
  42. {
  43. "id": 11,
  44. "name": "CSRF",
  45. "details": "CSRF",
  46. "created_at": "2016-06-27 10:51:30",
  47. "updated_at": "2016-06-27 10:51:30"
  48. }
  49. ]
  50. }
  51. }
Response of store method

In this method i create new product.

  1. {"error":false,"message":"Product created successfully"}
Response of show method

In this method i can see the particular product details.

  1. {
  2. "error": false,
  3. "product": {
  4. "id": 1,
  5. "name": "product1",
  6. "details": "details of product1",
  7. "created_at": "2016-06-27 10:51:30",
  8. "updated_at": "2016-06-28 12:15:04"
  9. }
  10. }
Response of update method

In this method i update product details.

  1. {"error":false,"message":"Product updated successfully"}
Response of destroy method

In this method i delete the product.

  1. {"error":false,"message":"Product deleted successfully"}

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
gaziantep escort antep escort porno
avcılar escort