mallocthePD
mallocthePD

Reputation: 83

Saving more than one field to database using eloquent

I have a weird problem: I use this code to save data to one field in a database table

for ($i = 0; $i < count($arr); $i++) {

    if (property_exists((object)$arr[$i], "id")) {
        $port = Port::where('id', $arr[$i]->id)->first();
    } else {
        $port = Port::where('id', $arr[$i][0]->id)->first();
    }

    $port->company_a_json = json_encode($arr[$i]);
    $port->save();
}

The thing is when I want to save data to another field in this table it doesn't save anything to both of the fields. I use this code to do that:

for ($i = 0; $i < count($arr); $i++) {

    if (property_exists((object)$arr[$i], "id")) {
        $port = Port::where('id', $arr[$i]->id)->first();
    } else {
        $port = Port::where('id', $arr[$i][0]->id)->first();
    }

    $port->company_a_json = json_encode($arr[$i]);

    if (property_exists((object)$arr[$i], "days")) {
        $port = Port::where('id', $arr[$i]->id)->first();
        $daysToSave = json_encode($arr[$i]->days);
    } else {
        $port = Port::where('id', $arr[$i][0]->id)->first();
        $daysToSave = json_encode($arr[$i][count($arr[$i]) - 1]->days);
    }

    $port->company_a_days = $daysToSave;
    $port->save();
    $ms->addMessageTranslated("success", "port after save days " . json_encode($port) . "", $post);
}

the $ms thing at the end is intended to output the $port variable so I can check if the values are saved correctly. this is the $ms output:

port after save days {"id":3,"name":"\tXIAMEN","price_mk":0,"company_a_20":0,"company_a_40":0,"company_a_days":"\"444\"","company_a_json":"[{\"portName\":\"XIAMEN\",\"days\":\"4\",\"id\":\"3\",\"datefrom\":\"02\\\/02\\\/2017\",\"dateTo\":\"02\\\/28\\\/2017\",\"price_x\":4,\"prices_y\":4},{\"portName\":\"XIAMEN\",\"days\":\"12\",\"id\":\"3\",\"datefrom\":\"\",\"dateTo\":\"\",\"price_x\":0,\"prices_y\":0},{\"portName\":\"XIAMEN\",\"days\":\"333\",\"id\":\"3\",\"datefrom\":\"02\\\/08\\\/2017\",\"dateTo\":\"02\\\/23\\\/2017\",\"price_x\":12,\"prices_y\":12},{\"portName\":\"XIAMEN\",\"days\":\"444\",\"id\":\"3\",\"datefrom\":\"02\\\/09\\\/2017\"...,

notice the column "company_a_days" updated to 444, which is the wanted value, but inside the physical table the value of "company_a_days" is not updated.

The problem is it does output a correct $port, but in the actual table the changes are not saved.

Upvotes: 1

Views: 112

Answers (2)

mallocthePD
mallocthePD

Reputation: 83

With some help from ukalpa I have finally solved it.

  • I used json_encode on the to save to 'company_a_json' and have not used json_encode to save to 'company_a_days'.
  • In addition, I save $port to the row twice, even though it's the same row in both saves, and I even pull this row to $port twice, once before each editing of values and save().
  • And instead of getting value out of $arr[$i][0]->days I use $arr[$i][count($arr[$i]) - 1]->days. Don't think it had to do with why it didn't update anything in the first place because the 'company_a_days' column had no value at all in the table, and not the one inside $arr[$i][0]->days.

Also added a check if ->days > 0 but that's just something concerning my own assignment.

for ($i = 0; $i < count($arr); $i++) {
    if(property_exists((object)$arr[$i], "id")){
        $port = Port::where('id', $arr[$i]->id)->first();
    }else{
        $port = Port::where('id', $arr[$i][0]->id)->first();
    }
    $port->company_a_json = json_encode($arr[$i]);
    $port->save();
    if(property_exists((object)$arr[$i], "days")){
        $port = Port::where('id', $arr[$i]->id)->first();
        if($arr[$i]->days > 0){
            $port->company_a_days = $arr[$i]->days;
            $port->save();
        }
    }else{
        $port = Port::where('id', $arr[$i][0]->id)->first();
        if($arr[$i][count($arr[$i]) - 1]->days > 0){
            $port->company_a_days = $arr[$i][count($arr[$i]) - 1]->days;
            $port->save();
        }
    }
}

$arr is an array of json inputs, so $arr[$i] is a single json input. Here's a sample input from $arr[$i] to make the whole solution more clear:

"[{\"portName\":\"HONG KONG\",\"days\":\"33\",\"id\":\"2\",\"datefrom\":\"02\/01\/2017\",\"dateTo\":\"02\/21\/2017\",\"price_x\":20,\"prices_y\":40}]"

and here is the updated values in the mentioned columns now:

  • company_a_json: {\"portName\":\"HONG KONG\",\"days\":\"33\",\"id\":\"2\",\"datefrom\":\"02\/01\/2017\",\"dateTo\":\"02\/21\/2017\",\"price_x\":20,\"prices_y\":40}.
  • company_a_days: 33.

this is an example in which the json is a single object and is taken care of inside these ifs:

  • if (property_exists((object)$arr[$i], "id"))

  • if(property_exists((object)$arr[$i], "days"))

    As oppose to the else parts where it takes care of json arrays that could be an input too. Guess its not the neatest code ever but i'm on a deadline and at least it works now :)

Thanks everyone

Upvotes: 1

ukalpa
ukalpa

Reputation: 329

After reviewed your code, I assume ['company_a_days'] should have been changed, but ['company_a_json'] has not been changed because you re-got the object $port again in the section of getting "days", you should remove getting $port again in the second part.

for ($i = 0; $i < count($arr); $i++) {

    if (property_exists((object)$arr[$i], "id")) {
        $port = Port::where('id', $arr[$i]->id)->first();
    } else {
        $port = Port::where('id', $arr[$i][0]->id)->first();
    }

    $port->company_a_json = json_encode($arr[$i]);

    if (property_exists((object)$arr[$i], "days")) {
        $daysToSave = json_encode($arr[$i]->days);
    } else {
        $daysToSave = json_encode($arr[$i][count($arr[$i]) - 1]->days);
    }

    $port->company_a_days = $daysToSave;
    $port->save();
    $ms->addMessageTranslated("success", "port after save days " . json_encode($port) . "", $post);
}

Upvotes: 1

Related Questions