【问题标题】:Laravel 4 belongsToMany Relationship Returns EmptyLaravel 4 belongsToMany 关系返回空
【发布时间】:2014-11-23 07:05:45
【问题描述】:

我正在使用 Laravel 4,并且正在努力建立多对多关系。这是我正在尝试做的一个例子。在这里,我试图在用户和组织之间建立多对多关系。

这是我的迁移文件,创建了一个用户表、一个组织表和一个数据透视表以在两者之间移动。

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('email');
        $table->string('password');
        $table->timestamps();
    });

    Schema::create('organizations', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });

    Schema::create('organization_user', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('organization_id')->unsigned()->index();
        $table->foreign('organization_id')->references('id')->on('organizations')->onDelete('cascade');
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->timestamps();
    });
}

我也使用了默认的 User 模型,并添加了 belongsToMany 关系。

    use Illuminate\Auth\UserTrait;
    use Illuminate\Auth\UserInterface;
    use Illuminate\Auth\Reminders\RemindableTrait;
    use Illuminate\Auth\Reminders\RemindableInterface;

    class User extends Eloquent implements UserInterface, RemindableInterface {

        use UserTrait, RemindableTrait;

        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'users';

        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = array('password', 'remember_token');

        public function organizations()
        {           
            return $this->belongsToMany('Organization');
        }

    }

我已经创建了一个组织模型,关系朝着相反的方向发展。

class Organization extends \Eloquent {
    protected $fillable = ['name'];

    public function users()
    {
        return $this->belongsToMany('User');
    }
}

问题是,如果我尝试使用 User::find(1)->organizations() 进行查询,当然在添加示例数据后,它总是以空数组的形式返回,反之亦然使用 Organization::find(1)->users() 的方向。奇怪的是,如果我尝试执行诸如 Organization::find(1)->users()->attach(1) 之类的操作,它会在数据透视表中添加适当的行,因此它知道存在关系。

关于为什么查询似乎不起作用的任何想法?

【问题讨论】:

    标签: php laravel many-to-many relationship


    【解决方案1】:

    这只是您访问关系的方式。请尝试执行以下操作:

    $organisations = User::find(1)->organisations;
    
    $users = Organisation::find(1)->users;
    

    如果您使用关系的方法版本,您也可以在查询中添加额外的内容。但请注意,您需要在其后面加上get() 才能真正执行查询。

    // The same as just accessing the property
    $organisations = User::find(1)->organisations()->get();
    
    // With extra clauses
    $organisations = User::find(1)->organisations()->where('created_at', '>=', '2010-01-01 00:00:00')->get();
    

    【讨论】:

    • 太棒了,谢谢!我知道这很简单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 2020-06-03
    • 2016-06-21
    • 2020-10-02
    • 2015-02-22
    • 1970-01-01
    • 2018-05-19
    相关资源
    最近更新 更多