在Laravel完成新手,想知道我是否正确使用模型

时间:2014-04-30 20:47:57

标签: php sql laravel eloquent

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

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

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

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');
}   

}

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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;

答案 2 :(得分:0)

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

    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;