Git Product home page Git Product logo

crud-generator's Introduction

CRUD Generator

A simple crud generator for Laravel Framework

Installation

You can install the package via composer:

composer require ion/crud-generator

Usage

You can create :

  • Migration with its columns.
  • Model with fillable attributes.
  • RequestForm with validation.
  • Resource with a transformation layer.
  • Api resource routes.
  • Controller CRUD.

Via this command

php artisan make:crud example --f="title:string, is_visible:boolean"

the id and timestaps will be generated in migraion file by default.

for other types of columns you can find them in Laravel Docs

The Generation Resualt

  • Migration
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('examples', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->boolean('is_visible');
            $table->timestamps();
        });
    }
  • Model
    class Example extends Model
    {
        use HasFactory;

        /**
        * The attributes that are mass assignable.
        *
        * @var array
        */
        protected $fillable = [
            'title', ' is_visible'
        ];
    } 
  • Request
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => ['required',],
            'is_visible' => ['required',]
        ];
    }
  • Resource
    class ExampleResource extends JsonResource
    {
        /**
        * Transform the resource into an array.
        *
        * @param  \Illuminate\Http\Request  $request
        * @return array
        */
        public function toArray($request)
        {
            return [
                'title' => $this->title,
                'is_visible' => $this->is_visible
            ];
        }
    }
  • Controller
class ExampleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \App\Http\Resources\ExampleResource
     */
    public function index()
    {
        return ExampleResource::collection(Example::paginate(10));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\ExampleRequest  $request
     * @return \App\Http\Resources\ExampleResource
     */
    public function store(ExampleRequest $request)
    {
        Example::create($request->all());
        return response()->json([
            'message' => 'row created'
        ],201);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \App\Http\Resources\ExampleResource
     */
    public function show($id)
    {
        return new ExampleResource(Example::findOrFail($id));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\ExampleRequest  $request
     * @param  int  $id
     * @return \App\Http\Resources\ExampleResource
     */
    public function update(ExampleRequest $request, $id)
    {
        $example = Example::findOrFail($id);
        $example->update($request->all());
        return new ExampleResource($example);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $example = Example::findOrFail($id);
        $example->delete();
         return response()->json([
            'message' => 'row deleted'
        ],201);
    }
}

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

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

crud-generator's People

Contributors

mohammedalkutrani avatar ridaomar avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.