محمد
5 سال پیش توسط محمد مطرح شد
2 پاسخ

مشکل در ارتباط many to many

من دو جدول course و article دارم که ارتباط many to many با category داره و همینطور میخام این جداول ارتباط many to many با جدول Tag داشته باشه اما ارور دارم.

 Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `e3ddata`.`#sql-6c0_f` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `article_tag` add constraint `article_tag_tag_id_foreign` foreign key (`tag_id`) references `tags` (`id`) on delete cascade on update cascade)

ثبت پرسش جدید
علی بیات
تخصص : توسعه دهنده ارشد وب
@ali.bayat 5 سال پیش مطرح شد
1

برای استفاده از روابط چند شکلی یا پلی‌مورفیک در لاراول باید مراحل زیر رو دنبال کنید.

ساختار جدول‌های دیتابیس:
courses
    id - integer
    name - string

articles
    id - integer
    name - string

tags
    id - integer
    name - string

taggables
    tag_id - integer
    taggable_id - integer
    taggable_type - string
ساختار مدل‌ها:

مدل‌های Article و Course هر دو یک متد tags دارند که رابطه morphToMany رو از کلاس پایه الکوئنت فراخوانی میکنه

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    // Get all of the tags for the article.

    public function tags()
    {
        return $this->morphToMany('App\Tag', 'taggable');
    }
}
رابطه عکس در مدل Tag:
<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    // Get all of the articles that are assigned this tag.

    public function articles()
    {
        return $this->morphedByMany('App\Article', 'taggable');
    }

    // Get all of the courses that are assigned this tag.

    public function courses()
    {
        return $this->morphedByMany('App\Course', 'taggable');
    }
}
بازیابی رابطه:
$course = App\Course::find(1);

foreach ($course->tags as $tag) {
    //
}

و

$tag = App\Tag::find(1);

foreach ($tag->articles as $article) {
    //
}

مهدی طهرانی
تخصص : cloud architect
@mtehrani 5 سال پیش مطرح شد
0

@manvfx
سلام
دوست عزیز شما باید از روابط many to many polymorphic استفاده کنید از لینک زیر میتونید کمک بگیرید
https://laravel.com/docs/master/eloquent-relationships#many-to-many-polymorphic-relations


علی بیات
تخصص : توسعه دهنده ارشد وب
@ali.bayat 5 سال پیش مطرح شد
1

برای استفاده از روابط چند شکلی یا پلی‌مورفیک در لاراول باید مراحل زیر رو دنبال کنید.

ساختار جدول‌های دیتابیس:
courses
    id - integer
    name - string

articles
    id - integer
    name - string

tags
    id - integer
    name - string

taggables
    tag_id - integer
    taggable_id - integer
    taggable_type - string
ساختار مدل‌ها:

مدل‌های Article و Course هر دو یک متد tags دارند که رابطه morphToMany رو از کلاس پایه الکوئنت فراخوانی میکنه

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    // Get all of the tags for the article.

    public function tags()
    {
        return $this->morphToMany('App\Tag', 'taggable');
    }
}
رابطه عکس در مدل Tag:
<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    // Get all of the articles that are assigned this tag.

    public function articles()
    {
        return $this->morphedByMany('App\Article', 'taggable');
    }

    // Get all of the courses that are assigned this tag.

    public function courses()
    {
        return $this->morphedByMany('App\Course', 'taggable');
    }
}
بازیابی رابطه:
$course = App\Course::find(1);

foreach ($course->tags as $tag) {
    //
}

و

$tag = App\Tag::find(1);

foreach ($tag->articles as $article) {
    //
}

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام