【问题标题】:Complete Newbie at Laravel, wondering if I'm using models correctly在 Laravel 完成新手,想知道我是否正确使用模型
【发布时间】:2014-04-30 20:47:57
【问题描述】:

所以我有三个模型,“不完整”、“用户”和“协作”。它们与外键相关:

Collaboration->incomplete_id
incomplete_id->Incomplete
Incomplete->user_id
user_id->User

如果我想获取协作模型的用户的电子邮件,我有以下代码

User::find(Incomplete::find($collab->incomplete_id)->user_id)->email);

我觉得这是错误的,因为我没有加入任何表,但我不想突破 mvc 并从控制器直接调用 SQL。基本上我很好奇我怎么能正确地做到这一点。

协作模式

class Collaboration extends Eloquent{
//Set the database to connect to as form d
protected $connection = 'formd';

//Set the table for the model to incomplets
protected $table = 'collaborations';

//Set the fillable columns
protected $fillable = array('incompid', 'link', 'shareFlag');


private $rules = array(
    'link'=>'required|unique:collaborations|size:56',
    'incompid'=>'required|integer'

);

private $errors;

public function validate($data)
{
    // make a new validator object
    $v = Validator::make($data, $this->rules);

    // check for failure
    if ($v->fails())
    {
        // set errors and return false
        $this->errors = $v->errors();
        return false;
    }

    // validation pass
    return true;
}

public function errors()
{
    return $this->errors;
}

public function getId(){
    return $this->getKey();
}

public function incomplete(){
    return $this->hasOne('Incomplete');
}   

}

模型不完整

class Incomplete extends Eloquent{
//Set the database to connect to as form d
protected $connection = 'formd';

//Set the table for the model to incomplets
protected $table = 'incompletes';

//Set the fillable columns
protected $fillable = array('name', 'data', 'userid');


private $rules = array(
    'name' => 'required|min:3',
    'data' => 'required'

);

  private $errors;

public function validate($data)
{
    // make a new validator object
    $v = Validator::make($data, $this->rules);

    // check for failure
    if ($v->fails())
    {
        // set errors and return false
        $this->errors = $v->errors();
        return false;
    }

    // validation pass
    return true;
}

public function errors()
{
    return $this->errors;
}

public function getId(){
    return $this->getKey();
}

public function getData(){
    return $this->data;
}

public function getName(){
    return $this->name;
}


public function user(){
    return $this->hasOne('User');
}   

}

【问题讨论】:

    标签: php sql laravel eloquent


    【解决方案1】:

    您可以使用 Eloquents 关系: http://laravel.com/docs/eloquent#relationships

    class Collaboration extends Eloquent {
    
        public function incomplete()
        {
            return $this->hasOne('Incomplete', 'incomplete_id', 'id');
        }
    
    }
    

    然后您可以通过执行以下操作从不完整记录中获取数据:

    $collab->incomplete->user_id
    

    【讨论】:

    • 我的模型中有函数,但是当我尝试运行 $collab->incomplete()->user_id 时,它给了我以下错误 Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo: :$user_id
    • @okawei 邮政编码,请。另外,请确保您返回 $this->hasOne() - 我犯了这个错误,我花了很长时间才意识到。
    • @okawei 改为:$collab->incomplete->user_id。调用不完整()返回关系而不是结果对象。要按照您尝试的方式进行操作,您必须这样做 $collab->incomplete()->first()->user_id
    • @ErinDrummond 刚注意到,正要说同样的话:)
    • 知道了! @Jono20201 我确实必须切换 hasOne 参数,所以它是 hasOne('Incomplete', 'id', 'incomplete_id') 感谢您的帮助!
    【解决方案2】:

    在这种情况下,请在模型上使用关系:

    class Collaboration extends Eloquent {
    
    public function incomplete()
    {
        return $this->belongsTo('Incomplete', 'incomplete_id');
    }}
    

    class Incomplete extends Eloquent {
    
    public function user()
    {
        return $this->belongsTo('User', 'user_id');
    }}
    

    然后,这样做:

    Collaboration::find($id)->incomplete()->user()->email;
    

    【讨论】:

      【解决方案3】:

      首先您必须更新模型类的部分内容

          Class Collaboration extends Eloquent{
            protected function incomplete(){
              return $this->belongsTo('Incomplete');
            } 
          }
          Class Incomplete extends Eloquent{
            protected function collaboration(){
              return $this->hasOne('Collaboration');
            }
            protected function user(){
              return $this->belongsTo('User');
            }
          }
          Class User extends Eloquent(){
            protected function incomplete(){
              return $this->hasOne('Incomplete');
            }
          }
      

      那么为了得到你想要的,这里是查询

      Collaboration::find($id)->incomplete()->user()->email;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-23
        相关资源
        最近更新 更多