20210715のlaravelに関する記事は4件です。

複数の値を一度に同じテーブルに入れる 【Laravel】

複数の値を一度に同じテーブルに入れる【Laravel】 アンケートなどを作成するとき、一つの質問に対して、答えを複数用意したい。 そんな時のためのメモ。 環境:Laravel 8.50.0 まずはQuestionモデルとテーブル、Answerモデルとテーブルを作成。 Questionモデル namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Question extends Model { use HasFactory; protected $guarded = []; public function answers() { return $this->hasMany(Answer::class); } } questionsテーブル public function up() { Schema::create('questions', function (Blueprint $table) { $table->id(); $table->string('question'); $table->timestamps(); }); } Answer モデル <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Answer extends Model { use HasFactory; protected $guarded = []; public function question() { return $this->belongsTo(Question::class); } } answersテーブル public function up() { Schema::create('answers', function (Blueprint $table) { $table->id(); $table->foreignId('question_id')->constrained()->cascadeOnDelete(); $table->string('answer'); $table->timestamps(); }); } web.php Route::get('/questionnaires/{questionnaire}/question/create', [QuestionController::class, 'create'])->name('question.create'); Route::post('/questionnaires/{questionnaire}/questions', [QuestionController::class, 'store'])->name('question.store'); Route::get('/questionnaires/{questionnaire}', [QuestionnaireController::class, 'show'])->name('questionnaire.show'); question/create.blade @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Create New Question</div> <div class="card-body"> <form action="{{ route('question.store', $questionnaire->id) }}" method="post"> @csrf <div class="form-group"> <label for="question">Question</label> <input name="question[question]" type="text" class="form-control" value="{{ old('question.question') }}" id="question" aria-describedby="questionHelp" placeholder="Enter Question"> <small id="questionHelp" class="form-text text-muted">Ask simple and to-the-point questions for best results.</small> @error('question.question') <small class="text-danger">{{ $message }}</small> @enderror </div> <div class="form-group"> <fieldset> <legend>Choices</legend> <small id="choicesHelp" class="form-text text-muted">Give choices that give you the most insight into your question.</small> <div> <div class="form-group"> <label for="answer1">Choice 1</label> <input name="answers[][answer]" type="text" value="{{ old('answers.0.answer') }}" class="form-control" id="answer1" aria-describedby="choicesHelp" placeholder="Enter Choice 1"> @error('answers.0.answer') <small class="text-danger">{{ $message }}</small> @enderror </div> </div> <div> <div class="form-group"> <label for="answer2">Choice 2</label> <input name="answers[][answer]" type="text" value="{{ old('answers.1.answer') }}" class="form-control" id="answer2" aria-describedby="choicesHelp" placeholder="Enter Choice 2"> @error('answers.1.answer') <small class="text-danger">{{ $message }}</small> @enderror </div> </div> <div> <div class="form-group"> <label for="answer3">Choice 3</label> <input name="answers[][answer]" type="text" value="{{ old('answers.2.answer') }}" class="form-control" id="answer3" aria-describedby="choicesHelp" placeholder="Enter Choice 3"> @error('answers.2.answer') <small class="text-danger">{{ $message }}</small> @enderror </div> </div> <div> <div class="form-group"> <label for="answer4">Choice 4</label> <input name="answers[][answer]" type="text" value="{{ old('answers.3.answer') }}" class="form-control" id="answer4" aria-describedby="choicesHelp" placeholder="Enter Choice 4"> @error('answers.3.answer') <small class="text-danger">{{ $message }}</small> @enderror </div> </div> </fieldset> </div> <button type="submit" class="btn btn-primary">Add Question</button> </form> </div> </div> </div> </div> </div> @endsection 細かく見てみます。 question <div class="form-group"> <label for="question">Question</label> //'question.question'で受け取る <input name="question[question]" type="text" class="form-control" value="{{ old('question.question') }}" id="question" aria-describedby="questionHelp" placeholder="Enter Question"> <small id="questionHelp" class="form-text text-muted">Ask simple and to-the-point questions for best results.</small> @error('question.question') <small class="text-danger">{{ $message }}</small> @enderror </div> answer1 <div> <div class="form-group"> <label for="answer1">Choice 1</label> //answeresを配列で渡した1番目のanswer。answerがkey。 <input name="answers[][answer]" type="text" //1番目なので0を指定 value="{{ old('answers.0.answer') }}" class="form-control" id="answer1" aria-describedby="choicesHelp" placeholder="Enter Choice 1"> @error('answers.0.answer') <small class="text-danger">{{ $message }}</small> @enderror </div> </div> answer2,3,4も同じ。 QuestionController class QuestionController extends Controller { public function create(Questionnaire $questionnaire) { return view('question.create',compact('questionnaire')); } public function store(Questionnaire $questionnaire) { //Validationして$dataに入れる。 $data = request()->validate([ //question['question'] 'question.question' =>' required', //arrayを入力するのでワイルドカード 'answers.*.answer' =>' required', ]); //$data['question']を作成 $question = $questionnaire->questions()->create($data['question']); //$data['answers']を作成 answersは複数あるのでcreateMany $question->answers()->createMany($data['answers']); return redirect()->route('questionnaire.show',[$questionnaire->id]); } } QuestionnaireController public function show(Questionnaire $questionnaire) { //lazy loading.questionsと一緒にquestionsとリレーションを貼ったanswersを呼び出す。取得できる結果はwith(Eager)と同じ $questionnaire->load('questions.answers'); return view('questionnaire.show',compact('questionnaire')); } questionnaire/show.blade //受け取った変数を$questionとしてループ @foreach ($questionnaire->questions as $question) <div class="card mt-4"> <div class="card-header">{{ $question->question }}</div> <div class="card-body"> <ul class="list-group"> //$questionとのリレーション$answersを$answerとしてループ @foreach ($question->answers as $answer) <li class="list-group-item">{{ $answer->answer }}</li> @endforeach </ul> </div> </div> @endforeach Question Answer1 Answer2 Answer3 Answer4 と出力されます。 参考 https://youtu.be/_SyG3HMv48k
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Laravel】DBから取得したCollection型の空判定

はじめに PHP、Laravel初学者である私だが、他の方のコードをレビューする機会があった。 そこで、Collection型と配列で空のチェックが異なることを知ったため、ここに記載する。 結論 Collection型と配列の空を判定する方法は以下の通り。 ■ Collectionの空チェック $collection = collect([]); $collection->isEmpty(); // true ■ 配列の空チェック $array = []; empty($array); // true 詳細 Collection型は、LaravelにあるObject型の一種であり、配列のラッパーである。例えば、DBから値を複数取得した結果(クエリビルダ等を用いた際)の型がCollectionである。また、Collection型にはメソッドが存在しており、今回は、Collectionが持っているメソッドを用いて空かどうかを判定している。 ■ 備考 因みに、今回はisEmpty()を紹介したが、isNotEmpty()も存在する。 他にも、Collectionのメソッドは複数あるため、興味があれば以下公式ページを参照して欲しい。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Laravelのテスト用接続先データベースを切り替える

経緯 TDDを使ってLaraveで開発する。 開発環境でちょくちょくテストを動かすことになるが、 テスト実行しているとデータベースが汚れてしまう。(*1) このためテストだけのデータベースを別にしたい。 設定 データベースを2つ用意 ひとつは mydev もうひとつは mydev_testing ユニットテストでは mydev_testing データベースを使う。 やりかた .envを2つ作る方式でやる 通常の開発用の設定は .envファイルに記述。 ユニットテストでは .env.oreore_testing ファイルに記述。 Laravelのプロジェクトルートにある .envをコピーして .env.oreore_testing ファイルを作る。 .env.oreore_testingのデータベース接続先を mydev_testing に変更する。 phpunit.xmlを変更 APP_ENVのところにテスト設定ファイルの後ろの部分を記述。 こんな感じ <php> ・・・中略・・・ <server name="APP_ENV" value="oreore_testing"/> ・・・中略・・・ </php> 注 *1 use DatabaseTransaction;を使うとテストが終わった時にrollbackするので、データベース汚れないですが、 テスト実行終了時にどんなデータが残ってたか確認できないと開発やりにくいから、use DatabaseTransaction;は使わない。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

PHP Laravel Artisanコマンドのメモ

はじめに Laravelの開発で色々なArtisanコマンドがあるため、よく使用するものを、 ここにメモとして残して、追加で必要なものは更新していこうと思う Artisanコマンドとは PHPのフレームワークであるLaravelのコマンドのこと Artisanコマンドを使用することでModelやControllerを作成することができる Artisanでのコマンドリスト ターミナル php artisan list Artisanでのコマンドのリストをターミナルに表示できる サーバの起動コマンド cd コマンドでLaravelのプロジェクトがあるとこまで移動しておくこと ターミナル php artisan serve 記載されているURL:http://〇〇〇.〇〇〇.〇〇〇.〇〇〇:8000 このURLにアクセスすればサーバーが立ち上がっているか確認できる 画像コマンドのミスがありますが見逃してください・・・ tinker起動 Laravelの機能を試せるREPLが起動します。 ターミナル php artisan tinker tinker終了コマンド ターミナル >>> exit Exit: Goodbye マイグレーション マイグレーションファイルの作成 マイグレーションファイルとは、Laravelからデータベースのテーブルを管理 するためのファイルのこと ターミナル php artisan make:migration create_messages_table --create=messages php artisan make:migration //マイグレーションファイル作成のためのコマンド create_messages_table //マイグレーションファイルの名前 名前を見て何をするかわかる名前にする --create=messages //テーブル名を決定 --create=messages ← すべて小文字で複数形 マイグレーションのロールバックのコマンド マイグレーションファイルをある地点まで戻すコマンド ターミナル php artisan migrate:rollback マイグレーションの実行 ターミナル php artisan migrate マイグレーションの実行状況の確認 ターミナル php artisan migrate:status モデルの作成 Modelはapp/ 直下に生成、配置される ターミナル php artisan make:model モデル名 ← 頭が大文字で単数形であること! Controllerの生成 ターミナル php artisan make:controller コントローラ名 RESTful Resource Controllerの生成 例文のルーティング routes/web.php <?php Route::resource('messages', 'MessagesController'); この時のコントローラの生成コマンド Routerに対応したController名をつけないとルーティングされないので注意 ターミナル php artisan make:controller MessagesController --resource 参考にさせていただいたサイト 【Laravel】Artisanコマンド基礎
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む