1

I am new in Laravel. I want to insert data from an array into the database. My data is inserted into the database but when the data exist in the database, the same data uploads into the database. I want to insert only new data that is not in the database.

$str=$this->entry();
$end=$this->exit_entry();

$st=array_chunk($str,5);
$ex=array_chunk($end,5);
$result=array();
foreach($st as $v1){
    $tmp = array(
        'Employee_id'=>$v1[0],
        'Employee_name'=>$v1[1],
        'Date' => $v1[2],
        'Day' =>$v1[3],
        'Time1' => $v1[4]
    );

    foreach($ex as $v2){
        if($v2[1] == $v1[1] && $v2[0] == $v1[0] && $v2[2] == $v1[2] && $v2[3] == $v1[3]  ){
            $tmp['Time2']=$v2[4];
        }
    }
    $result[] =   $tmp;
}

foreach($result as $res) {
    $res1 = EmployeeList::insert($res);
}

//var_dump($res1);
Pang
  • 9,564
  • 146
  • 81
  • 122
Alien
  • 724
  • 1
  • 8
  • 24

2 Answers2

0

I think so that your question is update if the data already exists. So this is what you can do:

$res1 = EmployeeList::firstOrCreate($res);

But this works only if your Employee_id is set to be the primary key.

Aditya Giri
  • 1,786
  • 18
  • 33
  • firstOrCreate() function is working. First, I have inserted a form data into database .Then I have inserted this database data into a new database which code is given in the question. When every time I submit form,new data is entered after previous data into second database. If same data is in database, I only want update the array new value into database – Alien Sep 20 '15 at 14:47
  • @Alien maybe your `Employee_id` is not set as index. Please try to set it as index and you should have what you mean to say. – Aditya Giri Sep 20 '15 at 14:51
  • Just run a `migrate:rollback` and then try again – Aditya Giri Sep 20 '15 at 14:51
0

There are two ways to go about this, depending on what you want to do exactly; using firstOrCreate() and firstOrNew().

firstOrCreate() will check all of the values that go in to it, and if they all match up with a record in there already then it will return a result, otherwise it will create a new record with the parameters you pass.

The catch with firstOrCreate is that if one of the parameters doesn't match then it will create a new instance. For example if you made two inserts with just the Employee_name spelt differently then this would create a new instance, even though the Employee_id hasn't changed.

The other option is firstOrNew(), which performs the same check and if no results are found then it returns a model instance for you to work with. However, you will need to manually save this by calling save().

Use of these:

  • $res1 = EmployeeList::firstOrCreate($res);
  • $res1 = EmployeeList::firstOrNew($res); $res1->save();

If you can just make the insert without needing to play around with what is inserted in to the model then go with firstOrCreate(), if you'd like to alter or choose what is entered in to the model at all then go with firstOrNew()

If you're after more info between the two, then this post sums it up well.

Community
  • 1
  • 1
James
  • 15,754
  • 12
  • 73
  • 91