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
okey metin2 pvp serverler metin2 pvp
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
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort