PHP code example of devtools-marvellous / laravel-core-kit

1. Go to this page and download the library: Download devtools-marvellous/laravel-core-kit 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/ */

    

devtools-marvellous / laravel-core-kit example snippets


    [
      'mysql' => [
          //...,
          'strict' => false,
          //...,
      ]
    ];
    

    [
        'api' => [
                    //'driver' => 'token', you can remove this row.
                    'driver' => 'passport',
                    'provider' => 'users',
                    'hash' => false,
                ],
    ];
    

    [
        'default' => [
                    'url'                => env('REDIS_URL'),
                    'host'               => env('REDIS_HOST', '127.0.0.1'),
                    'password'           => env('REDIS_PASSWORD', NULL),
                    'port'               => env('REDIS_PORT', '6379'),
                    'database'           => env('REDIS_DB', '0'),
                    'read_write_timeout' => 0, // Add this into each settings block to prevent errors - "error while reading line from the server."
                ],
    ];
    

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
      //...,
      'verified' => \AttractCores\LaravelCoreKit\Http\Middleware\EnsureEmailIsVerified::class,
      //...,
    ];
    

    $this->call([
      	    DefaultAdminSeeder::class,
      	    DefaultRolesAndPermissionsSeeder::class
      	]);
    

        namespace Database\Seeders;
        
        use App\Models\Permission;
        use App\Models\Role;
        use AttractCores\LaravelCoreAuth\Database\Seeders\DefaultRolesAndPermissionsSeeder as CoreDefaultRolesAndPermissionsSeeder;
        use AttractCores\LaravelCoreAuth\Resolvers\CorePermission;
        use AttractCores\LaravelCoreAuth\Resolvers\CoreRole;
        
        /**
         * Class DefaultRolesAndPermissionsSeeder
         *
         * @package AttractCores\LaravelCoreAuth\Database\Seeders\Publishes
         * Date: 16.12.2020
         * Version: 1.0
         * Author: Yure Nery <[email protected]>
         */
        class DefaultRolesAndPermissionsSeeder extends CoreDefaultRolesAndPermissionsSeeder
        {
            /**
             * Seed the application's database.
             *
             * @return void
             */
            public function run()
            {
                parent::run();
        
                $permissions = CorePermission::all();
                $permissionSlugFieldName = CorePermission::getSlugField();
        
                if ( ! $permissions->contains($permissionSlugFieldName, Permission::CAN_ORGANIZATION_ACCESS) ) {
        
                    CorePermission::factory()
                                  ->createOne([ 'name_en' => 'Can have Organisation access', 'slug' => Permission::CAN_ORGANIZATION_ACCESS ]);
        
                    CoreRole::factory()
                            ->createOne([ 'name_en' => 'Organisation Access', 'slug' => Role::CAN_ORGANIZATION ])
                            ->permissions()
                            ->sync([ 6 ]);
                }
            }
        }
    

    /**
     * Return status for handler catchers.
     *
     * @return int
     */
    protected function getCantLoginStatus()
    {
        return 401;
    }
    

    /**
     * Test api registration validation.
     *
     * @return void
     * @throws \Throwable
     */
    public function testApiRegistrationValidation()
    {
        $notUnique = $this->getTestRegisterData(false, 5);
        $response = $this->withHeaders([ 'Authorization' => $this->getBearerClientToken() ])
                         ->json('POST', $this->getRegisterRoute(), $notUnique);

        $response->assertStatus(422);
        $errors = collect($response->decodeResponseJson()->json('errors'));
        $this->assertEquals(2, count($errors));
        $this->assertTrue($errors->contains('field', 'password'));
        $this->assertTrue($errors->contains('field', 'email'));
    }    
    

namespace App\Http\Resources;

use AttractCores\LaravelCoreKit\Http\Resources\UserResource as CoreUserResource;
use Illuminate\Support\Arr;

/**
 * Class UserResource
 *
 * @property \App\Models\User $resource
 *
 * @package App\Http\Resources\PublicResources
 * Date: 17.12.2020
 * Version: 1.0
 * Author: Yure Nery <[email protected]>
 */
class UserResource extends CoreUserResource
{

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        $parentData = Arr::except(parent::toArray($request), [ 'name' ]);

        return array_merge(
            $parentData, [
            'first_name'                  => $this->resource->first_name,
            'last_name'                   => $this->resource->last_name,
            'relations'                   => array_merge($parentData[ 'relations' ], [
                'avatar'             => $this->whenLoaded('avatar', function () {
                    return new MediaResource($this->resource->avatar);
                }),  
            ]),
        ]);
    }
}

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
    'email',
    'first_name',
    'last_name',
    'firebase_token',
];

// Replace Core User Resource
$this->app->bind(\AttractCores\LaravelCoreKit\Http\Resources\UserResource::class, \App\Http\Resources\UserResource::class);
bash
        php artisan migrate --seed
        
bash
        php artisan passport:keys --force &&
        php artisan passport:client --client --no-interaction &&
        php artisan passport:client --password --no-interaction