首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel多图像上传

Laravel多图像上传
EN

Stack Overflow用户
提问于 2020-07-07 06:47:53
回答 2查看 53关注 0票数 0

我正在尝试保存多个图像,但在帖子底部的图像中显示错误。

我尝试过请求名称,但我得到了另一个错误,我在属性和图像之间有一对多的关系,我正在尝试保存属性的多个图像。

如果我正在接收图像,如果我在创建之前创建了一个dd,我就会收到图像。

存储方法

代码语言:javascript
运行
复制
public function store(Request $request)

{
  /*--from this session you start to save the properties with all their attributes  --*/
  
    $properti = new Propertie;

    $detail = new Detail;

    $detail->antiquity = $request->antiquity;
    $detail->furnished = $request->furnished;
    $detail->floor = $request->floor;

    $detail->save();

    $properti->details_id = $detail->id;

    $properti->name = $request->name;
    $properti->price = $request->price;
    $properti->description = $request->description;


    $properti->departaments_id = $request->departaments;
    $properti->municipalities_id = $request->municipalities;

    $properti->property_type_id = $request->type_property;
    $properti->offer_type_id = $request->type;


    $properti->details_id = $detail->id;

    $properti->lat = $request->lat;
    $properti->lng = $request->lng;
    $properti->address = $request->address;


    if (isset($request->property_id)) {
        $property_type = $request->property_id;
    } else {
        $property_type = null;
    }
    
    

    $properti->save();
    
    $image->name = $request->name;
   
        
        foreach($request->file('images') as $image ){ 
            $name = $image->getClientOriginalName();
            $image->move('image',$name);
        }
    
    
    $properti->images()->create(['name' => $name ]);



    $piso_id = $properti->id;
    $space = new Space;

    $space->property_id = $piso_id;
    $space->bedrooms = $request->bedrooms;
    $space->bathrooms = $request->bathrooms;
    $space->parking = $request->parking;
    $space->area = $request->area;


    $space->save();

    $properti->spaces_id = $space->id;


    foreach ($request->input('characteristic') as $characteristic) {

        $charc = new Characteristic;

        $charc->property_id = $piso_id;
        $charc->characteristic = $characteristic;
        $charc->save();
    }


  

    Session::flash('message', 'Se ha registrado su propiedad De forma exitosa');

    return redirect()->action('PropertyController@index',compact('name'));
    // return view('properties.index',compact('properties'));
}

文件输入

代码语言:javascript
运行
复制
<div class="custom-file">
                    <input required type="file" class="form-control" name="images" placeholder="Imagenes" multiple>
                </div>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-07 22:46:33

看起来只有在没有附加图像的情况下才会发生此错误。您应该在循环请求数据时附加图像:

代码语言:javascript
运行
复制
foreach ($request->file('images') as $image) { 
    $name = $image->getClientOriginalName();
    $image->move('image', $name);
    // v- TO HERE -v
    $properti->images()->create(['name' => $name ]);
}

// MOVE THIS: $properti->images()->create(['name' => $name ]);
票数 1
EN

Stack Overflow用户

发布于 2020-07-07 07:27:54

正如我所看到的,您正在调用一个在foreach循环内定义的变量$name,这意味着它不是在循环外定义的,这就是错误所说的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62765603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档