Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 嗨,我明白了;在布尔值上调用成员函数update()时出错;在Laravel中更新图像时_Php_Laravel - Fatal编程技术网

Php 嗨,我明白了;在布尔值上调用成员函数update()时出错;在Laravel中更新图像时

Php 嗨,我明白了;在布尔值上调用成员函数update()时出错;在Laravel中更新图像时,php,laravel,Php,Laravel,有人能帮我解决拉威尔的问题吗? 当我编辑和更新广告的图像时,对布尔值上的成员函数update()进行了错误调用 因此,当我创建新广告时,图像将被存储,但当用新图像更新广告时,旧图像将保留,并且我在storeImage中的这一行中得到此错误 private function storeImage($ad){ if (request()->has('image')) { $ad->update([ 'image' => reques

有人能帮我解决拉威尔的问题吗?
当我编辑和更新广告的图像时,对布尔值上的成员函数update()进行了
错误调用


因此,当我创建新广告时,图像将被存储,但当用新图像更新广告时,旧图像将保留,并且我在
storeImage
中的这一行中得到此错误

private function storeImage($ad){

    if (request()->has('image')) {
        $ad->update([
            'image' => request()->image->store('uploads', 'public'),
        ]);

        $image = Image::make(public_path('storage/' . $ad->image))->fit(300, 300, null, 'top-left');
        $image->save();

    }
}
这是我的AdsController

public function edit($id)
{
   $ad = Ad::find($id);

   return view('ads.edit_ad', compact('ad', 'id'));
}


public function update(Request $request, $id)
{
    $ad = Ad::find($id);

    $user_id = Auth::user()->id;

    $rules = [
    'ad_title'  => 'required',
    'ad_description'  => 'required',
    'purpose'  => 'required',
    'image'  => 'sometimes|file|image|max:5000',
    ];


    $this->validate($request, $rules);

    $title = $request->ad_title;


    $is_negotialble = $request->negotiable ? $request->negotiable : 0;

    $data = [
    'title'         => $request->ad_title,
    'description'   => $request->ad_description,
    'type'          => $request->type,
    'price'         => $request->price,
    'purpose'       => $request->purpose,
    'address'       => $request->address,
    'user_id'       => $user_id,
    ];


    $updated_ad = $ad->update($data);

    if ($updated_ad){
        $this->storeImage($updated_ad);
    }

    return redirect()->back()->with('success','Ad Updated');
}


public function destroy(Ad $ad)
{

    $ad->delete();
    return redirect()->back()->with('success','Ad Deleted');
}


/**
* Listing
*/

public function listing(Request $request){

    $ads = Ad::all();

    $roles = Role::all();

    return view('listing', compact('ads'));
}

public function myAds(){

    $user = Auth::user();
    $ads = $user->ads()->orderBy('id', 'desc')->paginate(20);

    return view('ads.my_ads', compact('ads'));
}

private function storeImage($ad){

    if (request()->has('image')) {
        $ad->update([
        'image' => request()->image->store('uploads', 'public'),
        ]);

        $image = Image::make(public_path('storage/' . $ad->image))->fit(300, 300, null, 'top-left');
        $image->save();
    }
}

在update()函数的底部,尝试更改以下内容:

if($updated\u ad){
$this->storeImage($updated_ad);
}
为此:

if($updated\u ad){
$this->storeImage($ad);
}

雄辩的models
update()
函数返回更新是否成功完成的布尔值。在
$ad
变量上运行
update()
函数后,它会发生变化并包含更新的数据,您可以在
storeImage()
函数中将其用作参数。

这是因为
storeImage()
中的
$ad
是一个布尔值<代码>更新
在模型对象中被调用,我看到我做错了什么。解释很清楚。谢谢你,伙计!