yuktkhs
@yuktkhs (yuki takahashi)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

herokuにデプロイ後500エラー(laravel)

解決したいこと

ユーザー登録画面herokuにデプロイ後、ローカル環境では問題なかったのですが、500エラーが発生し、
解決できないため、お力添えをいただきたいです。

やろうとしていること

topページにはアクセス可能。
ユーザー登録画面にて登録ボタンを押すとエラーが出た。
image.png

エラー画面

image.png

エラーログ

image.png

[previous exception] [object] (PDOException(code: 23502): SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"profile_image\" of relation \"users\" violates not-null constraint
2021-05-01T01:01:06.966687+00:00 app[web.1]: DETAIL:  Failing row contains (43, test,test@icloud.com, null, $2y$10$lWtsK/2LzLNK0cg1FqBjaOiPydae9pHexhgi4xGPJw79PxnaWsySi, test, null, null, null, 2021-05-01 10:01:06, 2021-05-01 10:01:06). at /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:336)

和訳

null値がnull以外の制約に違反しています

該当するソースコードとDB

userテーブル

image.png

User.php

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','user_name','profile_image',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function posts()
    {
        return $this->hasMany(Post::class);
    }
    // いいね機能用
    public function favorites()
    {
        return $this->belongsToMany(Post::class)->withTimestamps();
    }
}

マイグレーションファイル

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Http\Controllers\Auth\Storage;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('user_name')->nullable();
            $table->integer('post_id')->nullable();
            $table->string('profile_image')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });

    }

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

registerController

<?php

namespace App\Http\Controllers\Auth;
use App\Models\User;
use Storage; 
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;

use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {

        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'user_name' => $data['user_name'],
            'profile_image' => null,
        ]);

        $path = Storage::disk('s3')->putFile('/',$data['profile_image'],'public');
        $user->profile_image = Storage::disk('s3')->url($path);
        $user->save();
        // dd($user);
        return $user;
    }
}

自分で試したこと

以下のサイトを参考にデータの型を見直すなどしました。
https://cloud6.net/so/sql/292020

0

2Answer

userテーブルの情報はherokuから参照しているデータベースのものですか?
ローカル環境のデータベースより古い状態、ということはないでしょうか?

0Like

ローカル環境のデータベースを更新したらできました。
ありがとうございます

0Like

Your answer might help someone💌