ryanborum's avatar

Two File Uploads within a Form

Hey all, I have a quick form that holds an input for a date, as well as two inputs, each for a file. I'm hoping to store the files in respective folders, and pass the path of the file into the database once it's uploaded. I intentionally have separated inputs, I don't think a multiple-input wouldn't be ideal as the files should be stored separately. Unfortunately, when I submit my form I get the error:

Call to a member function store() on null

My relevant function withing my Controller when I submit the form is this:

  public function store(Request $request)
  {
      //Validate
      $request->validate([
          'date' => 'required',
          'agenda' => 'required',
          'minutes' => 'required'
      ]);

      $agenda = Agenda::create([
        'date' => $request->date,
        'agendaPath' => $request->file('agenda')->store('uploads'),
        'minutesPath' => $request->file('minutes')->store('uploads')
      ]);
      return redirect('/agendas/'.$agenda->id);
  }

My form within my view looks like this:

    <form action="/agendas" method="post">
    {{ csrf_field() }}
    <div class="form-group">
      <label for="date">Meeting Date</label>
      <input type="date" class="form-control" name="date">
    </div>
    <div class="form-group">
      <label for="agenda">Agenda File</label>
      <input type="file" class="form-control" name="agenda">
    </div>
    <div class="form-group">
      <label for="minutes">Minutes File</label>
      <input type="file" class="form-control" name="minutes">
    </div>
     <input type="submit" class="btn btn-primary">
   </form>

My corresponding Model looks like this:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Agenda extends Model
{
  protected $table = 'agendas';

  protected $dates = ['date', 'created_at', 'updated_at'];

  protected $fillable = ['date' , 'agendaPath', 'minutesPath'];
}

Any help would be appreciated with this. Do I need to define a separate function to upload these and call it from within 'store'?

0 likes
5 replies
Grelav's avatar

You need to add enctype attribute to your form.


<form action="/agendas" method="post" enctype="multipart/form-data">
....
</form>
1 like
ryanborum's avatar

@Grelav Once I add that, I get an error: Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)

When I run php_fileinfo it displays data, so I assume this isn't the issue.

Grelav's avatar

Are you doing this somewhere "$filetype = $file->guessClientExtension(); " ?

ryanborum's avatar

I assume this has to do with calling the store method on a file upload. Is it correct syntax to write

      $agenda = Agenda::create([
        'date' => $request->date,
        'agendaPath' => $request->file('agenda')->store('uploads'),
        'minutesPath' => $request->file('minutes')->store('uploads')
      ]);

Where agendaPath is supposed to represent the actual path to the file, and the second part of the statement is supposed to store it to a folder named "uploads". Should the context be separated?

Please or to participate in this conversation.