【问题标题】:How to update already existing file如何更新已经存在的文件
【发布时间】:2021-10-24 15:05:20
【问题描述】:

在我的项目中,我有 2 个需要图像的元素(俱乐部有自己的图像和事件)

在这个问题中,我只关注俱乐部,因为事件解决方案可能是相同的。

我找到了一种方法来保存数据库中的文件路径,同时将文件保存在我的项目文件夹中。每当我在视图中调用图像时,我都会执行以下操作:

<img src="{{ asset('storage/' . $clubView->photo_patch) }}">

一切正常,但是当我尝试更新它们时(将数据库中的文件路径更改为新路径并将新图像添加到我的公共/存储/图像)

一切都出错了:

Call to a member function store() on string 

我的问题是,那我该怎么做呢?

这是我为完成这项工作而编写的代码:

控制器类及其下方的路由

--------Create new object/club-------------------

public function create(Request $request)
    {
        $id = Auth::id();
        $club = new Club;
        $club->name = $request->name;
        $club->description = $request->description;
        $club->address = $request->address;
        $club->city_id = $request->city_id;
        $club->user_id = $id;
        $club->photo_patch = $request->photo_patch->store('images','public');
        // $path = $request->photo_patch->store('images','public');

        // dd($club->photo_patch);
        $club->save();
        return redirect('clubs');
    }

Route::get('clubs/create', [App\Http\Controllers\ClubController::class, 'createView'])->name('clubs.createView');

--------Go to edit form-----------------------
public function edit($id)
    {
        $data = Club::find($id);
        $city = City::get();
        // $city_id = City::find;
        return view('backend/club/edit', ['data' => $data],['city' =>$city]);
    }

Route::get('clubs/edit/'.'{id}', [App\Http\Controllers\ClubController::class, 'edit'])->name('clubs.edit');

-----And then from edit, by pressing form button i do update function--------

    public function update(Request $request)
    {
        $data = Club::find($request->id);
        $data->name = $request->name;
        $data->description = $request->description;
        $data->address = $request->address;
        $data->photo_patch = $request->photo_patch->store('images','public');
        $data->city_id = $request->city_id;
        $data->save();

        return redirect('clubs');
    }

Route::post('club/update', [App\Http\Controllers\ClubController::class, 'update'])->name('club.update');

为了清楚起见,在 db 中,我存储了我的实际图像的路径,该图像位于我的 app/public/storage/images 中。

编辑:

创建表单

<form action="{{ route('club.create') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="container-sm">
        <div class="form-group">

            <label for="Club name">Club name</label>
            <input type="text" name="name" class="form-control" placeholder="enter name">

            <label for="Description">Description</label>
            <input type="textarea" rows="2" style="width:100%;height:60px;" name="description" class="form-control" placeholder="enter description">

            <label for="Address">Address</label>
            <input type="text" name="address" class="form-control" placeholder="enter address">

            <label for="inputState">City</label>
            <select id="inputState" name="city_id" class="form-control">
                @foreach($cities as $city)
                <option value="{{$city->id}}">{{ $city->name }}</option>
                @endforeach
            </select>

            <label for="Photo">Photo</label>
            <input type="file" class="form-control-file" name="photo_patch" accept="image/png, image/jpg">
            @error('photo_parch')
            <div class="invalid-feedback d-block">{{$message}}</div>
            @enderror

        </div>
        <button class="btn btn-primary" type="submit"> Create</button>
    </div>
</form>

编辑表单

<form action="{{ route('club.update') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="container-sm">
        <div class="form-group">

            <input type="hidden" name="id" value="{{$data->id}}">
            <input type="hidden" name="city_id" value="{{$data->city_id}}">

            <label for="City exit form input form">City name</label>
            <input type="text" name="name" class="form-control" value="{{$data->name}}">

            <label for="Description">Description</label>
            <input type="textarea" rows="2" style="width:100%;height:60px;" name="description" class="form-control" value="{{$data->description}}">

            <label for="Address exit form input form">Address</label>
            <input type="text" name="address" class="form-control" value="{{$data->address}}">

            <label for="inputState">City</label>
            <select id="inputState" name="city_id" class="form-control">
                @foreach($city as $city)
                <option value="{{$city->id}}">{{ $city->name }}</option>
                @endforeach
            </select>

            <label for="Photo">Photo</label>
            <input type="file" class="form-control-file" name="photo_patch" accept="image/png, image/jpg">
            @error('photo_parch')
            <div class="invalid-feedback d-block">{{$message}}</div>
            @enderror

        </div>
        <button class="btn btn-primary" type="submit"> Update</button>
        <div class="row">
        <img src="{{ asset('storage/' . $data->photo_patch) }}" options="transform: 0.5">
        </div>
    </div>
</form>

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    您似乎正在尝试访问像文件一样的表单字段。尝试改用$club-&gt;photo_patch = $request-&gt;file('photo_patch')-&gt;store('images','public');

    为此,您的编辑表单中应该有一个文件输入(我怀疑您的创建表单有)。为了更好地帮助您,查看您的表单 html 也会很有用(对于两种表单)。

    【讨论】:

    • 它给了我 null 作为回报。在 null 上调用成员函数 store()
    • 您确定要随请求一起上传文件吗?文件好像丢失了。尝试使用dd($request-&gt;files()) 进行调试。
    • 我的数据库存储了保存在 storage/images 中的图像的路径(在同一行中,它在创建函数 $club->photo_patch = $request->store('images','民众');)。然后我可以通过使用 @if($clubView->photo_patch) photo_patch) }}"> @else 在视图中调用它
    • 啊,所以您的意思不是要实际更新photo_patch 字段,对吧?只需从update 函数中删除该行即可。但是,如果您尝试更新它,您的表单上需要一个 &lt;input type="file" /&gt; 元素,而不仅仅是一个 &lt;img /&gt; 元素。
    • 我不知道我实际上在做什么先生,我更新了我的问题,所以它应该更清楚一点。
    【解决方案2】:

    您可以通过这种方式上传图片

    public function create(Request $request)
    {
        $imageName=date('d').time().date('m').'.'.$request->image->extension();
        $request->image->move(public_path('image'),$imageName);
    
        $id = Auth::id();
        $club = new Club;
        $club->name = $request->name;
        $club->description = $request->description;
        $club->address = $request->address;
        $club->city_id = $request->city_id;
        $club->user_id = $id;
        $club->photo_patch = $imageName;
        // $path = $request->photo_patch->store('images','public');
    
        // dd($club->photo_patch);
        $club->save();
        return redirect('clubs');
    }
    public function update(Request $request)
    {
        $imageName=date('d').time().date('m').'.'.$request->image->extension();
        $request->image->move(public_path('image'),$imageName);
    
        $data = Club::find($request->id);
        $data->name = $request->name;
        $data->description = $request->description;
        $data->address = $request->address;
        $data->photo_patch = $imageName;
        $data->city_id = $request->city_id;
        $data->save();
    
        return redirect('clubs');
    }
    

    【讨论】:

    • 先生,这样比较安全吗?
    • 我为您的方法更改了我的课程(也将表单名称从 photo_patch 更新为 image 但我又回到了与开始时相同的错误:D
    【解决方案3】:
      public function update(Club $club, Request $request) {
    
    $club->update([
    "name" => request->name,
    ...
    ]);
            return redirect('clubs');
        }
    
    
    Route:
    Route::get('update-club/{$club}' [AppControlller::class, 'update']); 
    

    【讨论】:

    • 这应该改变什么吗?或者我应该将我的更新请求存储在 ->update 中?
    • 内部更新和改变你的路线。链接也转到更新: 更新 并更改路线: Route::get('update-club/{ $club}' [AppController::class, 'update'])->name("update");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多