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
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
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 getting 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');
}
};
You are passing an array of an array of data instead of an array of data, this will work:
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;
}
}
Your code is doing this:
$data = $request->validate([
'name' => 'required|string|max:255',
'description' => 'nullable|string',
'start_time' => 'required|date',
'end_time' => 'required|date|after:start_time',
]);
$event = Event::create(
[
$data,
'user_id' => 1,
]
);
So, your data is like this:
$data = [
0 => [
'name' => 'name value',
'description' => 'description value',
'start_time' => 'start time value',
'end_time' => 'end time value',
],
'user_id' => 1,
];
But you want it to be like this:
$data = [
'name' => 'name value',
'description' => 'description value',
'start_time' => 'start time value',
'end_time' => 'end time value',
'user_id' => 1,
];
So, indeed, you have name
missing. That is also way user_id
is the only value completed on the tried query (check the error), but all the other values are missing.