1

i have 2 type of code use laravel eloquent to insert data to database. but the problem is laravel cant use the column name as array. here its the code

//manual
$insert = new AlarmActive;
$insert->$primaryKey = $value[$primaryIndex];
$insert->Specific_problem = $value[1];  
$insert->Managed_object = $value[2];    

$insert->save(); 

//with for
$insert = new AlarmActive;
for($i=0; $i<count($column); $i++){
    $insert->$column[$i] = $value[$i];
}
$insert->save();

the first code with tag //manual is doing fine and can input to database. but when i try with for (//with for) laravel say it to convertion the array to string. my $column[$i] values is the database column name and i can echo it.

so my question how to make the second code run.

EDIT here it my code to fill $column variable

 function inputAlarmActive($filenamewithoutex)
    {
        $line = Storage::get( 'public/'.$filenamewithoutex[0].'/show_alarm_active.log' );;
        $fLine = explode("\n", $line);
        $size = count($fLine);
        $data = array();
        $arr_data;
        $temp;
        $column;//Kolom tabel database
        $value;//Nilai Kolom
        $batas = "Appl. addl. info";
        $tabel = "show_alarm_active";
        $primaryKey ="Alarm_ID";
        $primaryIndex =0;


    for ($i=0; $i < $size; $i++) {
        if (strpos($fLine[$i], " :") !==false) {
            $temp[] = $fLine[$i];
        }
    }
    $this->ArrayData($temp, $batas, $tabel,$primaryKey,$primaryIndex);
}

function ArrayData($temp, $batas, $tabel, $primaryKey, $primaryIndex)
{
    global $fLine, $size,$data;
    for ($i=0; $i < count($temp) ; $i++) {
        $data = explode(":", $temp[$i]);
        $kolom = trim($data[0], " ");
        $kolomdot = preg_replace('/[^A-Za-z0-9\-\\ ]/', '', $kolom);

        if(strpos($kolomdot, ' ')){
            $column[]=str_replace(' ', '_', $kolomdot);
        }
        else{
            $column[]=str_replace('-', '_', $kolomdot);   
        }
        $value[]=$data[1];

        if (strpos($temp[$i], $batas)!==false) {
            $this->InsertData($column, $value, $tabel,$primaryKey, $primaryIndex);
            unset($column);
            unset($value);
        }
    }
}

first function "input alarm active" use to read the file with parameter the name of the folder. in this function im explode ":" to get data, the left of ":" will be the column, and the right will be the value.

second function is arraydata to fill the column array variable and fill value array variable.

third class is insert data, the code to insert to database.

here its the output of

for($i=0; $i<count($column); $i++){
echo $column[$i]." : ".$value[$i]."<br>";

}

Output $column and $value

here its the dd() output of $column

array:14 [▼
  0 => "Alarm_ID"
  1 => "Specific_problem"
  2 => "Managed_object"
  3 => "Severity"
  4 => "Cleared"
  5 => "Clearing"
  6 => "Acknowledged"
  7 => "Ack_user_ID"
  8 => "Ack_time"
  9 => "Alarm_time"
  10 => "Event_type"
  11 => "Application"
  12 => "Identif_appl_addl_info"
  13 => "Appl_addl_info"
]

here the dd() of $value

  array:14 [▼
  0 => " 103736\r"
  1 => " 70025 - POSSIBLE SECURITY THREAT IN NETWORK ELEMENT\r"
  2 => " fsFragmentId=PamAlarmThrower,fsFragmentId=security,fsClusterId=clusterRoot\r"
  3 => " 4 (minor)\r"
  4 => " no\r"
  5 => " manual\r"
  6 => " no\r"
  7 => " N/A\r"
  8 => " N/A\r"
  9 => " 2017-06-03 08"
  10 => " x4 (quality of service)\r"
  11 => " fsFragmentId=PamAlarmThrower,fsFragmentId=security,fsClusterId=clusterRoot\r"
  12 => " \r"
  13 => " \r"
]
7
  • could you show the code that fills the $column variable? Commented Sep 6, 2017 at 14:13
  • show the output of $column and $value array Commented Sep 6, 2017 at 14:14
  • could you do a dd($column) and show us the output? Commented Sep 6, 2017 at 14:21
  • @Quezler im already edit it. Commented Sep 6, 2017 at 14:21
  • @PankajMakwana im already show the output Commented Sep 6, 2017 at 14:22

2 Answers 2

1

You could try this:

$insert->setAttribute($columns[$i], $values[$i]);

If you check the __call() and __get()/__set() in the base Model-class this is what's happening there and the method itself seems to be public according to the trait that implements it.

Sign up to request clarification or add additional context in comments.

4 Comments

i got 2/2) QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column '__More__Severity' in 'field list' (SQL: insert into show_alarm_active (Alarm_ID, Specific_problem, Managed_object, __More__Severity, Cleared, Clearing, Acknowledged, Ack_user_ID, Ack_time, Alarm_time, Event_type, Application, Identif_appl_addl_info, Appl_addl_info) values ( 109248 bla bla bla..
Is the name __More__Severity really the name of the attribute/field? Maybe you have to sanitize the values in $column e.g. remove the underscores?
i want to ask, how to make condition to avoid duplicate data in primary key
I think the easiest solution for you would be to call a ::find($value[$primaryKeyIndex]) before changing the data and then check if something was found or not and then do whatever you want (error, change key, etc.). I would recommend asking a new question for this, as this could easily become a more complex answer.
0
$insert->{$column[$i]} = $value[$i];

2 Comments

i got 2/2) QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column '__More__Severity' in 'field list' (SQL: insert into show_alarm_active (Alarm_ID, Specific_problem, Managed_object, __More__Severity, Cleared, Clearing, Acknowledged, Ack_user_ID, Ack_time, Alarm_time, Event_type, Application, Identif_appl_addl_info, Appl_addl_info) values ( 109248 bla bla bla..
validate your $column array. There must be only array of columns for your table. Unknown column '__More__Severity' is very clear error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.