简体   繁体   English

Laravel雄辩-多对一关系

[英]Laravel Eloquent - Many to One Relation

I have Models: ArtObjects and Photos: 我有模型:ArtObjects和照片:

class Photo extends Model
{
    protected $fillable = ['caption','description','alternative_text'];

    public function artObject()
    {
        return $this->belongsTo('App\ArtObject');
    }
}

class ArtObject extends Model
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'description',
        'rating',
        'popularity',
        'type',
        'price'
    ];

    public function photos()
    {
        return $this->hasMany(ArtObjectPhoto::class);
    }
}

Controllers: 控制器:

ArtObject Controller: ArtObject控制器:

public function store(ArtObjectUploadRequest $request)
{
    $art_object = ArtObject::create(request(['title', 'description', 'rating', 'popularity', 'type', 'price']));

    $this->validate($request, [
        'title' => 'required',
        'description' => 'required'
    ]);

    foreach ($photo_ids = Input::get('photos') as $photo_id) {

        $photo = Photo::find($photo_id);

        /*
        Problem is here - The user wants to attach the selected photos with
        the art-object, ........ Please advise, thanks in anticipation !!!
        */  

    }

    //save the artobject to the database
    $art_object->save();

    //And redirect to the home page
    return redirect('/');
}

Problem: The user wants to attach the selected photos with the art-object. 问题:用户希望将所选照片附加在艺术品上。 Note that the photos already exists in the db. 请注意,照片已经存在于数据库中。 I have tried options - save(), associate () but nothing helped. 我尝试了选项-save(),associate(),但没有任何帮助。 My understanding is once I find () the photo it should give me Photo object which I should be able to save () with $art_object. 我的理解是,一旦我找到()照片,它就会给我Photo对象,我应该能够使用$ art_object保存()。 It wants me to new() and assign from DB and assign to the Photo object. 它要我new()并从数据库分配并分配给Photo对象。 But I don't think this is the right way of doing this. 但是我认为这不是正确的方法。 I believe this is not the best way of implementing the many-to-relation , then what is the best way saving this relation. 我认为这不是实现多对关系的最佳方法,那么保存这种关系的最佳方法是什么。 Please advise, thanks in anticipation !!! 请指教,谢谢期待!!!

According to Many to One Relationship Rule in Database,the Foreign key connecting the tables is always saved in the table which has "many" relation. 根据数据库中的多对一关系规则,连接表的外键始终保存在具有“许多”关系的表中。

Like here,One ArtObject can have many Photos.So,That "Many" table is Photos. 像这里一样,一个ArtObject可以包含许多Photos。因此,“很多”表就是Photos。 Your Photo Model must have an attribute named art_object_id as a Foreign key. 您的照片模型必须具有名为art_object_id的属性作为外键。

Then you have to save that ArtObject object first and save this object's id in the photos table in all those rows whose id is selected by the user. 然后,您必须先保存该ArtObject对象,并将该对象的ID保存在photos表中用户选择了ID的所有那些行中。

$art_object = ArtObject::create(request(['title', 'description', 'rating', 'popularity', 'type', 'price']));

$this->validate($request, [
    'title' => 'required',
    'description' => 'required'
]);

 //save the artobject to the database
$art_object->save();

foreach ($photo_ids = Input::get('photos') as $photo_id) {

    $photo = Photo::find($photo_id);
    $photo->art_object_id = $art_object->id;
    $photo->save();


 }

After doing this ,you can fetch the related ArtObject of a photo by the method you defined in the Photo model to relate ArtObject and Photo tables together.You can also fetch the photos related to an ArtObject by the defined method in ArtObject. 完成此操作后,您可以通过在Photo模型中定义的方法来获取照片的相关ArtObject,以将ArtObject和Photo表关联在一起。还可以通过ArtObject中定义的方法来获取与ArtObject相关的照片。

In ArtObject Model:- 在ArtObject Model中:

 public function photos()
{
    return $this->hasMany('App\Photo');
}

In Photo Model:- 在照片模型中:-

 public function artObject()
{
    return $this->belongsTo('App\ArtObject');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM