Mohamed Benhida

admin@devma.net

December 8, 2017

RESTfull API With Laravel

Hey,

We Will talk about how to handle the API Restfull links in laravel and how laravel provide us all we will need to make this works.

After we begin i suppose you know about CRUD (Create,Read,update,delete) each one has his particular method like :

Create => Post Method

Read => Get Method

Update => PUT or PATCH Method

DELETE => DELETE Method

 

We begin by creating our first controller using this command :

php artisan make:controller BlogController -r

And it will create you a full controller with the needed methods like this

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

in our BlogController example each method has his own role or explanation :

@index => is For showing all The blogs for example (use GET)

@create => You put the view form of creating a blog (use GET)

@store => You store the blog on database (use Post)

@show => You view a single blog (use GET)

@edit => You put the view form of editing a blog (use GET)

@update => You update the blog on database (use Patch or Path)

@destroy => You delete a particular blog (use DELETE)

For Default Laravel provide you with Route::resource  that will make you all the links related to the controller methods we talked above route::resource($prefix,$controllerName)

Route::resource('blog','BlogController');

Now We talk about how you can handle this methods on blade view

For Creating a Blog We use POST

<form method="POST" action="{{ route('blog.store') }}">
    {{ csrf_field() }}
</form>

For updates like we said we can't use Post or Get so we will need a method field helper provider by laravel

{{ method_field('METHOD') }}

<form method="POST" action="{{ route('blog.update',1) }}">
    {{ csrf_field() }}
    {{ method_field('PUT') }}
</form>

Or

<form method="POST" action="{{ route('blog.update',1) }}">
    {{ csrf_field() }}
    {{ method_field('PATCH') }}
</form>

And For Delete We use DELETE Method

<form method="POST" action="{{ route('blog.destroy',1) }}">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
</form>

I hope you enjoyed reading this post.

© Mohamed Benhida.

Blog | Packages | About