PT-83's avatar
Level 7

Create post form data not submitting to database?

Hi all, I once again am having a hard time with another form submission. I have tried to replicate past questions regarding this issue, but nothing seems to work. I may be missing a small detail but I cannot figure this out. I am hoping for help again.

Webphp

Auth::routes();

Route::get('/posts', 'PostController@index');
Route::get('/posts/create', 'PostController@create');
Route::post('/posts/create', 'PostController@store');
Route::get('/posts/{post}', 'PostController@show');
Route::get('/posts/{post}/edit', 'PostController@edit');
Route::patch('/posts/{post}', 'PostController@update');
Route::delete('/posts/{post}', 'PostController@destroy');

Route::post('/register', 'Auth\RegisterController@create')->name('register');

Route::get('/contact', 'ContactController@create');
Route::post('/contact', 'ContactController@store');

PostController


namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $posts = Post::all();

        return view('post.index', compact('posts'));
    }

    public function create()
    {
        $post = new Post();

        return view('post.create', compact ('post'));
    }

    public function store(Request $request)
    {
        $data = request()->validate([
            'title' => 'required',
            'body' => 'required'
        ]);

        $post = \App\Post::create($data);

        return redirect('/posts');
    }

    public function show(Post $post)
    {
        return view('post.show', compact('post'));
    }

    public function edit(Post $post)
    {
        return view('post.edit', compact('post'));
    }

    public function update(Post $post)
    {
        $post->update($this->validatedData());

        return \redirect('/posts');
    }
    
    public function destroy(Post $post)
    {
        $post->delete();

        return redirect('/posts');
    }

    protected function validatedData()
    {
        return request()->validate([
            'title' => 'required',
            'body' => 'required'
            //'photograph'=> nullable
            //'gif'=> nullable
        ]);
    }
}

Migration


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->longText('body');
            // $table->#('photograph');
             // $table->#('gif');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Post Form

    <div class="row justify-content-cemter">
       <div class="col-md-8">
           <div class="card">
               <div class="card-header">Create New Post</div>


                <div class="card-body">
                    <form action="/posts/create" method="post">

                        @csrf

                        <div class="form-group">
                            <label for="title">Title</label>
                            <input name="title" type="text" class="form-control" id="title" aria-describedby="titleHelp" placeholder="Enter Title" autocomplete="off">
                            <small id="titleHelp" class="form-text text-muted">Give your post a title that will describe your post easily</small>
                        </div>

                        <div class="form-group">
                            <label for="body">Body</label>
                            <textarea type="text" cols="30" rows="3" class="form-control" id="body" aria-describedby="bodyHelp" placeholder="Enter Post Body" autocomplete="off"></textarea>
                             <small id="bodyHelp" class="form-text text-muted">Enter as much detail you'd like!</small>
                        </div>

                        <button type="submit" class="btn btn-primary">Create Post</button>

                    </form>

                    @if ($errors->any())
                        <div class="alert alert-danger mt-5">
                            <ul>
                     @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                    @endforeach
                            </ul>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

Post Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $guarded = [];
}

0 likes
6 replies
PT-83's avatar
Level 7

Not that I see. The form just refreshes. I tried to dd($data) I got a 500Server error

PT-83's avatar
Level 7

@jlrdw I did try adding Request $request to the store function in the controller but still noting is passed through.

PT-83's avatar
Level 7

@jlrdw thanks! I have edited the store function, I believe correctly.

Edit* - I have discovered my issue. In the form, I forgot to include name="body" so it was not seeing the inputted text. By including the name"" the form is now passing the data to the database!

Please or to participate in this conversation.