Last Updated: January 25, 2021
·
882
· kalinin84

Laravel 7

<?php

use Illuminate\Support\Facades\Route;

Route::get('/documents/{id}', 'DocumentController@show');
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repositories\DocumentRepository;

class DocumentController extends Controller
{
    /**
     * The document repository implementation.
     *
     * @var DocumentRepository
     */
    protected $documents;

    /**
     * Create a new controller instance.
     *
     * @param DocumentRepository $documents
     * @return void
     */
    public function __construct(DocumentRepository $documents)
    {
        $this->documents = $documents;
    }

    /**
     * @param int $id
     * @return string
     */
    public function show($id)
    {
        $document = $this->documents->find($id);

        return response()->json($document);
    }
}
<?php

namespace App\Repositories;

use App\Document;

class DocumentRepository 
{
    /**
     * @param int $id
     * @return Document
     */
    public function find($id)
    {
        return Document::find($id);
    }
}
<?php

namespace App\Console\Commands;

use Faker;
use App\Document;
use Illuminate\Console\Command;

class ImportDocuments extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:documents';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import documents';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $faker = Faker\Factory::create();

        for($i = 0; $i < 1000; $i++) {
            $document = new Document();
            $document->content = $faker->text;
            $document->target = rand(0, 1);
            $document->save();
        }
    }
}
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDocumentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('documents', function (Blueprint $table) {
            $table->id();
            $table->text('content');
            $table->unsignedTinyInteger('target');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('documents');
    }
}