为什么我的request-> image是字符串而不是二进制文件?

时间:2019-04-23 08:25:59

标签: laravel laravel-5.7

我尝试进行更新,其中也可以包含图像,但是图像以字符串形式传递,而不是文件/二进制文件

dd($ request-> image); 我试图转储request-> image,但结果是

“ Busi-Spark-Plug.jpg”

它应该是文件或二进制

这是我的blade.php

@foreach($sparepart as $s)
    <form class="ui form" action="/sparepart/update" method="post">
        {{method_field('PATCH')}}
        {{ csrf_field() }}          
        Kode <input type="text" name="kode_sparepart" required="required" maxlength="15" value="{{old('kode_sparepart',$s->kode_sparepart)}}" disabled> <br/><br>
        Nama <input type="text" name="nama_sparepart" required="required" maxlength="100" value="{{old('nama_sparepart',$s->nama_sparepart)}}"> <br/><br>
        Merk <input type="text" name="merk_sparepart" required="required" maxlength="100" value="{{old('merk_sparepart',$s->merk_sparepart)}}"> <br/><br>
        Tipe <input type="text" name="tipe_sparepart" required="required" maxlength="20" value="{{old('tipe_sparepart',$s->tipe_sparepart)}}"> <br/><br>
        Harga Beli <input type="number" name="harga_beli" required="required" maxlength="10" min="0" value="{{old('harga_beli',$s->harga_beli)}}"> <br/><br>
        Harga Jual <input type="number" name="harga_jual" required="required" maxlength="10" min="0" value="{{old('harga_jual',$s->harga_jual)}}"> <br/><br>
        Jumlah Stok <input type="number" name="jumlah_sparepart" required="required" maxlength="10" min="0" value="{{old('jumlah_sparepart',$s->jumlah_sparepart)}}"> <br/><br>
        Stok Minimum <input type="number" name="stok_minimum" required="required" maxlength="10" min="0" value="{{old('stok_minimum',$s->stok_minimum)}}"> <br/><br>
        Letak Sparepart <input type="text" name="letak_sparepart" required="required" maxlength="12" value="{{old('letak_sparepart',$s->letak_sparepart)}}"> <br/><br>
        Gambar<br/>
        <div class="input-group control-group increment" >
            <input type="file" name="image" class="form-control">
        </div>
        <br><br>
        <input class="ui tiny green button" type="submit" value="Simpan Data">
        <a class="ui tiny red button" href="/sparepart"> Kembali</a>
    </form>
@endforeach 

这是SparepartController

public function update(Request $request)
{
    dd($request->image);
    $gbsparepart = new Sparepart;

    if($request->hasfile('image'))
    {
        $gambar_sparepart=$request->file('image');
        $name=time().$gambar_sparepart->getClientOriginalName();
        $gambar_sparepart->move(public_path().'/images/', $name);  
        $gbsparepart->gambar_sparepart = $name;  
    }
    else
    {
        $gbsparepart->gambar_sparepart = 'GAGAL Gambar';
    }

    $sparepart = new Sparepart;
    $sparepart = Sparepart::where('kode_sparepart',$request->kode_sparepart)->update([
    'nama_sparepart' => $request->nama_sparepart,
    'merk_sparepart' => $request->merk_sparepart,
    'tipe_sparepart' => $request->tipe_sparepart,
    'harga_beli' => $request->harga_beli,
    'harga_jual' => $request->harga_jual,
    'jumlah_sparepart' => $request->jumlah_sparepart,
    'stok_minimum' => $request->stok_minimum,
    'letak_sparepart' => $request->letak_sparepart,
    'gambar_sparepart' => $gbsparepart->gambar_sparepart
    ]);
     return redirect('/sparepart');         
}

1 个答案:

答案 0 :(得分:3)

首先在表单上,​​您缺少enctype属性,该属性将无法上传图像:因此请将表单更改为此:

<form class="ui form" action="/sparepart/update" method="post" enctype="multipart/form-data">

然后,为了从请求中获取文件,您应该使用:

dd($request->file('image'));

接下来要检查的方法是hasFile而不是hasfile,它区分大小写,所以要当心。