I want to make my attribute as an autoIncriment with the Id
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->decimal('price', 17, 6)->nullable();
$table->string('photo');
$table->decimal('reference')->autoIncrement()->unique()->from(1000)->unsigned();
$table->boolean('active');
$table->timestamps();
});
}
i have created this function and i want to make the reference auto increment start from 1000 , when i migrate it nothing change in my database
Try setting up the reference field to an unsignedInteger first.
Example:
$table->unsignedInteger('reference')->->autoIncrement()->unique()->from(1000);
Related
this image is show the my problem
i want try to type of payment in database
any body can help me
public function up()
{
Schema::create('gems_stats', function (Blueprint $table) {
$table->id();
$table->integer('userid');
$table->string('event');
$table->string('type');
$table->integer('closing_bal');
$table->time('');
});
}
I want to write a Laravel Migration auto increment ID as a primary key. I want to start this ID with a another value rather than 1. How can I do so ?
The migration up() function:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('phone');
$table->rememberToken();
$table->timestamps();
});
}
As of Laravel 8.x, you can now use this in your migration:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id()->startingValue(1200);
});
}
Source: https://laravel-news.com/laravel-auto-increment
You can use 2 methods
By Statement
DB::statement("ALTER TABLE your_table_here SET AUTO_INCREMENT = 9999;");
By inserting row and deleteing it.
DB::table('your_table_here ')->insert(['id' => 99999, ... ,'column' => 'value']);
DB::table('your_table_here ')->where('id', 99999)->delete();
Hope this helps
If you want your first ID in the table to be for example 10001.
If you are using Laravel 8.x
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id')->from(10001);
.
.
$table->timestamps();
});
}
add a record with id (desired id -1) and then delete it.
If you add a record with id 999, and then delete it, next record will have id 1000. You can also use SQL identity on your database
After Creating Migrations Just Go to your Mysql Database and put this query
ALTER TABLE users AUTO_INCREMENT = 1000;
try this ?
$table->increments('id')->start_from(10000);
migration file
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longText('content');
$table->string('short_description');
$table->unsignedInteger('media_id')->nullable();
$table->foreign('media_id')->references('id')->on('medias');
$table->unsignedInteger('creator_id');
$table->foreign('creator_id')->references('id')->on('users');
$table->boolean('hide')->default(0);
$table->timestamps();
});
}
after hide column i want to add 'privacy column'
Either add it to the migration file or create another migration like this :
Schema::table('posts', function (Blueprint $table) {
$table->string('privacy')->after('hide');
});
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
I am writing a code to be scheduled to run occasionally. I want to remove records of users who have their details on the users table but does not have their records in the profiles table as well. I am thinking about removing the by their id, but I am not sure how I can make the match. profiles migration:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('gender',5);
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); // When a profile is deleted- his corresponding associated id details are deleted as well.
$table->tinyInteger('age')->unsigned()->nullable();
$table->string('goals')->nullable();;
$table->string('activityType')->nullable();
$table->rememberToken(); // for remember me feature.
});
}
Users migration:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username',20)->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
I have started to write the query, yet I am not sure how to proceed.
public function handle()
{
// type the query to get users that have no data in their profiles.
$empty_profile=App\Profile:::where('id','')->get;
$user_id=App\User::where('id')
}
Appreciate your help. Thanks.
Although your example is not exactly clear to me (and I wonder why there would be users without profiles in the first place?) I think this is what you need:
public function handle()
{
$empty_profiles = App\User::doesntHave('profile')->delete();
}
This will work if the models are correctly connected see also:
https://laravel.com/docs/master/eloquent-relationships