I'm refreshing my Laravel skills (using version 7.13.0), following the freecodecamp.org tutorial, here: https://www.youtube.com/watch?v=ImtZ5yENzgE&t=11889s (1:21:49 to 1:22:50). When I get to the task of adding a profile manually with php artisan tinker, like I did on the frontend, it can not save. The database is sqlite.
THIS IS THE FULL ERROR:
Illuminate/Database/QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: profiles.url (SQL: insert into "profiles" ("title", "description", "user_id", "updated_at", "created_at") values (Cool Title, Description, 1, 2020-05-29 18:41:02, 2020-05-29 18:41:02))'
My function in profiles_table.php in database\migrations folder seems OK. It is this:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->timestamps();
$table->index('user_id');
});
}
The Profile.php model function is:
public function user()
{
return $this->belongsTo(User::class);
}
And the User.php model function is:
public function profile()
{
return $this->hasOne(Profile::class);
}
So, in the terminal, after entering php artisan tinker, I filled in the following before it failed to save:
>>> $profile = new \App\Profile();
=> App\Profile {#3056}
>>> $profile->title = 'Cool Title';
=> "Cool Title"
>>> $profile->description = 'Description';
=> "Description"
>>> $profile->user_id = 1;
=> 1
>>> $profile->save();
EDIT: this is the controller file ProfilesController.php, if it helps to solve the problem:
<?php
namespace App\Http\Controllers;
use \App\User;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function index($user)
{
$user = User::find($user);
return view('home', [
'user' => $user,
]);
}
}
All parts went OK, and after I typed the SAVE - it showed the error above. Therefore I can't continue ahead to show the new profile made manualy.
I searched up Google and looked here for answers, like in this question and that question. It didn't solve my problem, and others I think are not as relevant.
What should I do in order to fix it?
Tanks to @mrhn, adding a new null table + installing composer require doctrine/dbal
as suggested here: https://stackoverflow.com/a/37533819/12952748.
The problem is your database does not allow the column url
to be null. Your code is fine and nothing wrong with it, thou it seems you have maybe ran the migration and changed columns definition after.
In Laravel migrations only run once and secures the database has the same structure across systems. For getting the field nullable make a new migration.
php artisan make:migration url_nullable
For you new migration file, add the following migration. Which simply tells the migration to update url
to be a string
and nullable
.
Schema::table('profiles', function (Blueprint $table) {
$table->string('url')->nullable()->change();
});
After that run your migration and you database should be up to date.
php artisan migrate