1

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

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- 名称 我在消息上方得到了这个

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

  1. 从此模型中删除与该模型中具有该关系的其他模型之间的所有关系
  2. 删除了 insertNew() 中的验证步骤
  3. 删除了所有 Input 类调用并改用文字数据。

请注意,我在其他模型上使用了类似的插入功能,它完美无缺

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

解决方案

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

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

那是因为我忘记了

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

1 回答 1

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

解决了。

于 2013-10-04T20:23:19.177 回答