LoginSignup
51
39

More than 5 years have passed since last update.

【Laravel】モデルのディレクトリ構成変更についてのメモ

Last updated at Posted at 2019-01-19

プロジェクト作成時にUserモデルがapp/に作成されていますが、
Userモデルをapp/Models/に移す際の手順をメモ:notepad_spiral:

app/Modelsフォルダの作成

mkdir app/Models

User.phpの移動

User.php移動後は移動したディレクトリに応じてUser.phpの名前空間を修正

<?php

namespace App\Models; // ←名前空間を修正

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;

その他修正

修正が必要なファイルは以下

  • /app/Http/Controllers/Auth/RegisterController.php
  • /config/auth.php
  • /config/services.php
  • /database/factories/UserFactory.php

RegisterController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Models\User; // ←Userのパスを修正
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

auth.php

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class, // ←パスを修正
        ],

services.php

    'stripe' => [
        'model' => App\Models\User::class, // ←パスを修正
        'key' => env('STRIPE_KEY'),
        'secret' => env('STRIPE_SECRET'),
        'webhook' => [
            'secret' => env('STRIPE_WEBHOOK_SECRET'),
            'tolerance' => env('STRIPE_WEBHOOK_TOLERANCE', 300),
        ],
    ],

UserFactory.php

$factory->define(App\Models\User::class, function (Faker $faker) { // ←ここ
    return [

オートロードの参照先

composer.jsonで制御しているので修正

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Models\\": "app/Models/" // ←ここ
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },

以上です。

51
39
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51
39