-2

我创建了一个页面,您可以在其中向数据库添加记录,当我添加记录时出现错误:

SQLSTATE [HY000]:一般错误:1364 字段“网站”没有默认值(SQL:插入businessname、、、、)值(电机mail,motor@mail.com,2021-02-02 07:47:26 , 2021-02-02 07:47:26))updated_atcreated_at

下面附上截图。

我的数据库 business_table.php

<?php

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

    class BusinessTable extends Migration
    {
    /**
    * Выполнение миграций.
    *
    * @return void
    */
    public function up()
    {
    Schema::create('business', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('mail');
    $table->string('website')->nullable();
    $table->timestamps();
    });
    }

    /**
    * Отмена миграций.
    *
    * @return void
    */
    public function down()
    {
    Schema::drop('business');
    }
    }

我的控制器:BusinessController.php

<?php

    namespace App\Http\Controllers;
    use \App\Models\Business;
    use Illuminate\Http\Request;

    class BusinessController extends Controller
    {
    public function index()
    {
    $business = \App\Models\Business::all();
    return view('business', compact('business'));
    }
    public function createbusiness()
    {
    return view('/createbusiness');
    }
    public function create()
    {
    return view('business.create');
    }
    public  function store()
    {
    $business = new Business();
    $business->name = request()->input('name');
    $business->mail = request()->input('mail');
    $business->save();
    return redirect('/business');

    }
    }

我的模型:Business.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Business extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'business';
}

我的 createbusiness.blade.php(查看页面以向数据库添加新条目)

@extends('layouts.layout')
@section('title')Новий бізнес@endsection
@section ('main_content')
    <div class="row py-lg-5 ">
        <div class="col-lg-6 col-md-8 mx-auto">
            <h1 class="fw-light">Write new data</h1>
<form method="post" action="/business">
    {{ csrf_field() }}
    <div>
        <input type="id" name="id" value="" placeholder="id">
    </div>
    <div>
        <input type="text" name="name" value="" placeholder="name">
    </div>
    <div>
        <textarea type="text" name="mail" value="" placeholder="mail"></textarea>
    </div>
    <div>
        <textarea type="text" name="website" value="" placeholder="website"></textarea>
    </div>
    <div>
        <button type="sumbit">Sumbit</button>
    </div>
</form>
        </div>
    </div>
    </section>
    </p>
@endsection

错误截图

4

2 回答 2

2

如果您的“网站”字段不是强制性的,请修改:

$table->string('website');

经过 :

$table->string('website')->nullable();
于 2021-02-02T08:22:53.790 回答
1

您没有在您的代码的存储方法中插入列网站的值 - :

public  function store()
    {
    $business = new Business();
    $business->name = request()->input('name');
    $business->mail = request()->input('mail');
    $business->save();
    return redirect('/business');    
    }

做这个 :

public  function store()
    {
    $business = new Business();
    $business->name = request('name');
    $business->mail = request('mail');
    $business->website = request('website');
    $business->save();
    return redirect('/business');

    }

如果您的“网站”字段不是强制性的,则使其可为空

于 2021-02-02T08:29:15.607 回答