Xitox's avatar
Level 5

Upload Image to local and path recorded in database

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

https://medium.com/@sagarmaheshwary31/laravel-5-8-from-scratch-eloquent-relationships-and-image-upload-49daece52a24

https://www.larashout.com/laravel-image-upload-made-easy

0 likes
4 replies
Neven's avatar

Hi,

public function store() 

I think you forget somethink like the parameters ?

1 like
Talinon's avatar
Talinon
Best Answer
Level 51

@xitox

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'
        ]);

1 like
Xitox's avatar
Level 5

OMG, ITS WORKS!!! Thank you so much, I wasted 3 hours only because of this. Thank you so much @talinon.

Talinon's avatar

You're welcome! I'm glad that solved your issue.

1 like

Please or to participate in this conversation.