##前提、実現したいこと
<環境>
OS : macOS Big Sur 11.4
MAMP
Laravel : 6.2
<実現したい事>
produceers(生産者)とproducts(商品)というテーブルを作成したが、リレーションしたい
##考えた事
Product.phpに問題があると思ったが、どこが間違っているかわかりません。
##該当のソースコード
Product.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6use Illuminate\Database\Eloquent\Relations\BelongsTo; 7 8class Product extends Model 9{ 10 public function producer(): BelongsTo 11 { 12 return $this->belongsTo('App\Producer'); 13 } 14} 15
Producer.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Contracts\Auth\MustVerifyEmail; 6use Illuminate\Foundation\Auth\User as Authenticatable; 7use Illuminate\Notifications\Notifiable; 8 9class Producer extends Authenticatable 10{ 11 use Notifiable; 12 13 protected $guard = 'producer'; 14 /** 15 * The attributes that are mass assignable. 16 * 17 * @var array 18 */ 19 protected $fillable = [ 20 'name', 'email', 'password', 21 ]; 22 23 /** 24 * The attributes that should be hidden for arrays. 25 * 26 * @var array 27 */ 28 protected $hidden = [ 29 'password', 'remember_token', 30 ]; 31 32 /** 33 * The attributes that should be cast to native types. 34 * 35 * @var array 36 */ 37 protected $casts = [ 38 'email_verified_at' => 'datetime', 39 ]; 40} 41
create_products_table.php
php
1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateProductsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('products', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->bigInteger('producer_id'); 19 $table->string('name'); 20 $table->string('image'); 21 $table->text('introduction'); 22 $table->timestamps(); 23 }); 24 } 25 26 /** 27 * Reverse the migrations. 28 * 29 * @return void 30 */ 31 public function down() 32 { 33 Schema::dropIfExists('products'); 34 } 35} 36
create_producers_table
php
1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateProducersTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('producers', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->string('name'); 19 $table->string('email')->unique(); 20 $table->string('password')->unique(); 21 $table->text('introduction'); 22 $table->string('image')->nullable(); 23 $table->text('address'); 24 $table->timestamps(); 25 }); 26 } 27 28 /** 29 * Reverse the migrations. 30 * 31 * @return void 32 */ 33 public function down() 34 { 35 Schema::dropIfExists('producers'); 36 } 37} 38
'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'vege-cone'), 'username' => env('DB_USERNAME', 'yuma'), 'password' => env('DB_PASSWORD', '****'), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ],
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=vege-cone DB_USERNAME=yuma DB_PASSWORD=*****
接続設定が合ってないとか、DBがRunになってないとかはないですか?
返信ありがとうございます。
今、本文にdatabase.phpと.envを追記しましたが、おかしな点ありますか?
ちなみに試した事
①USERNAMEをroot、PASSWORDを空欄→エラー変わらない
②DBPORT8889→エラー変わらない
③php artisan config:cache→エラー変わらない
④mysql.server start→エラー変わらない
ちなみにMySQLでMAMPを使用しています。
同じ定義値で直にコマンドで接続できますか?
こういう事でしょうか?
mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.25 Homebrew
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>