Tự học Laravel: (22) ADD THE HELPER FUNCTION

This time, I will add an article deletion function. You will also create helper functions through this task.

ROUTING

// routes/web.php
 
Route::get('articles', 'ArticlesController@index');
Route::get('articles/create', 'ArticlesController@create');
Route::get('articles/{id}', 'ArticlesController@show');
Route::post('articles', 'ArticlesController@store');
Route::get('articles/{id}/edit', 'ArticlesController@edit');
Route::patch('articles/{id}', 'ArticlesController@update');
Route::delete('articles/{id}', 'ArticlesController@destroy');  //mới bổ xung

Add a route to ArticlesController@destroy. The method is delete.

CONTROLLER

// app/Http/Controllers/ArticlesController.php
    ...
    public function destroy($id) {
        $article = Article::findOrFail($id);
 
        $article->delete();
 
        return redirect('articles');
    }

The article is searched with $id and deleted by the delete() method.

VIEW

Add a delete button to the article display screen.

// resoruces/views/articles/show.blade.php
 
@extends('layout')
 
@section('content')
    <h1>{{ $article->title }}</h1> 
    ... 
    <div>
 
        {!! Form::open(['method' => 'DELETE', 'url' => ['articles', $article->id], 'class' => 'd-inline']) !!}
            {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
        {!! Form::close() !!}
 
        ...
    </div>
@endsection

Create a Form to request a DELETE and add a delete button. 
Now you can delete articles.

HELPER

Let’s cut out the creation part of Form that requests DELETE as a helper function.

ADD HELPER.PHP

Create a new helper.php. Add delete_form() function to generate Form tag and reply.

<?php
// app/Http/helper.php
 
function delete_form($url, $label = 'Delete')
{
    $form = Form::open(['method' => 'DELETE', 'url' => $url, 'class' => 'd-inline']);
    $form .= Form::submit($label, ['class' => 'btn btn-danger']);
    $form .= Form::close();
 
    return $form;
}

AUTOLOAD SETTING

Modify composer.json so that helper.php is auto-loaded into Laravel.

{
    ...
 
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App": "app/"
        },
        "files": ["app/Http/helper.php"]
    },
 
    ...
}

After changing the auto load setting of composer.json, you need to reflect the setting with the following command.

composer dump-autoload

USE OF HELPER

Modify show.blade.php to use delete_form().

// resoruces/views/articles/show.blade.php
 
@extends('layout')
 
@section('content')
    <h1>{{ $article->title }}</h1> 
    ...
    <div>      
        {!! delete_form(['articles', $article->id]) !!}
 
        <a href="{{ action('ArticlesController@index') }}" class="btn btn-secondary float-right">
            Index
        </a>
    </div>
@endsection

Oita was cleaned by using the delete_form helper. Also, the delete_form helper can be reused if you need to delete something other than an article.

SUMMARY

You can do the following things.

  • Delete data
  • Create helper function
  • Additional settings for automatic loading

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.