Error Call to a Member Function store() on string - image upload error

Asked

Viewed 30 times

-1

I’m trying to make a upload of images using the public function create, but when I try to send the form to register the error appears:

Error Call to a Member Function store() on string

Does anyone know how I can solve?

PUBLIC FUNCTION CREATE:

 public function create(Request $request)
    {
        
        $user = Auth::user()->id;
        $data = $request->all();

        /*if($request->image->isValid()){*/

           $image = $request->image->store('livros');
           $data['image'] = $image;
     /*   }*/
        Livro::create([
            'users_id' => $user,
            'namel' => $request['namel'],
            'autor' => $request['autor'],
            'editora' => $request['editora'],
            'categoria'=> $request['categoria'],
            'classificação'=>$request['classificação'] ,
            'descricao'=>$request['descricao'],
            'image'=>$request['image'],
        ]);
        return view('livros/cadastro');

        
    }

FORM:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card" >
            <br>
            <div class="textocs">
               <h4>CADASTRO DE LIVROS</h4>

               </div>
<br><br>
                <div class="card-body">
                    <form method="POST" enctype=”multipart/form-data” action="{{ url('/cadastro_livros') }}">
                        @csrf

                        <div class="form-group row">
                            <label for="namel" class="col-md-4 col-form-label text-md-right">{{ __('TÍTULO') }}</label>
                            
                            <div class="col-md-6">
                                <input id="namel" type="text" class="form-control @error('namel') is-invalid @enderror" name="namel" value="{{ old('namel') }}" required autocomplete="namel" autofocus>

                                @error('namel')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror

                                <br>
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="autor" class="col-md-4 col-form-label text-md-right">{{ __('AUTOR') }}</label>
                           
                            <div class="col-md-6">
                                <input id="autor" type="text" class="form-control @error('autor') is-invalid @enderror" name="autor" value="{{ old('autor') }}" required autocomplete="autor">

                                @error('autor')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                                <br>
                            </div>
                        </div>



                        <div class="form-group row">
                            <label for="editora" class="col-md-4 col-form-label text-md-right">{{ __('EDITORA') }}</label>
                            
                            <div class="col-md-6">
                                <input id="editora" type="text" class="form-control @error('editora') is-invalid @enderror" name="editora" value="{{ old('editora') }}" required autocomplete="editora" autofocus>

                                @error('editora')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror 

                                <br>
                            </div>
                        </div>


                        <div class="form-group row">
                            <label for="categoria" class="col-md-4 col-form-label text-md-right">{{ __('CATEGORIA') }}</label>
                            
                            <div class="col-md-6">
                                <input id="categoria" type="text" class="form-control @error('categoria') is-invalid @enderror" name="categoria" value="{{ old('categoria') }}" required autocomplete="categoria" autofocus>

                                @error('categoria')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror

                                <br>
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="classificação" class="col-md-4 col-form-label text-md-right">{{ __('AVALIAÇÃO -> 0-10') }} </label>
                            
                            <div class="col-md-6">
                                <input id="classificação" type="number" maxlength="2" class="form-control @error('classificação') is-invalid @enderror" name="classificação" value="{{ old('classificação') }}" required autocomplete="classificação" autofocus>

                                @error('classificação')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror 

                                <br>
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="descricao" class="col-md-4 col-form-label text-md-right">{{ __('DESCRIÇÃO') }}</label>
                            
                            <div class="col-md-6">
                                <input id="descricao" type="text" class="form-control @error('descricao') is-invalid @enderror" name="descricao" value="{{ old('descricao') }}" required autocomplete="descricao" autofocus>

                                @error('descricao')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror 

                                <br>
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="image" class="col-md-4 col-form-label text-md-right">{{ __('CAPA') }}</label>
                            
                            <div class="col-md-6">
                                <input type="file" class="form-control @error('image') is-invalid @enderror" name="image" value="{{ old('image') }}" >

                                @error('image')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror

                                <br>
                            </div>
                        </div>

                        

                        <br><br>
                        
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Cadastrar') }}
                                </button>
                           


                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

1 answer

0


The create method of the controller is the view, that is, you do not create/change/delete the database in view, the Laravel uses by default the "store" method to create the data, IN CASE on the routes you have used the RESOURCE method. Ex.:

Route::resource('cadastro-livros', 'LivrosController');

Using "Resource", the Laravel creates the methods:

  1. index() (GET - main view)
  2. create() (GET - creation view)
  3. store(Request $request) (POST - method to save data)
  4. show($id) (GET - item details selected by ID)
  5. Edit($id) (GET - edit view)
  6. update(Request $request, $id) (PUT - data editing method)
  7. delete($id) (DELETE - method for deletion of data by ID)

So the create method will not receive Request, it is a GET protocol view, IE, the request will be received by the store method, and so your code will be like this:

public function store(Request $request)
{
    
    $user = Auth::user()->id;
    $data = $request->all();

    /*if($request->image->isValid()){*/

       $image = $request->image->store('livros');
       $data['image'] = $image;
 /*   }*/
    Livro::create([
        'users_id' => $user,
        'namel' => $request['namel'],
        'autor' => $request['autor'],
        'editora' => $request['editora'],
        'categoria'=> $request['categoria'],
        'classificação'=>$request['classificação'] ,
        'descricao'=>$request['descricao'],
        'image'=>$request['image'],
    ]);
    return view('livros/cadastro');        
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.