在项目和数据库中上传照片 - Laravel

时间:2017-11-02 19:07:37

标签: php mysql laravel image-uploading

我需要一些上传帮助。我想在db中插入一个产品。该产品有3张图片。我希望将图片上传到项目中的特定文件夹,以及要输入到db的路径。 我要上传照片的文件夹是:/ public / css / img

我的Db看起来像这样:我将在db。中手动添加一个示例。

id |标题|价格| category_id | images1 | images2 | | images3 |等
    1 |沙发| 324.0 5 | /css/img/1.jpg | /css/img/2.jpg | /css/img/3.jpg

这是我的观点addProductModal.blade.php - >是一种形式的模态。

<div class="modal fade" id="modalFormaddproduct" role="dialog">
    <div class="modal-dialog" id="route">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Inchide</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Adauga Subcategorie</h4>
            </div>

            <!-- Modal Body -->
            <div class="modal-body" style="text-align: center;">
                <p class="statusMsg"></p>
                <form role="form" action="{{route('addproduct')}}" method="post">
                    {{csrf_field()}}
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label>Nume</label>
                                <input type="text" class="form-control  text-center" name="name" placeholder="">
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label>Pret</label>
                                <input type="text" class="form-control  text-center" name="price" placeholder="">
                            </div>
                        </div>
                    </div>
                    <!-- /.row -->
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label>Subcategoria:</label>
                            <select style="text-align-last:center" class="form-control text-center" name="category_id">
                                @foreach($categories as $category)
                                    @foreach($category->subcategories as $subcategory)
                                        <option value="{{$subcategory->id}}">{{$subcategory->category}}</option>
                                    @endforeach
                                @endforeach
                            </select>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label>Descriere</label>
                                <input type="text" class="form-control  text-center" name="description" placeholder="">
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label>Marime</label>
                                <input type="text" class="form-control text-center" name="size" placeholder="">
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label>Material</label>
                                <input type="text" class="form-control  text-center" name="material" placeholder="">
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label>Cantitate</label>
                                <input type="text" class="form-control  text-center" name="quantity" placeholder="">
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label>Recomandat:</label>
                                <select style="text-align-last:center" class="form-control text-center" name="hot">
                                    <option value="0">Nerecomandat</option>
                                    <option value="1">Recomandat</option>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-4">
                                <div class="form-group">
                                    <label>Imagine 1:</label>
                                    <input type="file" name="file1" id="file1">
                                    <input type="submit" value="Upload1" name="submit1">
                                    <input type="hidden" value="{{ csrf_token() }}" name="_token">
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <div class="form-group">
                                    <label>Imagine 2:</label>
                                    <input type="file" name="file2" id="file2">
                                    <input type="submit" value="Upload2" name="submit2">
                                    <input type="hidden" value="{{ csrf_token() }}" name="_token">
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <div class="form-group">
                                    <label>Imagine 3:</label>
                                    <input type="file" name="file3" id="file3">
                                    <input type="submit" value="Upload3" name="submit3">
                                    <input type="hidden" value="{{ csrf_token() }}" name="_token">
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" style="background: gainsboro; border-radius: 8px" class="btn btn-default" data-dismiss="modal">Inchide</button>
                            <button type="submit" style="background: #10D47D; border-radius: 8px" class="btn btn-primary">Adauga</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

路线为Route::post('/products/add', 'AdminController@addproduct')->name('addproduct');

控制器:AdminController.php

public function addproduct(Request $request)
    {
        $product = new Product();
        $product->title = $request->name;
        $product->price = $request->price;
        $product->category_id =  $request->category_id;
        $product->description = $request->description;
        $product->size = $request->size;
        $product->material = $request->material;
        $product->quantity = $request->quantity;
        $product->hot = $request->hot;
        $product->images1 = $request->file1;
        $product->images2 = $request->file2;
        $product->images3 = $request->file3;


         if (Input::hasFile('file1','file2','file3')) {

        echo 'Uploaded';
        $file = Input::file('file1','file2','file3');
        $file->move('uploads', $file->getClientOriginalName());
        echo '';
    }
        $product->save();
        return redirect(route('adminproducts'))->with('success', 'The Product was added');
    }

3 个答案:

答案 0 :(得分:0)

您应该添加到表单中:

enctype="multipart/form-data"

确保您可以上传如下文件:

<form role="form" action="{{route('addproduct')}}" method="post" enctype="multipart/form-data">

答案 1 :(得分:0)

我认为没有任何理由不使用某些维护的库,而是自己编写所有功能。我建议你看一下:https://github.com/spatie/laravel-medialibrary

答案 2 :(得分:0)

我找到了解决方案。

  public function addproduct(Request $request)
    {
        $product = new Product();
        $product->title = $request->name;
        $product->price = $request->price;
        $product->category_id = $request->category_id;
        $product->description = $request->description;
        $product->size = $request->size;
        $product->material = $request->material;
        $product->quantity = $request->quantity;
        $product->hot = $request->hot;
        $file1 = Input::file('file1');
        $file2 = Input::file('file2');
        $file3 = Input::file('file3');
        $filename1 = $file1->getClientOriginalName();
        $filename2 = $file2->getClientOriginalName();
        $filename3 = $file3->getClientOriginalName();
        $file1 = $file1->move(public_path().'/img', $filename1);
        $file2 = $file2->move(public_path().'/img', $filename2);
        $file3 = $file3->move(public_path().'/img', $filename3);
        $product->images1 = '/img/'.$filename1;
        $product->images2 = '/img/'.$filename2;
        $product->images3 = '/img/'.$filename3;
        $product->save();
        return redirect(route('adminproducts'))->with('success', 'Produsul a fost creat');
    }