Extending Modules

Overwriting views

If you want to extend one of the modules and add new fields (or remove some) you will need to overwrite the views first. To do that just copy a view from the vendor folder to the resources folder:

To edit theadmin/userview simply copy vendor/taffovelikoff/hotcoffee/resources/views/admin/user.blade.php to resources/views/vendor/hotcoffee/admin/user.blade.php

Extending the user module

If you need to add more fields in the user module you first have to copy the user.blade.php from vendor/taffovelikoff/hotcoffee/resources/views/admin to resources/views/vendor/hotcoffee/admin and add your new field where you desire (for example right after "job title"):

<!-- Job Title -->
<div class="col-lg-6">
  <div class="form-group">
    <label class="form-control-label" for="input-last-name">{{ __('hotcoffee::admin.user_job') }}</label>
    <div @if($errors->has('job_title')) class="has-danger" @endif>
      <input type="text" name="job_title" id="input-last-name" class="form-control form-control-alternative @if($errors->has('job_title')) is-invalid-alt @endif" @if(session('post')) value="{{ session('post.job_title') }}" @elseif(isset($edit)) value="{{ $edit->address->job_title }}" @endif>
    </div>
  </div>
</div>

<!-- NEW FIELD -->
<!-- Phone -->
<div class="col-lg-6">
  <div class="form-group">
    <label class="form-control-label" for="input-phone">Phone Number</label>
    <div @if($errors->has('phone')) class="has-danger" @endif>
      <input type="text" name="phone" id="input-phone" class="form-control form-control-alternative @if($errors->has('phone')) is-invalid-alt @endif" @if(session('post')) value="{{ session('post.phone') }}" @elseif(isset($edit)) value="{{ $edit->address->phone}}" @endif>
    </div>
  </div>
</div>

It's recommended to keep the user information in the user_address table, instead of the users table. Now create a migration to add the new fields to the table.

php artisan make:migration add_surname_field_to_user_addresses_table
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddPhoneFieldToUserAddressesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('user_addresses', function (Blueprint $table) {
            $table->string('phone')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('user_addresses', function (Blueprint $table) {
           $table->dropColumn('phone');
        });
    }
}

Now you need to publish the user configuration file:

php artisan vendor:publish --tag=hotcoffee_config_users

In this file you can change some user configurations, but most importantly you can change the UserAddress namespace.

return [

    /*
    |--------------------------------------------------------------------------
    | User Address Model
    |--------------------------------------------------------------------------
    | This allows to extend the model.
    |
    */

    'address_model'    => \App\Models\UserAddress,

Now create a new UserAddress model and populate the fillable attribute.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserAddress extends Model
{
    protected $fillable = ['city', 'country', 'job_title', 'company', 'bio', 'phone'];
}

If you need to do some validations you can add the rules and messages again in the users.php config file:

/*
|--------------------------------------------------------------------------
| Additional validation rules
|--------------------------------------------------------------------------
| Any additional validation rules and messages when updating or creating
| user. You can also overwrite the defaults.
*/

'validations' => [
    'normal'    => [
        'phone' => 'required', // This will add a new validation for "phone".
    ],
    'translatable'  => [], // Add rules for translatable fields heree
],

'messages' => [
    'normal'    => [
        'phone.required' => 'Please provide a phone number.',
    ],
    'translatable'  => [], // Add messages for translatable fields heree
],

If you need to do something very specific you can copy the UserController.php from vendor/taffovelikoff/hotcoffee/src/Http/Controllers/Admin to app/Http/Controllers/Admin and re-define all the routes in your routes/web.php:

<?php
/**
 * ADMIN
 * Below are all custom admin routes for your application.
 */
HotCoffee::routes();

Route::group(['prefix' => config('hotcoffee.prefix'), 'middleware' => ['hotcoffee']], function () {

    // Dashboard
    Route::get('/dashboard', 'Admin\DashboardController@index')->name('hotcoffee.admin.dashboard');

    // ===== OVERWRITING USERS MODULE ROUTES ===== //
    
    // Users (keep route names the same)
    Route::group(['prefix' => 'users'], function () {
        Route::get('/', 'Admin\UserController@index')->name('hotcoffee.admin.users.index');

        Route::get('/create', 'Admin\UserController@create')->name('hotcoffee.admin.users.create');
        Route::post('/', 'Admin\UserController@store')->name('hotcoffee.admin.users.store');

        Route::get('/{user}', 'Admin\UserController@edit')->name('hotcoffee.admin.users.edit');
        Route::post('/{user}', 'Admin\UserController@update')->name('hotcoffee.admin.users.update');

        Route::delete('/{user}', 'Admin\UserController@destroy')->name('hotcoffee.admin.users.destroy');
    });
});

Now the admin panel will use the controller from the app/Http/Controllers/Admin folder, instead the one in vendor/taffovelikoff/hotcoffee/src/Http/Controllers/Admin meaning you can edit the controller.

If you are not sure about the route names of a module you can check them either with php artisan route:list or open vendor/taffovelikoff/hotcoffee/routes/web.php.

Extending the article and info page modules

You can extend or change the article or info page modules in a very similar way.

php artisan vendor:publish --tag=hotcoffee_config_articles
php artisan vendor:publish --tag=hotcoffee_config_infopages

You can create new models, that will extend the ones from the package.

<?php

namespace App\Models;

class InfoPage extends \TaffoVelikoff\Hotcoffee\InfoPage
{
    //
}

Now change model in the configuration:

<?php

/*
|--------------------------------------------------------------------------
| Info Pages
|--------------------------------------------------------------------------
| Configuration concerning the info pages.
|
*/

return [

    /*
    |--------------------------------------------------------------------------
    | Models
    |--------------------------------------------------------------------------
    | This allows to extend the model.
    |
    */
    'model' => App\Models\InfoPage::class,

Last updated