Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel : Field 'name' doesn't have a default value

I’m testing the route locally through postman, sending JSON object with properties and values needed to create an event:

//http://127.0.0.1:8000/api/events

{
    "name":"Event 1",
    "description":"Event 1 description",
    "start_time":"2024-01-01 15:00:00",
    "end_time":"2024-01-01 16:00:00"
    
}

Event.php //Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Event extends Model
{
    use HasFactory;

    protected $fillable = ['name','description','start_time','end_time','user_id'];
    

    public function user():BelongsTo{
        return $this->belongsTo(User::class);
    }

    public function attendee():HasMany{
        return $this->hasMany(Attendee::class);
        
    }
}

EventController.php

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Event;
use Illuminate\Http\Request;

class EventController extends Controller
{


    public function store(Request $request)
    {


        $event = Event::create([
            $request->validate([
                'name' => 'required|string|max:255',
                'description'=>'nullable|string',
                'start_time' => 'required|date',
                'end_time'=> 'required|date|after:start_time',
            ]),
            'user_id'=>1
            ]);

        return $event;
    }


}

but i’m get this error object response:

"message": "SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (Connection: mysql, SQL: insert into `events` (`user_id`, `updated_at`, `created_at`) values (1, 2023-12-02 18:01:52, 2023-12-02 18:01:52))"

reading the message, the controller doesn’t seem to receive any data because its not even trying to insert them to the table (name,description,start_time,end_time)

reading about the error, some suggestions is i didn’t add these columns to my $fillable, another suggestion is that i’m sending data to the database and exempting fields that are not nullable or fields that can’t be left empty

but i didn’t fall for any of these mistakes, so what could be the issue ? thanks in advance.

please don’t hesitate to ask for another code blocks if needed.
please note that columns and fake data are generated using database factories and seeders

also here is the event migrations:

<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('events', function (Blueprint $table) {
            $table->id();
            $table->foreignIdFor(User::class);
            $table->string('name');
            $table->text('description')->nullable();
            $table->dateTime('start_time');
            $table->dateTime('end_time');

            $table->timestamps();
        });
    }

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

>Solution :

You’ve validated in the create method, which you tried to create first; that’s the reason it gives that error.

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Event;
use Illuminate\Http\Request;

class EventController extends Controller
{
    public function store(Request $request)
    {
       $request->validate([
            'name' => 'required|string|max:255',
            'description'=>'nullable|string',
            'start_time' => 'required|date',
            'end_time'=> 'required|date|after:start_time',
       ]);

        $event = Event::create([
            'name' => $request->name,
            'description' => $request->description,
            'start_time' => $request->start_time,
            'end_time' => $request->end_time,
            'user_id'=>1
        ]);

        return $event;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading