簡體   English   中英

Laravel 5在數據庫中存儲空行?

[英]Laravel 5 storing empty row in database?

我目前正在為我的網站開發一個簡單的博客系統,為此使用Laravel 5.5,但遇到了問題。 每當我使用創建的表單創建新文章時,都會在數據庫中存儲一個空行,而不是表單中的數據...

這是我存儲文章的功能:

public function newsStore()
{
    $input = Request::all();

    Article::create($input);

    return $input;
}

根據我要遵循的教程,這應該可以解決問題。這是我目前正在使用的表單:

{!! Form::open(['url' => 'news']) !!}
    <!-- Article Title -->
    <div class="form-group row">
        {!! Form::label('title', 'Title:', ['class' => 'col-2 col-form-label']) !!}
        <div class="col-10">
            {!! Form::text('title', null, ['class' => 'form-control']) !!}
        </div>
    </div>
    <!-- Article Description -->
    <div class="form-group row">
        {!! Form::label('description', 'Description:', ['class' => 'col-2 col-form-label']) !!}
        <div class="col-10">
            {!! Form::text('description', null, ['class' => 'form-control']) !!}
        </div>
    </div>
    <!-- Article Excerpt -->
    <div class="form-group row">
        {!! Form::label('excerpt', 'Excerpt:', ['class' => 'col-2 col-form-label']) !!}
        <div class="col-10">
            {!! Form::text('excerpt', null, ['class' => 'form-control']) !!}
        </div>
    </div>
    <!-- Article Image -->
    <div class="form-group row">
        {!! Form::label('image', 'Image:', ['class' => 'col-2 col-form-label']) !!}
        <div class="col-10">
            {!! Form::file('image') !!}
        </div>
    </div>
    <!-- Article Message -->
    <div class="form-group row">
        {!! Form::label('message', 'Message:', ['class' => 'col-2 col-form-label']) !!}
        <div class="col-10">
            {!! Form::textarea('message', null, ['class' => 'form-control', 'rows' => '7']) !!}
        </div>
    </div>
    <!-- Article Submit -->
    <div class="form-group">
        {!! Form::button('Add Article', ['class' => 'btn btn-primary waves-effect waves-light float-right', 'type' => 'submit']); !!}
    </div>
{!! Form::close() !!}

這是我使用的路線:

Route::post('news', 'DashboardController@newsStore'); // The articles store route

這是我的模型:

class Article extends Model
{
    protected $fillable = [
        'title',
        'description',
        'excerpt',
        'image',
        'body',
        'published_at'
    ];
}

我以前做過一次,並嘗試重做全部,但似乎無法弄清楚出了什么問題。

提前致謝!

為了使create()起作用,請確保您使用的是同名的列。 例如,如果您的列名是title ,那么您還需要在表單中使用title而不是news-title

{!! Form::text('title', null, ['class' => 'form-control']) !!}

當您使用本文Article::create(); 這意味着您的所有輸入名稱應與表列名稱相同。

例如:

<input name="title" ...與各個表中的列調用news-title相同


如果不一樣

使用查詢生成器

DB::table('table_name')->insert(
    [
        'name' => $input->news-title
        .....
    ]
);

閱讀有關MySQL DB列名稱中的連字符的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM