Having problems with Laravel Migrations - laravel

I have added all my in constraints 2020_06_17_221942_create_constraints_table
but its not working
All migrations successfully done but without any table constraints the 2020_06_17_221942_create_constraints_table is :
Schema::table('seasons', function (Blueprint $table) {
$table->unsignedBigInteger('best_project_id');
$table->unsignedBigInteger('manager_id');
$table->foreign('best_project_id')->references('id')->on('projects');
$table->foreign('manager_id')->references('id')->on('users');
});
Schema::table('admins', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onDelete('cascade');;
});
Schema::table('projects', function (Blueprint $table) {
$table->foreign('study_major_id')->references('id')->on('study_majors');
$table->foreign('user_id')->references('id')->on('Users');
$table->foreign('season_id')->references('id')->on('Seasons');
})

Try adding "Blueprint" type-hinting to the migration function. It should work
For example:
Schema::table('seasons', function (Blueprint $table) {
$table->unsignedBigInteger('best_project_id');
$table->unsignedBigInteger('manager_id');
$table->foreign('best_project_id')->references('id')->on('projects');
$table->foreign('manager_id')->references('id')->on('users');
});

Replace your last query with this
Schema::table('projects', function (Blueprint $table) {
$table->unsignedBigInteger('study_major_id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('season_id');
$table->foreign('study_major_id')->references('id')->on('study_majors');
$table->foreign('user_id')->references('id')->on('Users');
$table->foreign('season_id')->references('id')->on('Seasons');
});

Related

eloquent relation shows null

I have migrations for Users and Projects like this:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->bigInteger('last_used_society_id');
$table->bigInteger('last_used_project_id');
$table->rememberToken();
$table->timestamps();
});
and
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('eplan_name')->nullable();
$table->bigInteger('society_id');
$table->timestamps();
//$table->foreign('user_id')->references('id')->on('users');
});
Schema::table('projects', function (Blueprint $table) {
$table->foreign('society_id')->references('id')->on('societies');
});
table Users is altered after to add foreigns:
Schema::table('users', function (Blueprint $table) {
$table->foreign('last_used_society_id')->references('id')->on('societies');
$table->foreign('last_used_project_id')->references('id')->on('projects');
});
and in model of User i have this:
public function ActualProject(){
return $this->belongsTo('App\Models\Project', 'users_last_used_project_id_foreign');
}
public function ActualSociety(){
return $this->belongsTo('App\Models\Society', 'users_last_used_society_id_foreign');
}
but when i try to call $user->ActualProject it return null
why using 'users_last_used_project_id_foreign'?
just use the column's name as it is ...
public function ActualProject(){
return $this->belongsTo('App\Models\Project', 'last_used_project_id');
}

how to add a new column between 2 columns in laravel

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');
});

Laravel through relations

I have multi level relation in my app
Category
|products
|product_items
|product_group
|product_attributes
Category Model
public function products(){
return $this->belongsToMany(Product::class);
}
Product Model
public function items()
{
return $this->hasMany(ProductItem2::class);
}
Item Model
public function attribute()
{
return $this->belongsToMany(AttributeValue::class,'product_groups','item_id','attribute_id')->withTimestamps();
}
Attribute value
public function attributeName(){
return $this->belongsTo(Attribute::class,'attribute_id');
}
all relations works fine but now I need filter my product by example 'colors'
so I tried
return $this->where('parent_id',0)->whereHas('childrenRecursive')->with('products.items.attribute.attributeName')->get();
put of course it return all categories with all product and product items etc
here is the migration files
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('image');
$table->string('cover')->nullable();
$table->string('url')->nullable();
$table->string('cover');
$table->text('url_cover');
$table->text('description')->nullabel();
$table->unsignedInteger('parent_id');
$table->boolean('elite')->default(0);
$table->timestamps();
});
Schema::create('category_product',function (Blueprint $table){
$table->integer('category_id');
$table->integer('product_id');
$table->primary(['product_id','category_id']);
});
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->string('serial_number');
$table->float('price');
$table->integer('discount');
$table->enum('type',['pound','percentage']);
$table->timestamp('start')->nullable();
$table->timestamp('end')->nullable();
$table->string('image');
$table->unsignedInteger('shop_id');
$table->boolean('recommendation');
$table->boolean('published');
$table->integer('preparing_days')->unsigned();
$table->integer('visits')->defualt(0);
$table->unsignedInteger('priorty')->defualt(0);
$table->timestamps();
});
Schema::create('product_items', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('product_id');
$table->double('price');
$table->string('image');
$table->unsignedInteger('amount');
$table->timestamps();
});
Schema::create('product_groups', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('attribute_id');
$table->unsignedInteger('item_id');
$table->timestamps();
});
Schema::create('attribute_values', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('attribute_id');
$table->string('value');
$table->timestamps();
});
Schema::create('attributes', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
all i need to return the attribute 'color' values like ['red,blue,black'] with each category
seems complicated and need help.

Laravel relationship error in migration

I am trying to make two tables relationship in laravel. But i can't understand why i'm getting error "errno: 150 "Foreign key constraint is incorrectly formed"". please check my migration code below.
items table
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->string('name');
$table->decimal('price',5,2);
$table->integer('count_left')->default(0);
$table->timestamps();
$table->foreign('category_id')->references('id')->on('categories');
});
}
categories
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
}

update table column without reseting the database migrate Laravel

I want to add new column or delete column to a table in laravel migration and I don't want to reset the database because it delete all old data.
make a new migration then the code should be like
Schema::table('users', function (Blueprint $table) {
$table->string('name', 50)->nullable()->change();
});
or if you want to rename
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});
or if dropping
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['votes', 'avatar', 'location']);
});
to add simply
Schema::table('users', function (Blueprint $table) {
$table->string('email');
});

Resources