SQLSTATE [HY000]:常规错误:1364字段“标题”没有默认值

时间:2019-06-28 13:32:23

标签: laravel eloquent laravel-5.8

嗨,我正在尝试将数据插入db,但是它说:

  

SQLSTATE [HY000]:常规错误:1364字段“标题”没有   默认值(SQL:插入projectsowner_idupdated_at,   created_at)值(1,2019-06-28 13:17:11,2019-06-28 13:17:11))

我正在关注Laracasts Laravel from scratch tutorial

控制器:

      public function store()
      {
        $attributes = $this->validateProject();
        $attributes['owner_id'] = auth()->id();
        $project = Project::create($attributes);

    //Project::create($attributes);
    //Project::create(request(['title', 'description']));

          Mail::to($project->owner->email)->send(
            new ProjectCreated($project)
          );

        return redirect('/projects');
      }

型号:

  protected $guarded = [];

表:

      Schema::create('projects', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('owner_id');
        $table->string('title');
        $table->text('description');
        $table->timestamps();

        $table->foreign('owner_id')->references('id')->on('users')->onDelete('cascade');
    });

刀片文件:

   <form method="POST" action="/projects">
   @csrf
   <div class="field">
    <label class="label" for="title">Title</label>
    <div class="control">
        <input type="text" class="input {{ $errors->has('title') ? 'is-danger' : ''}}" name="title" value="{{ old('title') }}" placeholder="Project title">
    </div>
    </div>
    <div class="field">
      <label class="label" for="title">Description</label>
      <div class="control">
        <textarea name="description" class="textarea {{ $errors->has('description') ? 'is-danger' : ''}}" placeholder="Project description">{{ old('description') }}</textarea>
    </div>
   </div>
      <div class="field">
      <div class="control">
        <button type="submit" class="button is-link">Create Project</button>
        </div>
    </div>

   @include('errors')

  </form>

如何解决此问题

4 个答案:

答案 0 :(得分:1)

fillable中将您的列名称添加到您的模型中(我想您的模型名称为Project.php

所以您的模型类应该喜欢这个。

<?php

mnamespace App;

use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
protected $guarded = [];
protected $fillable = [
    'title', 'owner_id','description'
];
public function owner()
{
    return $this->belongsTo(User::class);
}

public function tasks()
{
    return $this->hasMany(Task::class);
}

public function addTask($task)
{
    $this->tasks()->create($task);
}
}

然后像这样更新您的controller store方法。

public function store(Request $request)
  {
    $attributes = $this->validateProject();
    $attributes->owner_id = auth()->id();
    $attributes->title = $this->request->title;
    $attributes->description= $this->request->description;
    $project = Project::create($attributes);

      Mail::to($project->owner->email)->send(
        new ProjectCreated($project)
      );

    return redirect('/projects');
  }

答案 1 :(得分:1)

您在title表上有字段projects,但是您没有为其分配值。由于将其设置为Not Nullable,因此会出现此错误。

在使用似乎没有的$fillable时,您需要所有属性都在模型的Project::create($attributes);属性中。

$fillable的示例为:

protected $fillable = [
    'title',
    'description',
    'owner_id',
];

还有其他一些潜在原因,但是如果您不包括完整的Project模型和此请求来自的视图,就无法分辨。

修改

您需要将功能更改为此:

public function store(ProjectRequest $request)
  {
    $attributes = $request->all();
    $attributes['owner_id'] = auth()->id();
    $project = Project::create($attributes);

      Mail::to($project->owner->email)->send(
        new ProjectCreated($project)
      );

    return redirect('/projects');
  }

您可以通过运行ProjectRequest然后将验证规则放在其中来创建php artisan make:request ProjectRequest类。

了解更多here

答案 2 :(得分:1)

错误本身不言自明,请检查以下代码:

$attributes['owner_id'] = auth()->id();
$project = Project::create($attributes);

在这里,您正在项目表中创建一条新记录,为此,您仅占用一列,即owner_id,但是该表中有一列title,该列没有默认值。 因此,要么在创建新记录时获取所有列,要么为这些列提供默认值(空值或其他值)。

要将null设置为迁移的默认值:

$table->string('title')->nullable();

或者您可以直接更改数据库中的列并将其默认值设置为null,请参见以下屏幕截图:

enter image description here

答案 3 :(得分:0)

无法跟踪您面临的问题。尝试一下此代码,如果遇到任何问题,请发表评论。

在您的路由文件中

Route::post('project', 'ProjectController@store')->name('project.store');

在您的创建视图中

<form method="POST" action="{{route('project.store')}}">
    @csrf
    <div class="field">
        <label class="label" for="title">Title</label>
        <div class="control">
            <input type="text" class="input {{ $errors->has('title') ? 'is-danger' : ''}}" name="title"
                   value="{{ old('title') }}" placeholder="Project title">
        </div>
    </div>
    ...
    <div class="field">
        <div class="control">
            <button type="submit" class="button is-link">Create Project</button>
        </div>
    </div>

    @include('errors')

</form>

在您的ProjectController中

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

class UserController extends Controller{
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:255',
        ]);

        $post = Post::create([
                    'title' => $request->title,
                    'owner_id' => auth()->id()
                    ]);

    return redirect('/projects');
  }

编辑1

在您之前的ProjectsController中的代码中,不要使用$ attributes尝试使用

public function store()
    {
        $project = Project::create([
            'title' => request('title'),
            'owner_id' => request('owner_id')
        ]);

        Mail::to($project->owner->email)->send(
            new ProjectCreated($project)
        );

        return redirect('/projects');
    }

编辑2

尝试使用此方法而不是使用create方法

public function store()
{
        $project = new Project();
        $project->title = request('title');
        $project->owner_id = request('owner_id');
        $project->save();
...
}