Monday, March 6, 2017

Store serialized array in Laravel

I am trying to store a serialized array in the database, but I'm not pretty sure how to do that. I need to store $raport->id_operator = $request->id_operator, as an array in the database with the others IDs i have in that column. The default value for id_operator is "a:0:{}". Here is my controller code:

public function edit_raport(Request $request,$id)
{



    $arr = array();

    $raport = Raports::find($id);

    $details = [
   $serializedArr = serialize(array(
       $raport->id_operator => $request->id_operator)),

        $raport->status => $request->status,
        $raport->approved => $request->approved,
        $raport->modify_user_id => Auth::user()->id,
        $raport->updated_at => date('Y-m-d H:i:s'),

    ];
    array_push($arr,$details);

    if($raport->save())
    {
        return redirect()->back()->with('success','Kërkesa u dërgua me sukses :)');
    }
    else
    {
        return redirect()->back()->with('error','Kërkesa nuk u dërgua :( ');
    }
}

And this is where i take the values

 <select class="select" name="id_operator" >
  <option value="">Zgjidh operator</option>
     @foreach($users as $user)
   <option value=""> </option>
      @endforeach

Thanks in advance if anyone can help



via User45648

Advertisement