掲示板サイトを作っており記事作成ができません。
記事を作成しても作成されず、画像のアップロードのみが起こってしまいます。
どうすれば画像のアップロードと記事作成ができるようになりますか?
controller
public function store(Request $request) { $params = $request->validate([ 'title' => 'required', 'body' => 'required', 'image' => 'required|file|image|max:2048', ]); #画像のアップロード $file = $params['image']->hashName(); $image = \Image::make(file_get_contents($params['image']->getRealPath())); $image ->save(public_path().'/images/origin/'.$file) ->resize(450, 300) ->save(public_path().'/images/main/'.$file) ->resize(225, 150) ->save(public_path().'/images/thumb_main/'.$file) ->resize(120, 80) ->save(public_path().'/images/thumb_sub/'.$file); #記事のタイトル、本文(body)、画像のハッシュ名($file)をdatabaseに保存。 $article = new Article; $article->title = $request->title; $article->body = $request->body; $article->image = $file; $article->save; return redirect()->route('articles.index'); }
view
@extends('layouts/layout') @section('title') 記事作成ページ @endsection @section('content') <p>記事作成</p> <form action="{{ route('articles.store') }}" method="post" enctype="multipart/form-data"> {{ csrf_field() }} <p>タイトル</p> <input type="text" name="title" value="{{old('title')}}"> @if($errors->has('title')) <p>{{$errors->first('title')}}</p> @endif <p>本文</p> <textarea name="body" rows="5">{{old('body')}}</textarea> @if($errors->has('body')) <p>{{$errors->first('body')}}</p> @endif <input type="file" name="image"> @if($errors->has('image')) <p>{{$errors->first('image')}}</p> @endif @foreach($tags as $tag) <input type="checkbox" name="tags[]" value="{{$tag->id}}">{{$tag->name}} @endforeach <input type="submit" value="記事作成"> </form> @endsection
migrationファイル
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateArticlesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('articles', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('body'); $table->string('image'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('articles'); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/18 12:35