如何选择组合框并输入数据库

时间:2019-09-19 04:41:33

标签: php html mysql laravel

我制作了一个组合框,其数据来自jenis_makanan表。从组合框中,选择内容将输入到makanan表中。我不知道哪里错了,如何将ComboBox选项输入数据库?

这是输入数据的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Gambar;
use App\JenisMkn;
use App\cmb;
use Input;
class MakananController extends Controller
{
    public function index()
    {
        $makanan = Gambar::all();
        return view('makanan', ['makanan'=>$makanan]);
    }
    public function input()
    {
        $jenis = JenisMkn::select('id_jenis','jenis_makanan')->get();
        return view('upload_gambar',['jenis'=>$jenis]);
    }
    public function proses(Request $request)
    {

        $this->validate($request,[
            'file'=>'required|file|image|mimes:jpeg,png,jpg|max:2048',
            'makanan'=>'required',
            'jenis'=>'required',
        ]);

        $file = $request->file('file');
        $nama_file = time()."_".$file->getClientOriginalName();

        //nama folder tempat file diupload
        $tujuan_upload = 'image_file';
        $file->move($tujuan_upload,$nama_file);

        Gambar::create([
            'file' =>$nama_file,
            'makanan'=>$request->nama_makanan,
            'jenis'=>$request->kode_jenis,
        ])->post();
        return redirect('/makanan');
    }
}

这是html

            <form action="{{Route('proses')}}" method="POST" enctype="multipart/form-data">
                {{ csrf_field() }}
                <div class="form-group">
                    <b>File Gambar</b>
                    <br/>
                    <input type="file" name="file">
                </div>

                <div class="form-group">
                    <b>Nama Makanan</b>
                    <br/>
                    <input type="text" name="makanan">
                </div>

                <div class="form-group">
                    <b>Jenis Makanan</b>
                    <br/>
                    <select name="cmb" id="" class="form control input-sm">
                        @foreach ($jenis as $j)
                            <option value="{{$j->id_jenis}}">{{$j->jenis_makanan}}</option>
                        @endforeach
                    </select>
                </div>
                <input type="submit" value="Upload" class="btn btn-primary">
            </form>

输出显示jenis字段为必填

1 个答案:

答案 0 :(得分:0)

您在控制器中使用了验证器

 $this->validate($request,[
            'file'=>'required|file|image|mimes:jpeg,png,jpg|max:2048',
            'makanan'=>'required',
            'jenis'=>'required',
        ]);

这里是jenis。.但是在刀片文件中,没有输入字段名称是jenis。因此,将输入字段名称更改为jenis

<div class="form-group">
                    <b>File Gambar</b>
                    <br/>
                    <input type="file" name="file">
                </div>

                <div class="form-group">
                    <b>Nama Makanan</b>
                    <br/>
                    <input type="text" name="makanan">
                </div>

                <div class="form-group">
                    <b>Jenis Makanan</b>
                    <br/>
                    <select name="jenis" id="" class="form control input-sm">
                        @foreach ($jenis as $j)
                            <option value="{{$j->id_jenis}}">{{$j->jenis_makanan}}</option>
                        @endforeach
                    </select>
                </div>

在控制器中,

Gambar::create([
            'file' =>$nama_file,
            'makanan'=>$request->makanan,
            'jenis'=>$request->jenis,
        ]);