laravel 5推进雄辩的多重选择

时间:2016-04-19 23:12:03

标签: laravel laravel-5 eloquent

我有一张包含多位艺术家的表格。在创建事件(更新事件表)时,我需要选择我想要在我的事件中执行的艺术家,并且需要在event_artist(传递)表(包含event_id& artist_id)中添加他们的ID。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以定义many to many relationship

在Artist模型类中:

    ...
        .directive(ionSlide, function(...) {
             return {
                 scope: {
                      itemId: '@',
                 },
                 .... // Other things
             };
        });

        // In your html you would then have
        ...
       <ion-slide item-id="{{item.id}}" ng-repeat="ite in allvodsforcategory">
                  <img ng-src="{{ ite.image }}">
                </ion-slide>
         ...

        // And in your controller,
        ...
        .controller(function($scope, ...){
            var id = $scope.itemId; // you have your id here.

            $http.get('http://api.com?user=' + $scope.apiusername + '&pass=' + $scope.apipassword + '&id=' id).success(function(data) {
       $scope.allvodsforcategory = data.videos;
     });
});

在事件模型类中:

public function events()
{
    $this->belongsToMany(Event::class);
}

这假设您的数据透视表名为public function artists() { $this->belongsToMany(Artist::class); } (其他两个表名按字母顺序组合)。否则,您可以将数据透视表名称作为第二个参数传递给belongsToMany:artist_event

然后,您可以使用belongsToMany(Event::class, 'event_artist') attach艺术家参加活动。或分离$event->artists()->attach($artist);。您还可以同步一系列艺术家:$event->artists()->detach($artist);。您还可以将事件附加,分离或同步到艺术家。这三种方法都可以接受Eloquent模型或ID。