简体   繁体   中英

Laravel - Submitting blog post without showing in database?

As I am still learning and loving it so far, I have reached another stumbling block. On my blog page, I have a simple form setup with name and body. When I submit the completed form it resets the form looking like it was passed through to the database. But, it was not. When I refresh MySQL nothing happens. However, if I pass test data on MySQL it does pass through without an issue. (Stack: Laravel, PHP, MySQL, HTML/CSS, TailWindCSS)

My question is when I submit the blog post how do I ensure it is passed through to MySQL database?

Blog Controller

<?php

namespace App\Http\Controllers;

use App\Blog;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    public function index()
    {
        $blogs = \App\Blog::all();

        return view ('blog.index', compact('blogs'));

    }

    public function store()
    {
        $data = request()->validate([
            'title' => 'required|min:5',
            'blog' => 'required'
        ]);

        \App\Blog::create($data);

       return redirect()->back();

    }

    public function create()
    {
        $blogs = \App\Blog::all();

        return view ('blog.create', compact('blogs'));
    }

    public function show()
    {
        return view('blog.index');
    }
}


Migration Table

<?php

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

class CreatesBlogsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('blogs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->string('body');
            $table->timestamps();

        });
    }

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

Routes


use App\Mail\WelcomeMail;

Route::get('/email', function() {
    return new WelcomeMail();
});

Route::get('/home', 'HomeController@index');
Route::get('/about', 'HomeController@about');
Route::get('/contact', 'HomeController@contact');

Route::get('/blog', 'BlogController@index');
Route::get('/blog/create', 'BlogController@create');
Route::get('/blog/{blog}', 'BlogController@show');
Route::post('/blog', 'BlogController@store');

App\\Blog

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

From your controller code, we can see your form is passing (at least) 2 fields, title , and blog :

$data = request()->validate([
    'title' => 'required|min:5',
    'blog' => 'required'
]);

Checking your migration though, there is no blog field in your table:

$table->bigIncrements('id');
$table->string('title');
$table->string('body');
$table->timestamps();

It looks like the blog field on the form and in your controller code should really be body - or maybe vice-versa, the migration should create a blog field.

In any case, your controller tries to create() a record using title , and blog , but not body .

The migration specifies that neither title nor body can be null ( you would use ->nullable() to allow that), neither does it specify a default value for those fields ( you would use ->default('foo') for that). So if you try to create a record that is missing a value for the body field, which is not allowed to be null, and which has no default value, MySQL will fail.

As mentioned in another answer, make sure you have your mass assignment set up correctly, so title and body (or blog !) can be filled.

You need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default. Add the following attribute to the App\\Blog class:

protected $fillable = ['title', 'blog'];

Source and examples

Answering the question: Laravel throws an exception when it fails to save a model to the database. In this case the POST /blog endpoint adds a record to the log file ( storage/logs/laravel.log ) and returns a response with 503 status (server error). Check your frontend code to see whether it handles error responses or just ignores them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM