Php 我想更新Laravel中的图像

Php 我想更新Laravel中的图像,php,laravel,Php,Laravel,我想在Laravel中更新一个图像,但它抛出了错误 对字符串调用成员函数getClientOriginalExtension() 似乎update()函数中的$this->storeImage('ad,'photo1')不起作用。我首先尝试了一张图片,但我不知道如何让它工作。我尝试在update()方法中执行与store()方法相同的操作,但不起作用 AdController.php public function store() { $ad = new Ad(); reque

我想在Laravel中更新一个图像,但它抛出了错误

对字符串调用成员函数getClientOriginalExtension()

似乎
update()
函数中的
$this->storeImage('ad,'photo1')
不起作用。我首先尝试了一张图片,但我不知道如何让它工作。我尝试在
update()
方法中执行与
store()
方法相同的操作,但不起作用

AdController.php

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');
}

看起来像是
$this->storeImage($ad,'photo1')
update()
方法中不起作用。有什么办法可以替换它吗?我必须改变什么?谢谢大家!

首先,您可以执行以下操作,而不是所有的
$ad->param=request('param')

 $input = $request->all();
 $ad->update($input);
这将使用给定的输入更新$ad中的所有字段

或者你甚至可以:

$input = $request->all()
$ad = \App\Models\Ad::updateOrCreate($id, $input);
$add->save();
这将清理您的代码一些

为了保存图像,您最好阅读laravel的文档:

$input = $request->all()
$ad = \App\Models\Ad::updateOrCreate($id, $input);
$add->save();