Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

930
Visualizações
Laravel 8 Call to a member function extension() on null

I am trying to upload an image in Laravel 8, after changing the file path to public in the config folder and setting up the Model properly. I am getting this response

Call to a member function extension() on null

My Product controller

 public function store(Request $request)
    {
        $file = $request->file('image');
        $name = Str::random(10);
        $url = Storage::putFileAs('images', $file, $name . '.' . $file->extension());

        $product = Product::create([
            'title' => $request -> input('title'),
            'description' => $request -> input('description'),
            'image' => env('APP_URL') . '/' . $url,
            'price' => $request -> input('price'),
        ]);

        return $product;
    }

My model

protected $guarded = ['id'];

Migration

public function up()
   {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('description')->nullable();
            $table->string('image');
            $table->decimal('price');
            $table->timestamps();
        });
    }

What have I done wrong or what am I not doing right please?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Verify that you have a file before creating the product. Or do a proper validation of all required fields.

public function store(Request $request)
    {
        if (!$request->has('image')) {
            return response()->json(['message' => 'Missing file'], 422);
        }
        $file = $request->file('image');
        $name = Str::random(10);
        $url = Storage::putFileAs('images', $file, $name . '.' . $file->extension());

        $product = Product::create([
            'title' => $request -> input('title'),
            'description' => $request -> input('description'),
            'image' => env('APP_URL') . '/' . $url,
            'price' => $request -> input('price'),
        ]);

        return $product;
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda