dongya3627 2018-11-21 18:28
浏览 79
已采纳

上传图片代码的问题 - laravel

I hope you are doing well. I am new with laravel. I have a problem with a code for adding a product. The problem is exactly in product_image. I can't have the image and I keep receiving the message "Product added Successfully! without image"

public function save_product(Request $request)
{
    $data=array();
    $data['product_name']=$request->product_name;
    $data['category_id']=$request->category_id;
    $data['manufacture_id']=$request->manufacture_id;
    $data['product_short_description']=$request->product_short_description;
    $data['product_long_description']=$request->product_long_description;
    $data['product_price']=$request->product_price;
    $data['product_size']=$request->product_size;
    $data['product_image']=$request->product_image;
    $data['product_color']=$request->product_color;
    $data['publication_status']=$request->publication_status;
    $image=$request->file('product_image');
    if ($image) {
        $image_name=str_random(20);
            $ext=strtolower($image->getClientOriginalExtension());
            $image_full_name=$image_name.'.'.$ext;
            $upload_path='image/';
            $image_url=$upload_path.$image_full_name;
            $success=$image->move($upload_path,$image_full_name);
            if ($success) {
                $data['product_image']=$image_url;
                DB::table('tbl_products')->insert($data);
                Session::put('message','Product added Successfully!');
                return Redirect::to('/add-product');
            }
        //echo "<pre>";
        //print_r($data);
        //echo "</pre>";
        //exit();
    }
    $data['product_image']='';
    DB::table('tbl_products')->insert($data);
    Session::put('message','Product added Successfully! without image');
    return Redirect::to('/add-product');
}

trying to solve the problem. I added in the if statement this code to see what I have in $data:

echo "<pre>";
        print_r($data);
        echo "</pre>";
        exit();

But I had nothing. just blank page then I added the same code before the last 4 lines. I received what in the array and data["product_image"] was empty.

This is the form (i did not forget to put enctype):

<form class="form-horizontal" action="{{ url('/save-product')}}" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}
    <fieldset>
        <div class="control-group">
            <label class="control-label" for="date01">Product Name</label>
            <div class="controls">
                <input type="text" class="input-xlarge" name="product_name" required="">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="selectError3">Category name</label>
                <div class="controls">
                    <select id="selectError3" name="category_id">
                        <option>Select Category</option>
                                        <?php
                                                $all_published_category=DB::table('tbl_category')
                                                                                                ->where('publication_status',1)
                                                                                                ->get();
                                                foreach($all_published_category as $v_category) {?>
                        <option>{{$v_category->category_name}}</option>
                                        <?php } ?>
                    </select>
                </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="selectError3">Brand name</label>
            <div class="controls">
                <select id="selectError3" name="manufacture_id">
                    <option>Select Brand</option>
                                        <?php
                                                $all_published_manufacture=DB::table('tbl_manufacture')
                                                                                                ->where('publication_status',1)
                                                                                                ->get();
                                                foreach($all_published_manufacture as $v_manufacture) {?>
                    <option>{{$v_manufacture->manufacture_name}}</option>
                                        <?php } ?>
                </select>
            </div>
        </div>                        
        <div class="control-group hidden-phone">
            <label class="control-label" for="textarea2">Product Short Description</label>
            <div class="controls">
                <textarea class="cleditor" name="product_short_description" rows="3" required=""></textarea>
            </div>
            <div class="control-group hidden-phone">
                <label class="control-label" for="textarea2">Product Long Description</label>
                <div class="controls">
                    <textarea class="cleditor" name="product_long_description" rows="3" required=""></textarea>
                </div>
                <div class="control-group">
                    <label class="control-label" for="date01">Product Price</label>
                        <div class="controls">
                            <input type="text" class="input-xlarge" name="product_price" required="">
                        </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="fileInput">Image</label>
                    <div class="controls">
                        <input class="input-file uniform_on" name="product_image" id="product_image" type="file">
                    </div>
                </div>   
                <div class="control-group">
                    <label class="control-label" for="date01">Product Size</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="product_size" required="">
                    </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="date01">Product Color</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" name="product_color" required="">
                </div>
            </div>
            <div class="control-group hidden-phone">
                <label class="control-label" for="textarea2">Publication status </label>
                <div class="controls">
                    <input type="checkbox" name="publication_status" value="1">
                </div>
            </div>
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">Add product</button>
                <button type="reset" class="btn">Cancel</button>
            </div>
    </fieldset>
</form>   

thank you in advance!

  • 写回答

1条回答 默认 最新

  • duancenxie2233 2018-11-21 22:15
    关注

    Check your code

    <input class="input-file uniform_on" id="product_image" type="file">

    There is no name attributes. Add it and problem solved.

    <input class="input-file uniform_on" name="product_image" id="product_image" type="file">

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP