dimanche 13 septembre 2020

how to solve 'array to string conversion'

here is my controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Barangmasuk;
use App\Models\M_kategoribarang;
use App\Models\M_barang;
use App\Models\Barangmasuk_line;

class BarangMasuk_controller extends Controller
{
    public function add(){
        $title = 'Tambah Barang Masuk';
        $docno = 'BM-'.rand();
        $kategoribarang =M_kategoribarang::orderBy('nama_kategoribarang','asc')->get();

        return view('barangmasuk.add',compact('title','docno','kategoribarang'));
    }

    public function store(Request $request){
        try {
            $barang = $request->barang;
            $jumlah_barang = $request->jumlah_barang;

            $document_no = $request->document_no;
            $kategoribarang = $request->kategoribarang;

            $id_bm = Barangmasuk::insertGetId([
                'document_no'=>$document_no,
                'kategoribarang'=>$kategoribarang,
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ]);

            foreach ($jumlah_barang as $e=>$jb) {
                if ($jb == 0) {
                    continue;
                }

                 $dt_barang=M_barang::where('id',$barang[$e])->first();

                Barangmasuk_line::insert([
                    'barangmasuk'=>$id_bm,
                    'barang'=>$barang[$e],
                    'jumlah_barang'=>$jumlah_barang
                     
                ]);
            }

            \Session::flash('sukses','barangmasuk berhasil diinput');
        } catch (Exception $e) {
            \Session::flash('gagal',$e->getMessage());
        }

        return redirect()->back();
    }

    public function get_barang($id_kategoribarang){
        $title = 'Tambah Barang Masuk';
        $docno = 'BM-'.rand();
        $kategoribarang =M_kategoribarang::orderBy('nama_kategoribarang','asc')->get();
        $barang = M_barang::where('kategoribarang',$id_kategoribarang)->get();

        return view('barangmasuk.add',compact('title','docno','kategoribarang','barang','id_kategoribarang'));
    }
}

and here is my blade

@extends('layouts.master')
 
@section('content')
 
<div class="row">
    <div class="col-md-12">
        <h4></h4>
        <div class="box box-warning">
            <div class="box-header">
                <p>
                    <button class="btn btn-sm btn-flat btn-warning btn-refresh"><i class="fa fa-refresh"></i> Refresh</button>
                </p>
            </div>

                <form role="form" method="post" action="">
                    @csrf
            <div class="box-body">
               
               
                  <div class="box-body">
                    <div class="form-group">
                      <label for="exampleInputEmail1">Nomor Dokumen</label>
                      <input type="text" name="document_no" class="form-control" id="exampleInputEmail1" placeholder="Nomor Dokumen" value="">
                    </div>

                    @if(isset($id_kategoribarang))
                    <div class="form-group">
                      <label for="exampleInputPassword1">Kategori Barang</label>
                      <select class="form-control select2" name="kategoribarang">
                            <option selected="" disabled="">Pilih Kategori Barang</option>
                         @foreach($kategoribarang as $sp)
                            <option value="" ></option>
                         @endforeach
                      </select>
                    </div>
                    @else
                    <div class="form-group">
                      <label for="exampleInputPassword1">Kategori Barang</label>
                      <select class="form-control select2" name="kategoribarang">
                            <option selected="" disabled="">Pilih Kategori Barang</option>
                         @foreach($kategoribarang as $sp)
                            <option value=""></option>
                         @endforeach
                      </select>
                    </div>
                    @endif
                  </div>
                  <!-- /.box-body -->
     
            

                @if(isset($barang))
                <div class="row">
                    <div class="col-md-12">
                        <table class="table myTable">
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Nama</th>
                                    <th>Satuan Barang</th>
                                    <th>Jumlah Barang</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($barang as $e=>$pd)
                                <tr>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td>

                                        <input type="hidden" name="barang[]"value="">

                                        <input type="number" value="0" class="form-control" name="jumlah_barang[]">
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
                @endif

            </div>


            <div class="box-footer">
                    <button type="submit" class="btn btn-primary btn-block">Submit</button>
                  </div>
                </form>
        </div>
    </div>
</div>
 
@endsection
 
@section('scripts')
 
<script type="text/javascript">
    $(document).ready(function(){
        
        $("select[name='kategoribarang']").change(function(e){
          var id = $(this).val();
          var url = ""+'/'+id;

          window.location.href = url;  
        })    

        // btn refresh
        $('.btn-refresh').click(function(e){
            e.preventDefault();
            $('.preloader').fadeIn();
            location.reload();
        })
 
    })
</script>
 
@endsection

I always get 'array to string conversion' as the result. here is the screencapture of 'array to string conversion I don't know where the line/lines are the problems. I suspect the under the foreach at blade, there's the problem but I don't know how to convert that pls someone help me



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire