8

Have array which have key & value. The keys name are same database columns name.

Total of keys in an index is greater than 50.

  • So,not possible to save data one by one key like

    $user = new User;
    
    $user->name = 'John';
    ..........
    .......
    .......so on
    
    $user->save();
    

Sample array:

Array
(
    [name] => 'Amit',
    [age] => 25,
    [field1] => 'val1',
    ....
  [field33] => 'how'    ,
....
   [field54] => 'sumit'     
)

Now, my question is that how can i save data in an model using easiest process.

Info:

  • Using laravel 5

Try:

$Plan=new Plans;
 $Plan->fill( $array);
 $Plan->save();

Note: I know that fill not working as it not defined at fillable variable of that model

3
  • 1
    Fill not working because the fillable variable is not defined? And why don't you define it? Commented Dec 22, 2015 at 8:41
  • Can you describe why you don't want to use the fillable attribute? Commented Dec 22, 2015 at 8:58
  • as there are lot colums that why i does not want to define fillable Commented Dec 22, 2015 at 9:44

2 Answers 2

5

The Eloquent model has encapsulated the attributes of the object. So you can only alter/set them through the set ($model->attribute) or the fill(array $attributes) method. All the Eloquent methods that mass assign the attributes is using the method fill().

So there are 2 ways to insert new Eloquent based models with an array in the database, one that uses the fill() method and one that doens't.

Method 1: Mass assignment

Add the attribute protected $fillable = [ 'column_a', 'column_b', .. ]; to your model. Then you can use the mass assignment like this.

$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 any boot methods registered on creating and created (DB won't trigger these) then you also can use the QueryBuilder to insert the rows in 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 );
Sign up to request clarification or add additional context in comments.

2 Comments

thank for u answer.I does not follow method.Is there any other process with out fill();
I'm sorry but I don't understand what you mean. The 2nd method is not using the fill() method.
3

I don't understand why you don't want to define the database columns/array keys in $fillable attribute of your model, as it is a just a one-time task and then you would be able to do this simply:

$plan = Plans::create( $array );

Another way, if you have all the data in an associative array with keys matching to the columns in DB table, which you do, is to use a foreach loop:

$plan = new Plans;
foreach( $array as $key => $value )
{
    $plan->$key = $value;
}
$plan->save();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.