Hi,
public function store()
I think you forget somethink like the parameters ?
Hello, what I'm trying to achieve is, When user create a team and upload the image for that team, I want to save the image in local and path in database. Most of tutorial/article show about updating model, instead I want when the model is created.
Currently I got this error and I'm stuck. Call to a member function hasFile() on array
Can someone review my code and tell me if I'm in the right track?
Controller
public function store()
{
$request = request()->validate([
'captain_id' => 'required',
'name' => 'required',
'area' => 'required',
'qtty_member' => 'required',
'image' => 'required|image|max:1999'
]);
if($request->hasFile('image')){
$image = $request->image;
$ext = $image->getClientOriginalExtension();
$filename = uniqid().'.'.$ext;
$image->storeAs('public/pics',$filename);
$request->image = $filename;
}
$team = Team::create([
'captain_id' => $request['captain_id'],
'name' => $request['name'],
'area' => $request['area'],
'qtty_member' => $request['qtty_member'],
'image' => $request->image
]);
return redirect('teams.index');
}
View
<form method="POST" action="/teams" enctype="multipart/form-data">
@csrf
<input name="captain_id" type="hidden" value="{{ $id }}">
<input name="qtty_member" type="hidden" value="1">
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">Team Name:</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="area" class="col-md-4 col-form-label text-md-right">Area</label>
<div class="col-md-6">
<input id="area" type="text" class="form-control @error('area') is-invalid @enderror" name="area" value="{{ old('area') }}" required autocomplete="area" autofocus>
@error('area')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label class="col-md-4 col-form-label text-md-right" for="image">Team Image</label>
<input type="file" name="image" id="image">
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
Currently I referring to these article
I would suggest not to mix the request() helper along with the Request object. Neither is wrong, just pick one and stick with it. In your case, you are assigning your $request variable to the result of the validate() method, which is why you're getting that error.
Instead, just inject the Request object by type-hinting it in your controller's method:
public function store(Request $request)
and then remove the $request assignment and helper call:
$request->validate([
'captain_id' => 'required',
'name' => 'required',
'area' => 'required',
'qtty_member' => 'required',
'image' => 'required|image|max:1999'
]);
Please or to participate in this conversation.