bekaskaki
1 week ago

remove commas or whitespace in array data

Posted 1 week ago by bekaskaki

how to remove whitespace in array data.

i have array data like this :

array:1 [▼
  0 => {#386 ▼
    +"id": 1
    +"deskripsi": "kesepakatan bersama"
    +"jenis": "MoU"
    +"tgl_mulai": "2019-10-01"
    +"tgl_selesai": "2019-11-09"
    +"pihak_1": "muhammada, dinasa, kadisa"
    +"pihak_2": "muhassmmad, dinas, kadis"
    +"pihak_3": ", , "
    +"pihak_4": ", , " 
    +"pihak_5": ", , "
    +"created_at": null
    +"updated_at": null
    +"status": "Masih Berjalan"
  }
]

store controller :

  public function store(Request $request)
    {
        $deskripsi = $request->deskripsi;
        $jenis = $request->jenis;
        $tgl_mulai = date('Y-m-d', strtotime($request->tgl_mulai));
        $tgl_selesai = date('Y-m-d', strtotime($request->tgl_selesai));
        $pihak_1 = implode(', ', $request->pihak_1);
        $pihak_2 = implode(', ', $request->pihak_2);
        $pihak_3 = implode(', ', $request->pihak_3);
        $pihak_4 = implode(', ', $request->pihak_4);

        $data = array(
           'deskripsi' =>  $deskripsi,
           'jenis' =>  $jenis ,
            'tgl_mulai' =>  $tgl_mulai,
            'tgl_selesai' => $tgl_selesai,
            'pihak_1' => $pihak_1,
             'pihak_2' =>  $pihak_2,
             'pihak_3' =>  $pihak_3,
             'pihak_4' =>  $pihak_4,
        );

        $query_insert = DB::table('kesepakatan_bersamas')->insert($data);

       return redirect('admin/'.$this->title)->with('success', 'Data Berhasil Di Update!');

    }

i use this for show the data :

 $data = DB::select('SELECT * ,
                        CASE WHEN DATEDIFF(tgl_selesai, CURDATE()) <= 0 THEN "Berakhir"
                        WHEN DATEDIFF(tgl_selesai, CURDATE()) < 30  THEN "Akan Berakhir"
                        ELSE "Masih Berjalan"
                        END status
                        FROM kesepakatan_bersamas');
return view('admin.'.$title.'.index', compact('title','data'));

in blade view i use @foreach($data as $dt) :

 <td class="text-center">
 {{ !empty($dt->pihak_1) ? str_replace(', ','/ ',$dt->pihak_1) :'' }}
{{ !empty($dt->pihak_2) ? str_replace(', ','/ ',$dt->pihak_2) :'' }} <br>
{{ !empty(str_replace(', ','',$dt->pihak_3)) ? str_replace(', ','/',$dt->pihak_3) :'' }}  <br>
{{ !empty(str_replace(', ','',$dt->pihak_4)) ? str_replace(', ','/ ',$dt->pihak_4) :'' }} <br>
</td>

how to remove the white space..i use str_replace but if data null 'commas' still appear in the view

Please sign in or create an account to participate in this conversation.