PHP code example of innoflash / larastart

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

    

innoflash / larastart example snippets




use InnoFlash\LaraStart\Traits\APIResponses;

class ClassName
{
    use APIResponses;



namespace App\Http\Controllers;

use InnoFlash\LaraStart\Traits\APIResponses;
...

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    use APIResponses;
}

$this->successMessege('{optional :message here}')



namespace App\Exceptions;

use Exception;
use InnoFlash\LaraStart\Traits\ExceptionsTrait;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    use ExceptionsTrait;

    public function render($request, Exception $exception)
    {
        return $this->apiExceptions($request, $exception);
    }

return $this->apiExceptions($request, $exception, true); //true triggers the default error

use InnoFlash\LaraStart\Services\AuthService;

class AuthController extends Controller
{
    private $authService;
    public function __construct(AuthService $authService)
    {
        $this->authService = $authService;
    }

    function login(LoginRequest $request)
    {
        return $this->authService->attemptLogin();
    }

function login(LoginRequest $request)
    {
        return $this->authService->attemptLogin([], 'your-guard);
    }

php artisan vendor:publish --tag=larastart-config

return [
    /**
     * Replace this login resource with your own if you have it
     */
    'resource' => \InnoFlash\LaraStart\Http\Resources\User::class,

    /**
     * Sets the default limit for paginated items
     */
    'limit' => 15,

    /**
     * Sets the default guard you want to use in the auth service
     */
    'guard' => 'api',

    /**
     * Sets whether or not all CRUD ops should return json or object.
     */
    'return_object'       => false,
];

class ModelController extends Controller
{
    private $modelService;
    function __construct(ModelService $modelService)
    {
        $this->modelService = $modelService;
    }

//your app/Providers/AppServiceProvider.php

public function register()
{
    $this->app->singleton(ModelService::class);
}

    /**
     * How to get the selected model
     */
    return $this->modelService->getModel();

    /**
     * Query for multiple results
     *
     * You are still open to run default model query like
     */
    return Model::all();

public function delete(DeleteRequest $request)
{
    /**
     * Since the service is injected into the controller it
     * knows exactly what model to target so calling destroy
     * automates a precise delete
     *
     * You can pass a custom delete message or leave as is
     */
    return $this->modelService->destroy('custom delete message');
}

public function update(UpdateRequest $request)
{
    /**
     * You just have to sent the right model id and the
     * service finds the model and updates the model
     *
     * $params is highly likely to be the validated input
     *
     * You can override the update success message by adding
     * a message param as a second param
     * return {
     *              success: true,
     *              message: 'model updated'
     *        }
     */
    return $this->modelService->update($params);
}

public function create(CreateRequest $request)
{
    /**
     * the create method takes an array for params so we are just gonna
     * set the validated values from our request
     *
     * This return the a default response
     * {
     *      success: true,
     *      message: "model created"
     * }
     */
    return $this->modelService->create($request->validated());

    /**
     * You can override the output message by passing a second parameter
     */
    return $this->modelService->create($request->validated(), 'custom message');

    /**
     * If you want to get the created object you must add another boolean param and set it to true to get the object
     * This returns the object as is
     */
        return $this->modelService->create($request->validated(), 'custom message', true);
}

/**
     * Attaches a parent to the current model
     */
    function getParentRelationship()
    {
        /**
         * For example you want to create a comment for a post
         * You will need to initialize the post($parentObject)
         *  and call the relationship it has to the current model(relationshipToModel)
         */
        return [
            ParentModel::class, //parent model of the model to be created
            'relationshipName', //relationship name in parent model
            'foreign_key', //OPTIONAL fK in new model or default is used
        ];

        /**
         * Mainly i have been using to attach a relationship to the
         * authenticated user
         * If you already have the parent object call the relationship
         * for this as shown in the example below
         */
        return auth('guard')->user()->relationshipToModel();
    }

    public function create(CreateRequest $request)
    {
        //where $params is the validated input
        return $this->taskService->createFromParent($params, 'Task created!');
    }
sh
php artisan jwt:secret
larastart.php
sh
php artisan make:service {ServiceName} --model={ModelName}