Model.php第232行中的MassAssignmentException

All*_*din 1 php laravel eloquent laravel-5.4

我有以下总线模型:

class Bus extends Model
{
    protected $guarded =[''];
    protected $fillable = ['plate_num','seat_num'];

    public function seats(){

        return $this->hasMany(Seat::class);
    }

    public function addSeat($number){

        $this->seats()->create([
            'seat_num'=> $number

            ]);
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的BusController中,我具有以下存储函数,该函数从一个表单(“ plate_num”和“ seats_num”)获取2个值

尽管我具有$ fillable数组,其中包含所需的字段(Seat @ seat_numBus @ plate_num),但是,每当我提交表单时,都会出现以下错误:

Model.php行232 Seat_num中的MassAssignmentException

该代码的逻辑基本上是,每当我键入公交车牌号座位数(例如“ ABC213”“ 5”)时,应在包含table_num的公交车表中创建一个记录,并在其中创建5条记录。 Seats表,其中每个记录都会获取公交车的ID和座位号。

Sid*_*art 5

编辑模型

protected $fillable = ['plate_num'];
Run Code Online (Sandbox Code Playgroud)

添加您的座位模型

protected $fillable = ['seat_num'];
Run Code Online (Sandbox Code Playgroud)

现在您可以在控制器中使用添加

use App\seat;
use App\bus;
public function add(){
$seat = new seat;
$bus = new bus;
Run Code Online (Sandbox Code Playgroud)