Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 他有很多经历_Php_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Php 他有很多经历

Php 他有很多经历,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,一个场馆有许多订阅 订阅有许多订阅者(用户) 有一个透视表,包含用户id和订阅id之间的关系 如何从场馆获取所有订阅者 我试过: class Venue { /** * Members */ public function members() { return $this->hasManyThrough('App\User', 'App\Subscription'); } } 但它失败了,错误是: S

一个
场馆
有许多
订阅

订阅
有许多
订阅者
用户

有一个透视表,包含用户id和订阅id之间的关系

如何从
场馆
获取所有
订阅者

我试过:

class Venue {
    /**
     * Members
     */

        public function members() {
            return $this->hasManyThrough('App\User', 'App\Subscription');
        }
}
但它失败了,错误是:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.subscription_id' in 'on clause' (SQL: select `users`.*, `sub
    scriptions`.`venue_id` from `users` inner join `subscriptions` on `subscriptions`.`id` = `users`.`subscription_id` where `
    users`.`deleted_at` is null and `subscriptions`.`venue_id` = 1)
我的订阅模式的外观:

`Subscription`

class Subscription extends Model {

    protected $table = 'subscriptions';

    /**
     * Subscripers
     */

        public function subscribers() {
            return $this->belongsToMany('App\User');
        }

    /**
     * Venue
     */

        public function venue() {
            return $this->belongsTo('Venue');
        }

}

简单的问题:为什么要使用第三种模式进行
订阅
?这听起来像是
用户
场馆
之间的正常n:m关系,如上面的评论中所述

class User {
    public function venues() {
        return $this->belongsToMany('App\Venue');
    }
}

class Venue {
    public function users() {
        return $this->belongsToMany('App\User');
    }
}
这个星座实际上需要三个表,它们是(我给每个模型一列
名称
):

但要了解这些关系,你可以简单地使用雄辩的魔法:

// List of all venues (as Venue models) that are in relation with User with id $id
$venues = User::find($id)->venues()->get();

// Returns the alphabetically first user that has a relation with Venue with id $id
$user = Venue::find($id)->users()->orderBy('name', 'asc')->first();
如果需要在数据透视表中存储其他信息(例如,当关系建立时),可以使用其他数据透视字段:

user_venue
  - user_id
  - venue_id
  - created_at

class User {
    public function venues() {
        return $this->belongsToMany('App\Venue')->withPivot('created_at');
    }
}

class Venue {
    public function users() {
        return $this->belongsToMany('App\User')->withPivot('created_at');
    }
}

// Returns the date of the relations establishment for the alphabetically
// first Venue the User with id $id has a relation to
$created_at = User::find($id)->venues()->orderBy('name', 'asc')->first()->pivot->created_at;

我从来没有尝试过做任何你想做的事情,因为(根据目前的信息)在概念上似乎是错误的。我也不知道是否可以为pivot表设置自己的模型,但我认为如果pivot表有自己的主id列,它应该可以工作。如果您有第三个模型需要与其他两个模型连接,那么这可能会很有帮助,但通常不会发生这种情况。因此,请首先尝试使用pivot表,如上图所示


好的,我仍然没有看到一个好的用例,但是我可以为您提供一个有效的查询。不幸的是,我无法让一个雄辩的查询工作,但解决方案应该仍然是好的

class Venue {
    public function members($distinct = true) {
        $query = User::select('users.*')
            ->join('subscription_user', 'subscription_user.user_id', '=', 'users.id')
            ->join('subscriptions', 'subscriptions.id', '=', 'subscription_user.subscription_id')
            ->where('subscriptions.venue_id', '=', $this->id);
         if($distinct === true) {
             $query->distinct();
         }
         return $query;
     }
}
可以像正常情况一样查询关系:

Venue::find($id)->members()->get()
// or with duplicate members
Venue::find($id)->members(false)->get()

在您的应用程序中,什么是
订阅
?对我来说,听起来你想让
用户
可以订阅
场馆
,但这将是一种n:m关系,没有额外的
订阅
模式(不确定是否可以额外使用)。其他信息可以存储为pivot元素。我已经更新了我的答案。我有一个数据透视表,其中包含用户和订阅之间的关系。这是因为您的概念是错误的。您不需要子脚本模型,因为您可以在用户和场地之间直接进行n:m映射。查看多对多的文档。谢谢您的回答,但这不是我想要实现的。我不想要场馆用户关系,因为
用户
仅当
用户
场馆
订阅时才属于场馆。我的数据库关系:请提供关于订阅是什么、它的作用(或代表什么)以及它包含什么其他信息的更多信息。我得到了这个。但是你想达到什么目的呢?为什么一个场馆需要很多订阅,而每个订阅都需要很多用户?从技术上讲,这有什么意义呢?因为用户可以选择在同一地点进行多种不同的订阅。我需要一种方法来查看与场馆相关的所有用户。如果用户订阅了某个场馆,则该用户与该场馆相关。我再次更新了我的答案。请检查它是否适合你。
Venue::find($id)->members()->get()
// or with duplicate members
Venue::find($id)->members(false)->get()