I am working on a project and want to make a timestamp column in the database immutable. In Symfony, I know how to do that, but in Laravel, I don't understand.
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('email')->unique();
$table->string('verification_code')->nullable();
$table->timestamp('accept_policy_at');
});
}
}
Related
This is my migration code. This is not working not changing column collation.
public function up()
{
Schema::table('product', function (Blueprint $table) {
$table->string('hash_id')->collate('utf8mb4_bin')->change();
});
}
public function up()
{
Schema::table('product', function (Blueprint $table) {
$table->string('hash_id')->collation('utf8mb4_bin')->change();
});
}
I don't know what's wrong because I'm very new to this.
// Product Model
class Product extends Model
{
use HasFactory;
public function store()
{
return $this->belongsTo(Store::class);
}
}
// Store Model
class Store extends Model
{
use HasFactory;
public function products()
{
return $this->hasMany(Product::class);
}
}
// Products table migration
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->float('price');
$table->string('description');
$table->timestamps();
$table->foreignId('store_id')->constrained()->onDelete('cascade');
});
// Stores table migration
Schema::create('stores', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('image_url');
$table->string('phone');
$table->timestamps();
});
When I run the migration, it gives me this error
I've tried changing the data type of the 'id' but still not working. I've also tried with
$table->foreign('store_id')->references('id')->on('stores')->onDelete('cascade');
but still not working.
What I want is a relation so that when I delete a store, all products that belong the store are also deleted.
Thanks 🙏
Change the name of the stores migration file to a date prior to 2021-07-28 so the table stores is migrated before the table products
Example: 2021_07_27_004700_create_stores_table
Laravel uses the name of the migration files for the order of migration. With the format of the date as the start of the file name, it is dependant on the date of the creation of the file.
In my Larave-8, I created a table using this migration:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id');
$table->string('first_name',50);
$table->string('last_name',50);
$table->string('other_name',50)->nullable();
$table->string('gender',20);
$table->string('user_photo',350)->nullable();
$table->integer('nationality_id');
$table->string('marital_status',50);
$table->date('dob')->nullable();
$table->string('address', 300)->nullable();
$table->integer('country_id')->nullable();
$table->integer('state_id')->nullable();
$table->integer('city_id')->nullable();
$table->string('cv_file',350)->nullable();
$table->text('summary')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
How do I alter the table through migration and add these:
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
Thanks
create another migration like to alter existing table
public function up()
{
Schema::table('profiles', function (Blueprint $table) {
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
});
}
public function down() {
Schema::table('profiles', function (Blueprint $table) {
$table->dropForeign('nationality_id');
$table->dropForeign('country_id');
$table->dropForeign('state_id');
$table->dropForeign('city_id');
});
}
So, I want to add a column ('Semester_TA') and change the property of this column ('kode_mk') to be unique value, so I created this migration:
public function up()
{
Schema::table('mk', function(Blueprint $table){
$table->string('Semester_TA')->after('sks');
$table->string('kode_mk')->unique();
});
Schema::table('lr2', function(Blueprint $table){
$table->dropForeign(['id']);
});
}
public function down()
{
//
}
I also want to drop/delete this foreign key ('id') that I created as it sometimes give a constraint error in my project. My question is what function down should I write to reverse the update above that i'm about to do in case of migration fails or error?
this is the table "mk" and "lr2" migrations that i use right now:
Schema::create('mk', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('kode_mk');
$table->string('mk');
$table->integer('sks');
$table->integer('semester');
$table->integer('available_seats');
$table->timestamps();
});
Schema::create('lr2', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('k_mk');
$table->string('mk');
$table->string('angkatan');
$table->integer('request_seats');
$table->boolean('status_request');
$table->timestamps();
$table->foreign('id')->references('id')->on('mk');
});
First, you have to create a new migration:
php artisan make:migration Add_column_change_the_property
Then in that migration file up(), try:
public function up()
{
Schema::table('mk', function(Blueprint $table){
$table->string('Semester_TA')->after('sks');
$table->string('kode_mk')->unique();
});
}
Then in the migration file down() try:
public function down()
{
$table->dropColumn('kode_mk');
$table->foreign('id')->references('id')->on('mk');
//
}
Then migrate the file by:php artisan migrate
Hey guys so I'm using spatie binary uuid package and I had few doubts
Things done so far:
User.php Migration:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('uuid');
$table->primary('uuid');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Role migration just have basic field called "name" with timestamps
Pivot table: role_user
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned()->nullable()->index();
$table->uuid('user_uuid');
});
}
I know this is terribly wrong and I don't know what to do, I'm trying to save the Role model via this call
$uuid = '478d7068-ae64-11e8-a665-2c600cf6267b';
$model = User::withUuid($uuid)->first();
$model->roles()->save(new Role(['name' => 'Admin']));
it doesn't work, where am I going wrong? I think it has something to do with role_user migration
User.php
public function roles()
{
return $this->belongsToMany(Role::class);
}
try this, pivot migration:
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned()->nullable()->index();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->uuid('user_uuid');
$table->foreign('user_uuid')->references('uuid')->on('users')->onDelete('cascade');
});
}
roles relation:
public function roles(){
return $this->belongsToMany(Role::class,'role_user','user_uuid','role_id');
}
please let me know if it didn't work
you should edit your relation to this
return $this->belongsToMany(Role::class,'role_user','user_uuid','role_id');
if you say about your error we better can help you