如何使用正确的外键插入数据?

时间:2014-01-03 22:23:29

标签: php laravel-4 relationship eloquent

我有一个名为Answers的表,其中有一个名为“questions_id”的外键,引用了Questionsid列。

如何使用正确的外键将答案插入Answers表?

Questions.php

class Questions extends Eloquent
{
    protected $table = 'questions';

    public function answers()
    {
        return $this->hasMany('Answers');
    }
}

Answers.php

class Answers extends Eloquent
{
    protected $table = 'answers';
}

Controller.php这样

public function store()
{

    $Question = new Questions;
    $Question->question     = Input::get('question'); 
    $Question->timestamps   = false;
    $Question->save();


    $Answer = new Answers;
    $Answer->answer       = Input::get('answer');
    $Answer->timestamps = false;
    $Answer->save();
*/          // redirect
    Session::flash('message', 'Successfully created nerd!');
    return Redirect::action('Controller@index');

}

Form.blade.php

{{ Form::open(array('action' => 'Controller@index')) }}
    <div class="form-group">
       {{ Form::label('question', 'Question') }}
       {{ Form::text('question', Input::old('question'), array('class' => 'form-control')) }}
    </div> 
    <div class="form-group">
       {{ Form::label('answer', 'Answer') }}
       {{ Form::text('answer', Input::old('answer'), array('class' => 'form-control')) }}
    </div>
    {{ Form::submit('Create ', array('class' => 'btn btn-primary')) }}    
{{ Form::close() }}

的MySQL&GT;描述问题;

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(255)     | NO   | PRI | NULL    | auto_increment |
| question | varchar(255) | NO   |     | NULL    |                |
| type     | char(1)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

的MySQL&GT; DESCRIBE答案;

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(255)     | NO   | PRI | NULL    | auto_increment |
| answer       | varchar(255) | NO   |     | NULL    |                |
| questions_id | int(255)     | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

questions_id是引用问题的外键(id)

我在laravel上找到了一些文档,但我无法弄清楚如何在输入中使用它。

3 个答案:

答案 0 :(得分:2)

  • 要准确,您应该发布数据库架构。
  • 您不应该为您的型号名称使用复数。
  • 查看我对你发表评论的错字。
  • 为什么questionanswer相同 形式/控制器?
  • 如果你在db模式中有timestamps,我的意思是 迁移中$table->timestamps(),您无需分配 他们,他们会自动获得价值。
  • 对于命名约定,请使用小写变量名。


$answer = new Answers(['answer' => Input::get('answer')]);
$question = Questions::create(['question' => Input::get('question')]);
$question->answers()->save($answer);

否则请尝试以下操作,但上面的应该有效。

$question = Questions::create(['question' => Input::get('question')]);
$answer   = Answers::create(['answer' => Input::get('answer'),
                             'questions_id' => $question->id]);

最好定义答案的反向关系

public function question()
{
    return $this->belongsTo('Questions');
}

答案 1 :(得分:2)

很简单。我正在使用你的代码。

public function store()
{

    $Question = new Questions;
    $Question->question     = Input::get('question'); 
    $Question->save();


    $Answer = new Answers;
    $Answer->answer       = Input::get('answer');

    // Use the recently created Question id    
    $Answer->questions_id = $Question->id;

    $Answer->save();

    Session::flash('message', 'Successfully created nerd!');
    return Redirect::action('Controller@index');

}

我删除了$Answer->timestamps = false;,因为这可以在您的模型中完成,而您的表也没有任何timestamps列。

public $timestamps = false;添加到您的模型中。

<强> Questions.php

class Questions extends Eloquent
{
    protected $table = 'questions';
    public $timestamps = false;

    public function answers()
    {
        return $this->hasMany('Answers');
    }
}

<强> Answers.php

class Answers extends Eloquent
{
    protected $table = 'answers';
    public $timestamps = false;
}

答案 2 :(得分:-1)

我还没有看到的一个选项是使用模型上定义的关系。请参阅Laravel文档。 http://laravel.com/docs/eloquent#inserting-related-models

$answer = new Answer();
$answer->answer = Input::get('answer');
$question->answers()->save($answer);