LoginSignup
0
0

More than 3 years have passed since last update.

【Laravel基礎】DBにデータを保存・保存内容を表示

Last updated at Posted at 2020-06-28

LaravelでDBにデータを保存する方法と、保存した内容を表示する方法のメモ。

前提

resourceでControllerを作っています。

注意

コード部分の「****」「&&&&」みたいなのは代替文字です。

DBに保存

Controller,Route,Viewのそれぞれに書いていきます。

Route

web.php
Route::group(
    ['prefix' => 'contacts', 'middleware' => 'auth'],
    function () {
        Route::get('index', 'ContactFormController@index')->name('contacts.index');
        Route::get('create', 'ContactFormController@create')->name('contacts.create');
        Route::post('store', 'ContactFormController@store')->name('contacts.store');
        Route::get('show/{id}', 'ContactFormController@show')->name('contacts.show');
        Route::get('edit/{id}', 'ContactFormController@edit')->name('contacts.edit');
        Route::post('update/{id}', 'ContactFormController@update')->name('contacts.update');
        Route::post('destroy/{id}', 'ContactFormController@destroy')->name('contacts.destroy');
    }
);

postでviewで入力されたパラメータを受け取る。

View

create.blade.php
<form method="POST" action="{{route('contacts.store')}}">
    @csrf
    <input class="input-group" type="text" name="name" placeholder="Name">
    <input class="input-group" type="text" name="email" placeholder="Mail">
    男性:<input type="radio" name="gender" value="0">
    女性:<input type="radio" name="gender" value="1">
    <input class="input-group" type="text" name="title" placeholder="Title">
    <textarea class="input-group" name="contact" placeholder="Contact"></textarea>
    <input class="btn btn-primary" type="submit" value="Submit">
</form>

methodをpostに設定してパラメータをstoreに送信する。

Controller

ContactFormController.php

use App\Models\ContactForm;

public function store(Request $request)
    {
        //上記でModelを呼び出し
        //インスタンス化
        $contact = new ContactForm;

        $contact->name = $request->input('name');
        $contact->email = $request->input('email');
        $contact->title = $request->input('title');
        $contact->gender = $request->input('gender');
        $contact->contact = $request->input('contact');

        $contact->save();

        return redirect('contacts/index');  //indexページにリダイレクト
    }

Modelファイルを利用して送られてきたパラメータをDBに保存します。

保存できてるかどうか一度DBで確認してください。
できてたらおk。

Viewに表示

Controller,Viewのそれぞれに書いていきます。

Controller

ContactFormController.php
use Illuminate\Support\Facades\DB;  //クエリビルダを利用

public function index()
    {
        // クエリビルダ
        $contacts = DB::table('contact_forms')
            ->select('id', 'name')
            ->get();

        // compactでindexに$contactsを表示する
        return view('contacts/index', compact('contacts'));
    }

必要なデータだけ欲しい時はクエリビルダを使った方が処理速度が遅くならなくて済む。

View

index.blade.php
@foreach($contacts as $contact)
    <div>{{$contact->id}}</div>
    <div>{{$contact->name}}</div>
    <button onclick="location.href='{{route('contacts.show',['id' => $contact->id])}}'"
            type="button"
            class="btn btn-info">
        詳細
    </button>
@endforeach

データをforeachで回してあげる。
カラム名を指定して必要な情報を表示する。

Viewで表示できてるのが確認できたらおk。

まとめ

基本的にはController,View,Routeをいじればできます。

本当に基礎の基礎なので多分もっと良い書き方とか他にあるかと思いますが、基礎学習中の人間のメモなのでご容赦を。

0
0
0

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
0
0