1

laravelにて新規投稿画面作成後、コントローラーやビューファイルなども変更し、保存機能の処理もしましたがDBに保存されません。また投稿画面の登録ボタンを押した後の”投稿を作成しました”の文字表示設定もしているのですが、文字も出てこず、白紙の画面に移行してしまいます。色々調べて試行錯誤してみたのですが、分からず…原因分りましたら、教えて頂きたいです。

student.php

<?php

namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;

class Student extends Model { use HasFactory; protected $fillable = [ 'name', 'address', 'user_id', 'img_path', ];

public function school_grades() { return $this->hasMany('App\Models\School_grade'); } }

web.php

<?php

use Illuminate\Support\Facades\Route; use App\Http\Controllers\StudentController;

/* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */

Route::get('/', function () { return view('welcome'); });

Route::get('/dashboard', function () { return view('dashboard'); })->middleware(['auth'])->name('dashboard');

Route::resource('/student', StudentController::class);

require DIR.'/auth.php';

StudentController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request; use App\Models\Student;

class StudentController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { // }

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('student.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $student=new Student();
    $student-&gt;name=$request-&gt;name;
    $student-&gt;address=$request-&gt;address;
    $student-&gt;user_id=auth()-&gt;user()-&gt;id;
    $student-&gt;save();
    return back()-&gt;with('message','投稿を作成しました');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
}

}

create.blade.php

@extends('layouts.app')
@section('content')

<form method="student" action="{{route('student.store')}}" enctype="multipart/form-data"> @csrf <div class="row"> <div class="col-md-10 mt-6"> <div class="card-body"> <h1 class="mt-4">学生登録画面</h1> @if(session('message')) <div class="alert alert-success">{{session('message')}}</div> @endif <form enctype="multipart/form-data"> <div class="form-group"> <label for="name">名前</label> <input type="text" name="name" class="form-control" id="name" placeholder="name"> </div>

            &lt;div class=&quot;form-group&quot;&gt;
                &lt;label for=&quot;address&quot;&gt;住所&lt;/label&gt;
                &lt;input type=&quot;text&quot; name=&quot;address&quot; class=&quot;form-control&quot; id=&quot;address&quot; placeholder=&quot;address&quot;&gt;
            &lt;/div&gt;

            &lt;div class=&quot;form-group&quot;&gt;
                &lt;label for=&quot;img_path&quot;&gt;顔写真&lt;/label&gt;
                &lt;div class=&quot;col-md-6&quot;&gt;
                    &lt;input id=&quot;img_path&quot; type=&quot;file&quot; name=&quot;img_path&quot;&gt;
                &lt;/div&gt;
            &lt;/div&gt;

            &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;登録&lt;/button&gt;
            &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;戻る&lt;/button&gt;
        &lt;/form&gt;
    &lt;/div&gt;
&lt;/div&gt;

</div>

@endsection

sytle.css

/* CSS Reset */
*,*::before,*::after{box-sizing:border-box}ul[class],ol[class]{padding:0}body,h1,h2,h3,h4,p,ul[class],ol[class],figure,blockquote,dl,dd{margin:0}body{min-height:100vh;scroll-behavior:smooth;text-rendering:optimizeSpeed;line-height:1.5}ul[class],ol[class]{list-style:none}a:not([class]){text-decoration-skip-ink:auto}img{max-width:100%;display:block}article>*+*{margin-top:1em}input,button,textarea,select{font:inherit}img:not([alt]){filter:blur(10px)}@media(prefers-reduced-motion:reduce){*{animation-duration:.01ms !important;animation-iteration-count:1 !important;transition-duration:.01ms !important;scroll-behavior:auto !important}}

/* iOS デフォルトスタイルのリセット */ input[type="submit"], input[type="button"] { border-radius: 0; -webkit-box-sizing: content-box; -webkit-appearance: button; appearance: button; border: none; box-sizing: border-box; cursor: pointer; }

input[type="submit"]::-webkit-search-decoration, input[type="button"]::-webkit-search-decoration { display: none; } input[type="submit"]::focus, input[type="button"]::focus { outline-offset: -2px; }

.define-text1 br{ margin-bottom: 10px; } html, body { font-size: large; background-color: #fff; color: #3c4447; font-family: 'ヒラギノ角ゴ ProN', 'Hiragino Kaku Gothic ProN', 'メイリオ', Meiryo, 'MS Pゴシック', 'MS PGothic', sans-serif; font-weight: 200; height: 100vh; /* viewpointに対して100%の意味 */ } h3{ font-weight: 200; } b{font-weight: bold;} hr{margin: 20px 0;}

.wrapper{ width: 1000px; background-color: #b6c3d5; min-height:700px; /* border: 1px solid #ccc; */ margin: 0 auto; }

.header{ background-color: #4d8eb8; margin: 0 0 10 0px; height: 100px; padding: 20px; }

.sidebar{ display:flex; flex-flow: column; justify-content:space-around; margin:20px; background-color: #ffffff; border: 5px solid #4d8eb8; border-radius: 10px; width: 250px; min-height:200px; float: right; padding: 10px; }

.content{ text-align: center; width:690px; min-height:200px; background-color: #ffffff; border: 5px solid #4d8eb8; border-radius: 10px; margin: 20px; padding: 10px; }

.footer{ background-color: #4d8eb8; height: 70px; text-align: center; padding: 10px; }

.innertop{ background-color: #c3d2d2; min-height:130px; }

.topbtn{ display:inline-block; padding: 20px; width: 150px; background-color: #a3c3c2; font-weight: bold; border-radius: 10px;

}

.toph2{ padding-top: 10px; margin: 0 0 50px 0; }

.topbtn2{ display:inline-flex; flex-flow: column; justify-content:space-around; padding: 25px; width: 115px; background-color: #a3c3c2; font-weight: bold; border-radius: 10px;

}

cubick
  • 20,987
  • 5
  • 25
  • 64
ema
  • 11
  • 2
  • デバッガを使ってどこに問題があるか順番に追いかけていくということはやりましたか? やってなければ、まずそれをやって、どのあたり(送信? 受信? 受信データの処理? DB への INSERT 処理?)に問題がありそうかの切り分けをしましょう。それが今すぐできるのは質問者さんだけなのです。 – WebSurfer Jan 20 '24 at 04:07

1 Answers1

0

<form> の閉じタグが無さそうです.
もしコピペミスなら正しくコピペしていただけると助かります

fuwasegu
  • 411
  • 6
  • ありがとうございます。create.blade.phpの閉じタグ追加して、<form method="student"の部分のstudentをpostに変更したところ保存できました! – ema Feb 01 '24 at 12:44
  • よかったです〜〜 – fuwasegu Feb 02 '24 at 06:37