Im using SQL uuid_short to build my laravel. is it possible to apply uuid to spaite laravel?
Schema::create($tableNames['permissions'], function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('permissions_uuid'); //(add by myself)
$table->string('name');
$table->string('guard_name');
$table->timestamps();
});
//My Uuid creation
DB::selectOne(DB::raw('select uuid_short() as uuid'))->uuid
Related
I'm trying to make the id start from a certain value. It's working fine when I don't specify the startingValue(1000)
This is my products table schema
Schema::create('products', function (Blueprint $table) {
$table->id()->startingValue(1000);
$table->foreignId('store_id')->constrained()->onDelete('cascade');
$table->foreignId('category_id')->constrained()->onDelete('cascade');
$table->string('name');
$table->float('price');
$table->string('specification');
$table->string('description');
$table->timestamps();
});
This is my favorites table schema
Schema::create('favorites', function (Blueprint $table) {
$table->foreignIdFor(Product::class)->constrained()->onDelete('cascade');
$table->foreignIdFor(User::class)->constrained()->onDelete('cascade');
});
It gives me this error
Thanks 🙏
I am running this migration:
Schema::create('logs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('key');
$table->text('value')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
"Users" table migration (migrated at Laravel 5.8)
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('last_message_id')->nullable();
$table->timestamps();
});
Error that I receive:
Foreign key constraint is incorrectly formed
To me it seems like you ran the users migration with a previous version of laravel. Have a look with a db tool (like phpmyadmin, Sequel, etc.) what the actual data type of your users.id field is.
I assume it's not bigInteger so your solution could be to just use
$table->unsignedInteger('user_id');
I want to use two models on my laravel 7.x application : Users and Images :
# Users migration : 2014_10_12_000000_create_users_table.php
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
# Images migration : 2020_03_27_121254_create_models_images_table
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned;
$table->string('name');
$table->timestamps();
});
Schema::table('images', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
When trying to migrate i receive following error : General error: 1215 Cannot add foreign key constraint (SQL: alter table images add constraint images_user_id_foreign foreign key (user_id) references users (id) on delete cascade)
I have already searched over google without success, someone could helps me please ?
Thank you
Problem
You are not setting the unsigned properly.
Solution
Replace:
$table->bigInteger('user_id')->unsigned;
By:
$table->bigInteger('user_id')->unsigned();
Explanation
Since the user_id is not unsigned, the definition does not match the id of the users table which means you cannot set the foreign key.
Before Laravel 7.x
You could also do:
$table->unsignedBigInteger('user_id');
Laravel 7.x
As of Laravel 7, you can do it like this in your users migration:
Schema::table('users', function (Blueprint $table) {
$table->id();
// ...
});
And in your images migration:
Schema::table('users', function (Blueprint $table) {
// ...
$table->foreignId('user_id')->constrained();
});
You can find more information here: https://laravel.com/docs/7.x/migrations#foreign-key-constraints
You are missing bracket in unsiged()
As per Laravel Documentation
->unsigned() Set INTEGER columns as UNSIGNED (MySQL)
Change
$table->bigInteger('user_id')->unsigned;
to
$table->bigInteger('user_id')->unsigned();
I'm wondering if this is possible. I have 3 models.
Users
TenantPreferances
PropertyAdverts
I'm trying to find out if I can do a query like so.
Find all tenants, whose preferences, match the currently signed in users properties.
The 3 databases are like so
User Model
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('userType');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
PropertyAdverts
Schema::create('property_adverts', function (Blueprint $table) {
$table->increments('id');
$table->string("photo");
$table->string('address');
$table->string('county');
$table->string('town');
$table->string('type');
$table->string('rent');
$table->string('date');
$table->string('bedrooms');
$table->string('bathrooms');
$table->string('furnished');
$table->longText('description');
$table->integer('user_id'); //Landlord ID
$table->timestamps();
});
Tenant Preferances
Schema::create('tenant_preferances', function (Blueprint $table) {
$table->increments('id');
$table->string('county');
$table->string('type');
$table->string('rent');
$table->string('bedrooms');
$table->string('bathrooms');
$table->boolean('status')->default('0');
$table->integer('user_id'); //Tenant ID
$table->timestamps();
});
Yes, that is possible. You need to dig in on relations. You can then create a function to define the relation on you model:
function propertyAdverts() {
return $this->hasMany(PropertyAdverts::class);
}
You can access the relation from the user model by using $user->propertyAdverts. If you want to eager load them you can do so:
User::with('propertyAdverts')->find(3);
But notice that Laravel does not do a regular join by default. It first fetches all users, and then fetches all propertyAdverts using a single query using a in statement.
I need to make a HasManyThough relation where the middle Model holds the foreign keys of both Models.
Here is detail:
Migrations:
Schema::create('carriers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Schema::create('shipping_zones', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Schema::create('shipping_rates', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('shipping_zone_id')->unsigned()->index();
$table->integer('carrier_id')->unsigned()->index();
$table->decimal('rate', 20, 6);
});
Now I need something like
$carrier->shippingZone()
Is there any easy way to get this?
As per your migrations you need Many To Many relation rather than the HasManyThough relation.
Carrier.php
public function shippingZones()
{
return $this->belongsToMany('App\ShippingZone', 'shipping_rates);
}
You can access all shipping zones related to the carrier using, $carrier->shippingZones.
If you need to chain the query you can use $shippingZones = $carrier->shippingZones()->orderBy('id')->get(); here
If you need to use HasManyThough you would have to change the migrations. here