#解決したいこと
記事の投稿ができるようになりたい(create article)
#エラー内容
Symfony\Component\Debug\Exception\FatalThrowableError Class 'App\Post' not found
app/Http/Controllers/Auth/PostController.php:32
PostController.php:32
$article = Post::create([
#該当コード
PostController
php
1 2<?php 3 4namespace App\Http\Controllers\Auth; 5 6use Illuminate\Http\Request; 7use App\Http\Controllers\Controller; 8use App\Post; 9use Illuminate\Support\Facades\Auth; 10 11class PostController extends Controller 12{ 13 public function index() 14 { 15 return view('auth.drafts.new'); 16 } 17 18 public function postArticle(Request $request) 19 { 20 $request->validate([ 21 'title' => 'required|unique:posts|max:255', 22 'tags' => 'required|string', 23 'article' => 'required|string', 24 ]); 25 26 $tags = explode(' ', $request->tags); 27 $tag1 = $tags[0]; 28 $tag2 = (isset($tags[1])) ? $tags[1] : null; 29 $tag3 = (isset($tags[2])) ? $tags[2] : null; 30 31 $article = Post::create([ 32 'user_id' => Auth::user()->id, 33 'title' => $request->title, 34 'tag1' => $tag1, 35 'tag2' => $tag2, 36 'tag3' => $tag3, 37 'body' => $request->article, 38 ]); 39 return redirect('/'); 40 } 41}
web.php
Route::get('/', function () { return view('top'); }); Route::get('/', function () { return view('top'); }); Auth::routes(); Route::get('/home', 'HomeController@index')->name('home'); Route::get('/drafts/new', 'Auth\PostController@index')->name('drafts.new'); Route::post('/drafts/new', 'Auth\PostController@postArticle')->name('drafts.new.posts');
#バージョン
PHP 7.3.7
mysql Ver 14.14 Distrib 5.7.25
Laravel Framework 6.15.1
よろしくお願いします
回答2件
あなたの回答
tips
プレビュー