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.
Related
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');
}
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');
});
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();
});
}
How would you go to create a relationship between the folowing tables:
Users table:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('surename');
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('country')->nullable();
$table->string('postcode')->nullable();
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Setting_type table:
Schema::create('setting_types', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Settings table:
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('value');
$table->timestamps();
});
Setting_type_user table:
Schema::create('setting_type_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('type_id')->unsigned();
$table->integer('setting_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('type_id')->references('id')->on('setting_types');
$table->foreign('setting_id')->references('id')->on('settings');
$table->timestamps();
});
This is the result I want to get:
{"id":1,"value":"578943205.jpg","created_at":"2017-07-18 00:00:00","updated_at":null,"pivot":{"setting_id":1,"user_id":1,"type_id":1}}
It depends on many things, but it looks like you want to use many-to-many relationship here.
First, in settings pibot table change it to:
$table->unsignedInteger('user_id');
$table->unsignedInteger('type_id');
It's also a good idea to add foreign key constraints to the table:
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('type_id')->references('id')->on('setting_types');
Then define belongsToMany() relationship in both User and SettingType models. Since you don't follow naming conventions, you have to define foreign keys manually. In the User model:
public function settingTypes()
{
return $this->belongsToMany('App\SettingType', 'settings', 'user_id', 'type_id');
}
And in the SettingType model:
public function users()
{
return $this->belongsToMany('App\User', 'settings', 'type_id', 'user_id');
}
I'm trying to create a ploymorphic many-to-many relationship that also includes an additional relationship. I can't seem to figure out how to get Eloquent to map the additional field to a model.
I have projects, users on those projects, and project roles that dictate permissions for that user on the project.
My tables:
Schema::create('projects', function(Blueprint $table){
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('name');
$table->integer('possessor_id');
$table->string('possessor_type');
});
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('email');
$table->string('name');
$table->string('username');
});
Schema::create('project_accessors', function(Blueprint $table){
$table->increments('id');
$table->timestamps();
$table->integer('project_id')->unsigned();
$table->foreign('project_id')->references('id')->on('projects');
$table->integer('project_role_id')->unsigned();
$table->foreign('project_role_id')->references('id')->on('project_roles');
$table->integer('entity_id')->unsigned();
$table->string('entity_type');
});
Schema::create('project_roles', function(Blueprint $table){
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('name');
$table->string('permissions');
$table->integer('project_id')->unsigned();
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
});
I define projects on users like this:
public function projects()
{
return $this->morphToMany('App\Project', 'entity', 'project_accessors')->withTimestamps()->withPivot('project_role_id');
}
I define users and roles on projects like this:
public function roles()
{
return $this->hasMany('App\ProjectRole');
}
public function users()
{
return $this->morphedByMany('App\User', 'entity', 'project_accessors')->withTimestamps()->withPivot('project_role_id');
}
Is there a way to map project_role_id to an actual instance of my Project Role model?