我想在Laravel中更新图像,但是会引发错误
在字符串上调用成员函数getClientOriginalExtension()
[25] [22] [27] [33]
[25] [27] [33]
[27] [33]
[33]
函数中的$this->storeImage($ad, 'photo1')
似乎不起作用。我首先尝试仅使用一张图像,但无法弄清楚如何使其工作。我尝试在update()
方法中执行与在update()
方法中相同的操作,但是它不起作用。
AdController.php
store()
这是public function store()
{
$ad = new Ad();
request()->validate([
'title' => ['required', 'min:10'],
'description' => 'required',
'guarantee'=>'required',
'city' => 'required',
'address' => 'required',
'rent' => 'required',
'type' => 'required',
'partitioning' => 'required',
'number_of_rooms' => 'required',
'culinary' => 'required',
'phone' => 'required',
'date_available' => 'required',
// 'min_age' => ['min:1', 'max:99'],
// 'max_age' => ['min:1', 'max:99'],
'photo1' => 'required'
]);
$ad -> title = request('title');
$ad -> description = request('description');
$ad -> city = request('city');
$ad -> address = request('address');
$ad -> rent = request('rent');
$ad -> type = request('type');
$ad -> partitioning = request('partitioning');
$ad -> number_of_rooms = request('number_of_rooms');
$ad -> utilities_price = request('utilities_price');
$ad -> RadioFurniture = request('RadioFurniture');
$ad -> air_conditioning = request('air_conditioning');
$ad -> parking = request('parking');
$ad -> terace = request('terace');
$ad -> garden = request('garden');
$ad -> elevator = request('elevator');
$ad -> dishes_machine = request('dishes_machine');
$ad -> clothes_machine = request('clothes_machine');
$ad -> cable_internet = request('cable_internet');
$ad -> wifi_internet = request('wifi_internet');
$ad -> TV = request('TV');
$ad -> microwaves = request('microwaves');
$ad -> fridge = request('fridge');
$ad -> own_heat = request('own_heat');
$ad -> about_me = request('about_me');
$ad -> my_age = request('my_age');
$ad -> number_of_residents = request('number_of_residents');
$ad -> my_sex = request('my_sex');
$ad -> cleaning = request('cleaning');
$ad -> guests = request('guests');
$ad -> parties = request('parties');
$ad -> wake_up = request('wake_up');
$ad -> sleep_hour = request('sleep_hour');
$ad -> culinary = request('culinary');
$ad -> smoke = request('smoke');
$ad -> work_hours = request('work_hours');
$ad -> min_age = request('min_age');
$ad -> max_age = request('max_age');
$ad -> smoker_buddy = request('smoker_buddy');
$ad -> RadioStudents = request('RadioStudents');
$ad -> RadioPets = request('RadioPets');
$ad -> AcceptsPets = request('AcceptsPets');
$ad -> phone = request('phone');
$ad -> facebook = request('facebook');
$ad -> photo1 = request('photo1');
$ad -> photo2 = request('photo2');
$ad -> photo3 = request('photo3');
$ad -> photo4 = request('photo4');
$ad -> photo5 = request('photo5');
$ad -> photo6 = request('photo6');
$ad -> photo7 = request('photo7');
$ad -> photo8 = request('photo8');
$ad -> guarantee = request('guarantee');
$ad -> owner_id = auth()->id();
$ad -> date_available = request('date_available');
$this->storeImage($ad,'photo1');
$this->storeImage($ad,'photo2');
$this->storeImage($ad,'photo3');
$this->storeImage($ad,'photo4');
$this->storeImage($ad,'photo5');
$this->storeImage($ad,'photo6');
$this->storeImage($ad,'photo7');
$this->storeImage($ad,'photo8');
if($ad->max_age<$ad->min_age){
return redirect('/adauga')->withErrors('Intervalul de varsta trebuie sa fie unul valid(Ex:19-22)')->withInput();
}
$ad->save();
return redirect('/home');
}
// Here is the update function using the $id
public function update($id){
$ad = Ad::findOrFail($id);
$ad -> title = request('title');
$ad -> description = request('description');
$ad -> city = request('city');
$ad -> address = request('address');
$ad -> rent = request('rent');
$ad -> type = request('type');
$ad -> partitioning = request('partitioning');
$ad -> number_of_rooms = request('number_of_rooms');
$ad -> utilities_price = request('utilities_price');
$ad -> RadioFurniture = request('RadioFurniture');
$ad -> air_conditioning = request('air_conditioning');
$ad -> parking = request('parking');
$ad -> terace = request('terace');
$ad -> garden = request('garden');
$ad -> elevator = request('elevator');
$ad -> dishes_machine = request('dishes_machine');
$ad -> clothes_machine = request('clothes_machine');
$ad -> cable_internet = request('cable_internet');
$ad -> wifi_internet = request('wifi_internet');
$ad -> TV = request('TV');
$ad -> microwaves = request('microwaves');
$ad -> fridge = request('fridge');
$ad -> own_heat = request('own_heat');
$ad -> about_me = request('about_me');
$ad -> my_age = request('my_age');
$ad -> number_of_residents = request('number_of_residents');
$ad -> my_sex = request('my_sex');
$ad -> cleaning = request('cleaning');
$ad -> guests = request('guests');
$ad -> parties = request('parties');
$ad -> wake_up = request('wake_up');
$ad -> sleep_hour = request('sleep_hour');
$ad -> culinary = request('culinary');
$ad -> smoke = request('smoke');
$ad -> work_hours = request('work_hours');
$ad -> min_age = request('min_age');
$ad -> max_age = request('max_age');
$ad -> smoker_buddy = request('smoker_buddy');
$ad -> RadioStudents = request('RadioStudents');
$ad -> RadioPets = request('RadioPets');
$ad -> AcceptsPets = request('AcceptsPets');
$ad -> phone = request('phone');
$ad -> facebook = request('facebook');
$ad -> instagram = request('instagram');
$ad -> guarantee = request('guarantee');
$ad-> date_available = request('date_available');
$ad-> photo1 = request('photo1');
$this->storeImage($ad, 'photo1');
$ad -> owner_id = auth()->id();
$ad -> save();
return redirect('/home');
}
函数:
storeImage()
似乎private function storeImage($ad, $photo){
if(request()->has($photo)) {
$ad->$photo = $ad->title.$ad->date_available.$photo.'.'.request()->$photo->getClientOriginalExtension();
$imageExtensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'svg', 'svgz', 'cgm', 'djv', 'djvu', 'ico', 'ief','jpe', 'pbm', 'pgm', 'pnm', 'ppm', 'ras', 'rgb', 'tif', 'tiff', 'wbmp', 'xbm', 'xpm', 'xwd'];
$foo = \File::extension($ad->$photo);
if(in_array($foo, $imageExtensions))
{
$width = Image::make(request()->$photo)->width();
$height = Image::make(request()->$photo)->height();
$img = Image::make(request()->$photo->getRealPath())
->resize($width/3,$height/3)
->orientate()
->save('images/'.$ad->$photo,40);
}
}
}
在$this->storeImage($ad, 'photo1');
方法中不起作用。关于如何更换它的任何想法?我必须改变什么?谢谢!
答案 0 :(得分:0)
首先,可以代替所有的$ad->param = request('param')
:
$input = $request->all();
$ad->update($input);
这将使用给定的输入更新$ ad中的所有字段。
或者您甚至可以:
$input = $request->all()
$ad = \App\Models\Ad::updateOrCreate($id, $input);
$add->save();
这将清除您的代码。
为了节省图像,您最好阅读laravel的文档: File upload in laravel