Reputation: 57
In my project, I've got 2 elements that requires images (club got its own image and events)
In this question I focus only on club, since events solution will be the same probably.
I found a way to save the file path inside of database and in the same time save file in my project folder. Whenever I'm calling out image inside of view i do something like this:
<img src="{{ asset('storage/' . $clubView->photo_patch) }}">
Everything works fine but when i try to update them (change file path inside db to new one and add new image to my public/storage/images)
Everything falls with error:
Call to a member function store() on string
My question is, how do I make it properly then?
Here is code I made to get this work:
Controller class with route beneath it
--------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');
Just to be clear, inside db i store path to my actual image which is inside of my app/public/storage/images.
Edit:
Create form
<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>
Edit 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>
Upvotes: 0
Views: 118
Reputation: 1348
It seems like you're trying to access a form field like a file. Try using $club->photo_patch = $request->file('photo_patch')->store('images','public');
instead.
For this to work, your edit form should have a file input in it (as I suspect your create form does). In order to help you better, it would be useful to see your form html as well (for both forms).
Upvotes: 1
Reputation: 653
public function update(Club $club, Request $request) {
$club->update([
"name" => request->name,
...
]);
return redirect('clubs');
}
Route:
Route::get('update-club/{$club}' [AppControlller::class, 'update']);
Upvotes: 1
Reputation: 21
You can upload your image this way
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');
}
Upvotes: 1