为了账号安全,请及时绑定邮箱和手机立即绑定

多对多(多态)使用同一个模型不同类型

多对多(多态)使用同一个模型不同类型

PHP
ITMISS 2022-01-02 15:54:20
我在数据库中有这 3 个表:我正在使用多对多(多态)雄辩关系来连接模型。的问题是,所述Creadores表可以是类型artista或autor在Creaciones表中。是否可以告诉 Eloquent 何时使用artista或autor?如果我将Creador模型扩展到其他 2 个模型,它会起作用:Artista和Autor. 但是,当我想显示所有creacionesA的creador使用Creador模式,因为多态关系与扩展模型创建是不可能的。Libro 型号:<?phpnamespace App;use Illuminate\Database\Eloquent\Model;use ChrisKonnertz\BBCode\BBCode;class Libro extends Model{    protected $table = 'Libros';    // Return all the artists of the book    public function artistas()    {        return $this->morphedByMany('App\Creador', 'creador', 'creaciones');    }    // Return all the authors of the book    public function autores()    {        return $this->morphedByMany('App\Creador', 'creador', 'creaciones');    }}克雷多型号:<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Creador extends Model{    protected $table = 'creators';    // Return all the books where author    public function autorLibros()    {        return $this->morphToMany('App\Libro', 'creador', 'creaciones');    }    // Return all the books where artist    public function artistaLibros()    {        return $this->morphToMany('App\Libro', 'creador', 'creaciones');    }}
查看完整描述

2 回答

?
慕后森

TA贡献1802条经验 获得超5个赞

您最好在其中添加一个type属性Creadorwith 'artista'/ 'autor'。


多态关系只能采用单一模型。所以你的代码会变成:


public function creadors()

{

    // Return a general relation for all 'creadores'.

    return $this->morphedByMany(App\Creador::class, 'creador', 'creaciones');

}


public function artistas()

{

    // Filter for 'artista's.

    return $this->creadors()->where('type', 'artista');

}


public function autores()

{

    // Filter for 'autor's.

    return $this->creadors()->where('type', 'autor');

}


查看完整回答
反对 回复 2022-01-02
?
阿晨1998

TA贡献2037条经验 获得超6个赞

用下面的方法解决了。将关系从多态多对多更改为普通多对多,添加withPivot和wherePivot。


克里多尔模型


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Creador extends Model

{

    protected $table = 'creators';


    public function libros()

    {

        return $this->belongsToMany('App\Libro', 'creaciones')->withPivot('creador_type');

    }


    // All books as an Artist

    public function librosArtista()

    {

        return $this->libros()->wherePivot('creador_type', 1);

    }


    // All books as an Author

    public function librosAutor()

    {

        return $this->libros()->wherePivot('creador_type', 2);

    }

}

Libro 模型


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;

use ChrisKonnertz\BBCode\BBCode;


class Libro extends Model

{

    protected $table = 'libros';


    public function creador()

    {

        return $this->belongsToMany('App\Creador', 'creaciones')->withPivot('creador_type');

    }


    // All book artists

    public function artistas()

    {

        return $this->creador()->wherePivot('creador_type', 1);

    }


    // All book authors

    public function autores()

    {

        return $this->creador()->wherePivot('creador_type', 2);

    }

}

当创建一个附加Creador到 a 时Libro:


$libro->artistas()->attach( $creador, [

    'creador_type' => 1

]);


查看完整回答
反对 回复 2022-01-02
  • 2 回答
  • 0 关注
  • 164 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号