質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

1回答

776閲覧

DBとの接続ができない

yuyamaa

総合スコア3

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2021/06/21 12:00

編集2021/06/21 13:03

##前提、実現したいこと

<環境>
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=*****

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2021/06/21 12:04

接続設定が合ってないとか、DBがRunになってないとかはないですか?
yuyamaa

2021/06/21 13:11

返信ありがとうございます。 今、本文にdatabase.phpと.envを追記しましたが、おかしな点ありますか? ちなみに試した事 ①USERNAMEをroot、PASSWORDを空欄→エラー変わらない ②DBPORT8889→エラー変わらない ③php artisan config:cache→エラー変わらない ④mysql.server start→エラー変わらない ちなみにMySQLでMAMPを使用しています。
m.ts10806

2021/06/21 13:12

同じ定義値で直にコマンドで接続できますか?
yuyamaa

2021/06/21 13:23

こういう事でしょうか? 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>
guest

回答1

0

connection refusedなら、モデルやコントローラー は関係ありません。
.envおよびconfig/datanase.phpファイルの設定の問題
またはconfigがキャッシュされてしまっているか、です

投稿2021/06/21 12:05

mikkame

総合スコア5036

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuyamaa

2021/06/21 13:10

返信ありがとうございます。 今、本文にdatabase.phpと.envを追記しましたが、おかしな点ありますか? ちなみに試した事 ①USERNAMEをroot、PASSWORDを空欄→エラー変わらない ②DBPORT8889→エラー変わらない ③php artisan config:cache→エラー変わらない ④mysql.server start→エラー変わらない ちなみにMySQLでMAMPを使用しています。
yuyamaa

2021/06/22 10:07

以下のようなエラーが出ました ERROR 1045 (28000): Access denied for user 'yamaguchiyuma'@'localhost' (using password: NO)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問