【发布时间】: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>
【问题讨论】: