phpdatabaselaravellaravel-bladelaravel-datatables

How to solve the problem of writing data to the database?


I created a page where you can add records to a database, and when I add a record I get an error:

SQLSTATE[HY000]: General error: 1364 Field 'website' doesn't have a default value (SQL: insert into business (name, mail, updated_at, created_at) values (Motor, motor@mail.com, 2021-02-02 07:47:26, 2021-02-02 07:47:26))

Attached a screenshot below.

my database 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');
    }
    }

My Controller: 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');

    }
    }

My Model: 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';
}

My createbusiness.blade.php (view page to add new entries to the database)

@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

error screenshot


Solution

  • you are not inserting value for column website in store method your code -:

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

    do this :

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

    If your "website" field is not mandatory, then make it nullable