亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 多圖上傳

Laravel 多圖上傳

PHP
阿波羅的戰車 2023-07-15 16:54:49
我正在嘗試保存多個圖像,但帖子底部的圖像中顯示錯誤。我嘗試過請求名稱,但出現另一個錯誤,屬性和圖像之間存在一對多關系,我正在嘗試保存屬性的許多圖像。如果我收到圖像,如果我在創建之前進行 dd 操作,我就會收到圖像。存儲方法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();    }
查看完整描述

2 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

看起來只有在沒有附加圖像的情況下才會發生此錯誤。您應該在循環請求數據時附加圖像:


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 ]);


查看完整回答
反對 回復 2023-07-15
?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

正如我所看到的,您正在調用在 foreach 循環內定義的變量 $name ,這意味著它沒有在循環外定義,這就是錯誤所說的。



查看完整回答
反對 回復 2023-07-15
  • 2 回答
  • 0 關注
  • 158 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號