PHP code example of miladshm / controller-helpers

1. Go to this page and download the library: Download miladshm/controller-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/ */

    

miladshm / controller-helpers example snippets


use Miladshm\ControllerHelpers\Http\Traits\HasApiDatatable;

class TestController extends Controller
{
    use HasApiDatatable;
    
     /**
     * Specifying the model class to use
     * 
     * @return Model
     */
    private function model(): Model
    {
        return new TestModel;
    }
    
    /**
     * Specifying extra data to send with index response
     * 
     * @param Model|null $item
     * @return array|null
     */
    private function extraData(Model $item = null): ?array
    {
        return [];
    }

    /**
     * Specifying Model relations to load with data
     * @return array
     */
    private function relations(): array
    {
        return [];
    }
}


use Miladshm\ControllerHelpers\Http\Traits\HasApiDatatable;

class TestController extends Controller
{
    use HasApiDatatable;
    
    ...
    
    /**
    * @param Builder $builder
    * @return Builder|null
    */
    protected function filters(Builder $builder): null|Builder
    {
        return $builder
                    ->when('some_condition',
                        fn(Builder $builder) => $builder->where('field', 'value'))
                    ->when(\request()->filled('status'), function (Builder $builder) {
                        $builder->where('status', \request()->input('status'));
                    })
                    ->where('field2', 'value2'));
    }
    
    ...
}


use Miladshm\ControllerHelpers\Http\Traits\HasApiDatatable;

class TestController extends Controller
{
    use HasApiDatatable;
    
    /**
     * Order can be either 'asc' or 'desc', default value in 'desc'
     * @return string
     */
    protected function getOrder(): string
    {
        return 'desc'; // value can be 'desc' or 'asc'
    } 
    
    ...
}


use Miladshm\ControllerHelpers\Http\Traits\HasApiDatatable;

class TestController extends Controller
{
    use HasApiDatatable;
    
    /**
     * @return int
     */
    protected function getPageLength(): int
    {
        return 15;
    }
    
    ...
}


use Miladshm\ControllerHelpers\Http\Traits\HasApiDatatable;

class TestController extends Controller
{
    use HasApiDatatable;
    
    /**
     * @return array|string[]
     */
    public function getColumns(): array
    {
        return ['id', 'code', 'name'];
    }
    
    ...
}


use Miladshm\ControllerHelpers\Http\Traits\HasApiDatatable;

class TestController extends Controller
{
    use HasApiDatatable;
    
    /**
     * @return array
     */
    protected function getSearchable(): array
    {
        return ['id', 'code', 'name', 'relation.column']; // You can specify relation columns to search within
    }
    ...
}


use Miladshm\ControllerHelpers\Http\Traits\HasApiDatatable;

class TestController extends Controller
{
    use HasApiDatatable;
    
    protected function getPaginator(): ?string
    {
        return 'simple'; // value can be [default,simple,cursor]
    }
    
    ...
}

use Miladshm\ControllerHelpers\Http\Traits\HasStore;

class YourController extends Controller
{
    use HasStore;

    // Your other methods and properties
}

use App\Models\YourModel;

class YourController extends Controller
{
use HasStore;

    protected function model(): string
    {
        return new YourModel();
    }

    // Your other methods and properties
}

use App\Models\YourModel;

class YourController extends Controller
{
use HasStore;

    protected function model(): string
    {
        return new YourModel();
    }

    protected function storeCallback(Request $request, Model $item): void
    {
        // Perform additional actions here
    }

    // Your other methods and properties
}

use App\Models\YourModel;

class YourController extends Controller
{
use HasStore;

    protected function model(): string
    {
        return new YourModel();
    }

    protected function prepareForStore(Request &$request)
    {
        // Perform any necessary preparations here
    }

    // Your other methods and properties
}

use App\Models\YourModel;

class YourController extends Controller
{
use HasStore;

    protected function model(): string
    {
        return new YourModel();
    }

    /**
     * @return FormRequest
     */
    private function requestClass(): FormRequest
    {
        return new StoreRequest;
    }

    // Your other methods and properties
}

use App\Models\YourModel;
use App\Http\Resources\YourModelResource;

class YourController extends Controller
{
    use HasStore;

    protected function model(): string
    {
        return new YourModel();
    }
    
        /**
     * @return JsonResource|class-string|null
     */
    public function getJsonResourceClass(): JsonResource|string|null
    {
        return YourModelResource::class;
    }
    // Your other methods and properties
}

use Miladshm\ControllerHelpers\Http\Traits\HasUpdate;

class YourController extends Controller
{
    use HasUpdate;

    // Your other methods and properties
}

use App\Models\YourModel;

class YourController extends Controller
{
    use HasUpdate;

    protected function model(): string
    {
        return new YourModel();
    }

    // Your other methods and properties
}

use App\Models\YourModel;

class YourController extends Controller
{
    use HasUpdate;

    protected function model(): string
    {
        return new YourModel();
    }

    protected function prepareForUpdate(Request &$request): void
    {
        // Perform any necessary preparations here
        // Modify the request object as necessary
    }

    // Your other methods and properties
}

use App\Models\YourModel;

class YourController extends Controller
{
    use HasUpdate;

    protected function model(): string
    {
        return new YourModel();
    }

    protected function updateCallback(Request $request, Model $item): void
    {
        // Perform any additional actions here
    }

    // Your other methods and properties
}

use App\Models\YourModel;
use App\Http\Requests\YourModelUpdateRequest;

class YourController extends Controller
{
    use HasUpdate;

    protected function model(): string
    {
        return new YourModel();
    }

    protected function updateRequestClass(): ?FormRequest
    {
        return new YourModelUpdateRequest();
    }

    // Your other methods and properties
}

use Miladshm\ControllerHelpers\Http\Traits\HasChangePosition;
class YourController extends Controller
{
use HasChangePosition;

    // Your other methods and properties
}

use App\Models\YourModel;

class YourController extends Controller
{
    use HasChangePosition;

    protected function model()
    {
        return new YourModel();
    }

    // Your other methods and properties
}

use App\Models\YourModel;

class YourController extends Controller
{
    use HasChangePosition;

    protected function model()
    {
        return YourModel::class;
    }

    protected function filters($query)
    {
        return $query->where('status', 'active');
    }

    // Your other methods and properties
}

use App\Models\YourModel;

class YourController extends Controller
{
    use HasChangePosition;

    protected function model()
    {
        return YourModel::class;
    }

    protected function getPositionColumn()
    {
        return 'your_position_column';
    }

    // Your other methods and properties
}
shell
  php artisan vendor:publish --tag controller-helpers-config