2013-07-29 19 views
1

我不能插入到这个表,这让我发疯 这是错误消息我得到不能让一个新的插入 - Laravel雄辩ORM

var_export does not handle circular references 
open: /var/www/frameworks/Scout/vendor/laravel/framework/src/Illuminate/Database/Connection.php 
    * @param Exception $e 
    * @param string  $query 
    * @param array  $bindings 
    * @return void 
    */ 
    protected function handleQueryException(\Exception $e, $query, $bindings) 
    { 
     $bindings = var_export($bindings, true);  
     $message = $e->getMessage()." (SQL: {$query}) (Bindings: {$bindings})"; 

这里是我的全模式

<?php 
namespace Models; 
use Illuminate\Database\Eloquent\Collection; 

    class Student extends \Eloquent 
{ 
/** 
* The database table used by the model. 
* 
* @var string 
*/ 

protected $table = 'students'; 


/** 
* The rules used to validate new Entry. 
* 
* @var array 
*/ 
protected $newValidationRules = array(
    'studentCode' => 'unique:students,code|numeric|required', 
    'studentName' => 'required|min:2', 
    'dateOfBirth' => 'date', 
    'mobile' => 'numeric' 
); 

/** 
* Relation with sessions (Many To Many Relation) 
* We added with Created_at to the Pivot table as it indicates the attendance time 
*/ 
public function sessions() 
{ 
    return $this->belongsToMany('Models\Session', 'student_session')->withPivot('created_at')->orderBy('created_at', 'ASC'); 
} 

/** 
* Get Student Subjects depending on attendance, 
*/ 
public function subjects() 
{ 
    $sessions = $this->sessions()->groupBy('subject_id')->get(); 
    $subjects = new Collection(); 
    foreach ($sessions as $session) { 
     $subject = $session->subject; 
     $subject->setRelation('student', $this); 
     $subjects->add($subject); 
    } 
    return $subjects; 
} 

/** 
* Insert New Subject 
* @return Boolean 
*/ 

public function insertNew() 
{ 
    $this->validator = \Validator::make(\Input::all(), $this->newValidationRules); 


    if ($this->validator->passes()) { 
     $this->name = \Input::get('studentName'); 
     $this->code = \Input::get('studentCode'); 
     if ($this->save()) { 
      return \Response::make("You have registered the subject successfully !"); 
     } else { 
      return \Response::make('An Error happened '); 
     } 
    } else { 
     Return $this->validator->messages()->first(); 
    } 
} 

}

我只是想插入新行有三列(我称之为insertNew功能对学生的实例) 1 ID自动增加 2-特典 3-名称 我得到这个消息以上

什么我都试过至今:

  1. 之间移除从这个模式与其他模式 所有关系有这一个在关系
  2. 删除insertNew()中的验证步骤
  3. 删除所有Input类调用并使用文字数据。

注意,我用其他型号类似的插入功能,它完美的作品

任何评论,回复是赞赏:d

解决方案

我解决了它,问题是,我正在访问验证程序

$this->validator = \Validator::make(\Input::all(), $this->newValidationRules); 

而这是因为我忘了

/** 
    * The validator object. 
    * 
    * @var Illuminate\Validation\Validator 
    */ 
    protected $validator; 

回答

0

我有类似的问题。但对我来说,改变这个代码:

if ($this->validator->passes()) { 
    $this->name = \Input::get('studentName'); 
    $this->code = \Input::get('studentCode');" 

这样:

if ($this->validator->passes()) { 
    $this->setAttribute ("name" , \Input::get('studentName')); 
    $this->setAttribute ("code" , \Input::get('studentCode'));" 

解决它。