All Projects → Mahmoud-Italy → Larafast Fastapi

Mahmoud-Italy / Larafast Fastapi

A Fast Laravel package to help you generate CRUD API Controllers and Resources, Model.. etc

Projects that are alternatives of or similar to Larafast Fastapi

Project Name
Get the name of a project from package.json, git config, or basename of the current working directory.
Stars: ✭ 8 (-91.21%)
Mutual labels:  generate, scaffold
Create Ava
Add AVA to your project
Stars: ✭ 14 (-84.62%)
Mutual labels:  generate, scaffold
Lumen Api Demo
Lumen rest api demo with Dingo/Api, JWT, CORS, PHPUNIT
Stars: ✭ 856 (+840.66%)
Mutual labels:  laravel, lumen
Scaffold Interface
🚀 A Smart CRUD Generator For Laravel
Stars: ✭ 836 (+818.68%)
Mutual labels:  laravel, scaffold
Kickstarts
💻 No setup, just development!
Stars: ✭ 57 (-37.36%)
Mutual labels:  laravel, lumen
Laravel Bootstrap Table List
Bootstrap table list generator for Laravel.
Stars: ✭ 16 (-82.42%)
Mutual labels:  generate, laravel
Freshdesk Laravel
Freshdesk Service Provider for Laravel 5 and Lumen
Stars: ✭ 14 (-84.62%)
Mutual labels:  laravel, lumen
Laravel Responder
A Laravel Fractal package for building API responses, giving you the power of Fractal with Laravel's elegancy.
Stars: ✭ 673 (+639.56%)
Mutual labels:  laravel, lumen
Kbframe
一款基于Laravel框架开发的现代化二次开发框架,是高性能,高效率,高质量的企业级开发框架,具有驱动领域,敏捷开发,轻易上手,高内聚低耦合,开箱即用等特点。
Stars: ✭ 47 (-48.35%)
Mutual labels:  generate, laravel
Http Basic Auth Guard
HTTP Basic Auth Guard for Lumen 5.x
Stars: ✭ 39 (-57.14%)
Mutual labels:  laravel, lumen
Bugsnag Laravel
Bugsnag notifier for the Laravel PHP framework. Monitor and report Laravel errors.
Stars: ✭ 746 (+719.78%)
Mutual labels:  laravel, lumen
Laravel Sqs Fifo Queue
Adds a Laravel queue driver for Amazon SQS FIFO queues.
Stars: ✭ 75 (-17.58%)
Mutual labels:  laravel, lumen
Laravel
Laravel Model Generator
Stars: ✭ 715 (+685.71%)
Mutual labels:  laravel, scaffold
Jsonrpc
JsonRpc Server extension for Laravel/Lumen
Stars: ✭ 23 (-74.73%)
Mutual labels:  laravel, lumen
Laravel Elasticsearch
An easy way to use the official Elastic Search client in your Laravel applications.
Stars: ✭ 717 (+687.91%)
Mutual labels:  laravel, lumen
Generate Gh Repo
Generate generator to create a new repository on GitHub.
Stars: ✭ 11 (-87.91%)
Mutual labels:  generate, scaffold
Lumen Generator
A Lumen Generator You Are Missing
Stars: ✭ 578 (+535.16%)
Mutual labels:  laravel, lumen
Laravel Menu
Html menu generator for Laravel
Stars: ✭ 650 (+614.29%)
Mutual labels:  generate, laravel
Larawiz
Larawiz is a easy project scaffolder for Laravel
Stars: ✭ 28 (-69.23%)
Mutual labels:  laravel, scaffold
Laravel Health Check
A package for checking the health of your Laravel & Lumen applications
Stars: ✭ 59 (-35.16%)
Mutual labels:  laravel, lumen

Larafast FastAPI

Scrutinizer Code Quality Build Status Code Intelligence Status Total Downloads fast-api

What does mean FastAPI:

A Fastapi Laravel package to help you generate CRUD API Controllers and Resources, Model.. etc

What actually do?

Suppose you are building an api, and you want to create controller and resources and model and factory.. etc, then you have to do a ton of other tedious and to be honest, boring things like creating migrations, model factories, the controller, form validation and adding all.

So what FastAPI does is when you tell it the model name, it will do all those boring things. When it's done you have the following:

  • Blog.php
  • BlogController.php ship with code already exists
  • BlogStoreRequest.php and BlogUpdateRequest.php
  • BlogResource.php
  • Timestamped create_blogs_table.php migration file
  • BlogFactory.php

Installation

composer require larafast/fastapi

Then publish the config

php artisan vendor:publish --tag=fastApi

For Lumen

Just Add this line into bootstrap/app.php

$app->register(Larafast\Fastapi\FastapiServiceProvider::class);

Example

php artisan fastApi Blog

Once done, it will show you the details of the files generated.

Factory created successfully

Created Migration: 2020_07_14_125128_create_blogs_table

Model created successfully

Controller created successfully

Request created successfully

Request created successfully

Resource created successfully

Snapshot of BlogController

namespace App\Http\Controllers;

use App\Blog;
use Illuminate\Http\Request;
use App\Http\Requests\BlogUpdateRequest;
use App\Http\Requests\BlogStoreRequest;
use App\Http\Resources\BlogResource;

class BlogController extends Controller
{
    function __construct()
    {
        // $this->middleware('permission:view_blogs', ['only' => ['index', 'show']]);
        // $this->middleware('permission:add_blogs',  ['only' => ['store']]);
        // $this->middleware('permission:edit_blogs', ['only' => ['update']]);
        // $this->middleware('permission:delete_blogs', ['only' => ['destroy']]);
    }

    public function index()
    {
        $rows = BlogResource::collection(Blog::filter());
        return response()->json([
            'rows'        => $rows,
            'paginate'    => $this->paginate($rows)
        ], 200);
    }

    public function store(BlogStoreRequest $request)
    {
        $row = Blog::createOrUpdate(NULL, request()->all());
        if($row === true) {
            return response()->json(['message' => ''], 201);
        } else {
            return response()->json(['message' => 'Unable to create entry ' . $row], 500);
        }
    }

    public function show($id)
    {
        $row = new BlogResource(Blog::findOrFail(decrypt($id)));
        return response()->json(['row' => $row], 200);
    }

    public function update(BlogUpdateRequest $request, $id)
    {
        $row = Blog::createOrUpdate(decrypt($id), request()->all());
        if($row === true) {
            return response()->json(['message' => ''], 200);
        } else {
            return response()->json(['message' => 'Unable to update entry ' . $row], 500);
        }
    }

    public function destroy($id)
    {
        try {
            $row = Blog::query();
           
            // can work with multi select
            if(strpos($id, ',') !== false) {
                foreach(explode(',',$id) as $sid) {
                    $ids[] = $sid;
                }
                $row->whereIN('id', $ids);
            } else {
                $row->where('id', $id);
            }   
            $row->delete();

            return response()->json(['message' => ''], 200);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Unable to delete entry, '. $e->getMessage()], 500);
        }
    }
}

Snapshot of Blog Model

namespace App;

use DB;
use App\Image;
use Illuminate\Pipeline\Pipeline;
use Larafast\Fastapi\QueryFilters\Sort;
use Larafast\Fastapi\QueryFilters\Locale;
use Larafast\Fastapi\QueryFilters\Search;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;

class Blog extends Model
{
    protected $guarded = [];

    // imageable polymorphic
    public function image() {
        return $this->morphOne(Image::class, 'imageable');
    }
    
    // Filter
    public static function filter()
    {
        return app(Pipeline::class)
                ->send(self::query())
                ->through(
                [
                    Locale::class,
                    Search::class,
                    Sort::class
                ])
                ->thenReturn()
                ->paginate(request('paginate') ?? 10);
    }
    

    // Create or Update
    public static function createOrUpdate($id, $value)
    {
        try {

            // Begin Transaction
            DB::beginTransaction();

                // find Or New
                $row              = (isset($id)) ? self::find($id) : new self;
                $row->title       = $value['title'] ?? NULL;
                $row->body        = $value['body'] ?? NULL;
                $row->save();

                // Image
                if(isset($value['image'])) {
                    $row->image()->delete();
                    if($value['image']) {
                        if(!Str::contains($value['image'], [ Imageable::contains() ])) {
                            $image = Image::uploadImage($value['image']);
                        } else {
                            $image = explode('/', $value['image']);
                            $image = end($image);
                        }
                        $row->image()->create([ 'url' => $image ]);
                    }
                }


            DB::commit();
            // End Commit of Transaction

            return true;
        } catch (\Exception $e) {
            DB::rollback();
            return $e->getMessage();
        }
    }
}

Snapshot of Blog Resource

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class BlogResource extends JsonResource
{

    public function toArray($request)
    {
        return [
            'id'            => $this->id,
            'encrypt_id'    => encrypt($this->id),
            'image'         => ($this->image) ? $this->image->url : NULL,

            'title'         => $this->title,
            'body'          => $this->body,

            'dateForHumans' => $this->created_at->diffForHumans(),
            'timestamp'     => $this->created_at
        ];
    }
}

Now add the necessary fields and run

php artisan migrate

And that saved you an hour worth of repetitive and boring work which you can spend on more important development challenges.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].