无法制作新的插入 - Laravel Eloquent ORM

时间:2013-07-29 03:20:16

标签: laravel laravel-4

我无法插入此表,这让我发疯 这是我得到的错误

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

}

我只是想插入一个包含三列的新行(我在Student的实例上调用insertNew函数) 1- ID自动递增 2-特典 3-名字 而我在Msg上面得到了这个

到目前为止我尝试了什么:

  1. 删除此模型与其他模型之间的所有关系 在关系
  2. 中有这个
  3. 删除了insertNew()
  4. 中的验证步骤
  5. 删除了所有Input类调用并改为使用了文字数据。
  6. 请注意,我在其他模型上使用类似的插入功能,它可以完美地运行

    任何评论,回复表示赞赏:D

    解决方案

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

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

    这是因为我忘记了

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

1 个答案:

答案 0 :(得分: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'));"

解决了它。