Laravel 在模型中设置表名?

Laravel 在模型中设置表名?,laravel,laravel-4,Laravel,Laravel 4,我试图将一个表名传递给我的模型,因为模型在两个表上运行,但方法相同 我是这样做的: $this->model = new Emotions(array('section' => 'red')); 在模型中,我将表格设置为: public function __construct($attributes = array(), $exists = false){ parent::__construct($attributes, $exists); $this->

我试图将一个表名传递给我的模型,因为模型在两个表上运行,但方法相同

我是这样做的:

$this->model = new Emotions(array('section' => 'red'));
在模型中,我将表格设置为:

public function __construct($attributes = array(), $exists = false){

    parent::__construct($attributes, $exists);
    $this->table = $attributes['section'];
}
但我得到了一个错误:

Undefined index: section

你知道我哪里做错了吗?

在你的课堂上增加下面的一行

protected $fillable = array('section');

是的,我明白了,这门课可能会上两次

请试试这个

public function __construct($attributes = array(), $exists = false){

    parent::__construct($attributes, $exists);

    if(isset($attributes['section'])) {
        $this->table = $attributes['section'];
    }
}
我个人的建议

<?php

class Emotions extends Eloquent
{
    public function setTableName($name)
    {
        $this->table = $name;
        return $this;
    }
}

奇怪。我已经测试过了,你的代码运行得很好。您是否尝试传递非关联数组以查看是否有效?传递数组(“红色”)并查看是否可以作为属性[0]访问它。使用不同的字符集有什么变化吗?没有-未定义的偏移量:0问题是,我可以将$attributes['section']转储出来,并给出正确的输出,只有当我尝试设置$table时,它才会失败。这与问题无关。
$emotion = new Emotions(array('foo' => 'bar'))
    ->setTableName('blabla')
    ->save();