The Eloquent model encapsulated the attributes of an object. Thus, you can only modify or set them using the set ( $model->attribute ) or fill(array $attributes) methods. All Eloquent methods that assign mass to attributes use the fill() method.
So, there are two ways to insert new Eloquent-based models with an array in the database that uses the fill() method and one that doesn't work.
Method 1: Mass Assignment
Add the attribute protected $fillable = [ 'column_a', 'column_b', .. ]; to your model. Then you can use bulk assignment as follows.
$Plan = new Plans; $Plan->fill( $array ); $Plan->save();
Or you can use:
$plan = Plans::create( $array );
Method 2: QueryBuilder :: insert (array)
If you don't have boot methods registered with creating and created (DB will not run them), you can also use QueryBuilder to insert rows into the database.
// insert one plan DB::table('plans')->insert([ $array ]); $plan = Plans::find( DB::getPdo()->lastInsertId() ); // insert multiple plans in one query $plans = [ $plan_a_arr, $plan_b_arr, .. ]; DB::table('plans')->insert( $plans );
source share