透過CMD新增File

用命令提示字元超快

基本的新增

以下是不需要任何設定就可以直接新增檔案的方式

先叫出命令提示字元,直接來到你的專案的資料夾,按下ctrl+shift然後右鍵點專案的資料夾,就會多出幾個選項,選擇其中一個"在此處開啟命令視窗"然後key入以下的程式碼即可:

php artisan make:controller 名稱 --resource

然後把C換成M就可以長好你要的檔案了,都會幫你歸類清楚喔,超實用。

特殊的新增

以下是需要做相關的設定才可以使用cmd新增

將以下的程式碼新增到/App/Repositories命名為BaseRepository.php

<?php
namespace App\Repositories;
use Illuminate\Database\Eloquent\Model;
class BaseRepository
{
    /**
     * The Model name.
     *
     * @var \Illuminate\Database\Eloquent\Model;
     */
    protected $model;
    /**
     * Paginate the given query.
     *
     * @param The number of models to return for pagination $n integer
     *
     * @return mixed
     */
    public function getPaginate($n)
    {
        return $this->model->paginate($n);
    }
    /**
     * Create a new model and return the instance.
     *
     * @param array $inputs
     *
     * @return Model instance
     */
    public function store(array $inputs)
    {
        return $this->model->create($inputs);
    }
    /**
     * FindOrFail Model and return the instance.
     *
     * @param int $id
     *
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection
     *
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function getById($id)
    {
        return $this->model->findOrFail($id);
    }
    /**
     * Update the model in the database.
     *
     * @param $id
     * @param array $inputs
     */
    public function update($id, array $inputs)
    {
        $this->getById($id)->update($inputs);
    }
    /**
     * Delete the model from the database.
     *
     * @param int $id
     *
     * @throws \Exception
     */
    public function destroy($id)
    {
        $this->getById($id)->delete();
    }
}

將以下程式碼新增到/App/Console/Commands/stubs命名為repository.stub

<?php

namespace DummyNamespace;

use Illuminate\Database\Eloquent\Model;

class DummyClass extends BaseRepository
{
    //
}

將以下程式碼新增到/App/Console/Commands命名為MakeRepositoryCommand.php

<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class MakeRepositoryCommand extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:repository {name}';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new repository class';
    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Repository';
    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return __DIR__.'/stubs/repository.stub';
    }
    /**
     * Get the default namespace for the class.
     *
     * @param string $rootNamespace
     *
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Repositories';
    }
}

/App/Console/Kernel.php新增以下字串

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\MakeRepositoryCommand::class,   // 新增這一串!!!!!!!
    ];

這樣就可以做Repositories的新增了。

php artisan make:repository 檔案名稱

Last updated