PHP code example of lazerg / laravel-response-helpers

1. Go to this page and download the library: Download lazerg/laravel-response-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lazerg / laravel-response-helpers example snippets


    # Response with success message
    $this->responseMessage('Successfully updated');

    # Response with error message
    $this->responseMessage('Failed to update', 400);

    # Successful response with data
    $this->responseData($user);

    # Error response with data
    $this->responseData($user, 400);

    # Response with success message and data
    $this->response('Successfully updated', $user);

    # Response with error message and data
    $this->response('Failed to update', $user, 400);

    # Success response with resourceful data
    $this->responseResourceful(UsersResource::class, $users);

    # Error response with resourceful data
    $this->responseResourceful(UsersResource::class, $users, 400);

    # Success response with resourceful message and data
    $this->responseJsonResourceful(UsersResource::class, $users, 'Successfully updated');

    # Error response with resourceful message and data
    $this->responseJsonResourceful(UsersResource::class, $users, , 'Failed to update', 400);


$this->getJson(route('users.index'))
    ->assertOk()
    ->assertJsonCountData(5)

    # amount of $users->first()->posts
    ->assertJsonCountData(2, '0.posts')
    ->assertJsonStructureData([
        ['id', 'name', 'email']
    ]);
    ->assertJsonData([
        'id' => 1,
        'name' => 'John Doe',
        'email' => '[email protected]'
    ]);