rafalmietkiewicz's avatar

Laravel - uploading pdf via form

Guys, you've helped me before and I believe you'll give me a hint this time.

I've built a form and a controller and it does what I need apart from uploading the file. It does not spit any errors, though.

Here is my form:

 <form method="POST" action="/ijps" role="form">

                          <div class="form-group">
                            <textarea name="autor" class="form-control"></textarea>
                          </div>

                          <div class="form-group">
                            <textarea name="tytul" class="form-control"></textarea>
                          </div>

                          <div class="form-group">
                            <textarea name="rok" class="form-control"></textarea>
                          </div>

                          <div class="form-group">
                            <textarea name="issue" class="form-control"></textarea>
                          </div>

                          <div class="form-group">
                            <label for="plik">Plik</label>
                            <input type="file" id="plik">
                          </div>
                          
                          <input type="hidden" name="_token" value="{!! csrf_token() !!}">  

                          <button type="submit" class="btn btn-primary">Wyślij</button>
                    
                    </form>
    ```

and here is my controller:

public function store(Request $request) {

    $ijp = new Ijp;

    $ijp->create($request->all());

}

What am I doing wrong?
0 likes
10 replies
rafalmietkiewicz's avatar

usama.ashraf - I've tried this and still doesn't work. I've checked via phpmyadmin and the field is blank ( it's blob type )

usama.ashraf's avatar

Consider these things:

  • Use create as a static method: Ijp::create(....).
  • Read up on mass assignment and the fillable property of models.
  • Also, you can't just insert a file directly in the database without some conversion(s).

This is pretty basic stuff. You'd be better off going through the docs or some of the introductory videos here first.

Don't dive into coding directly. Learn first.

rafalmietkiewicz's avatar

usama.ashraf - mass assignment and the fillable property is all set, I hope I did it right.

I don't know the difference between create and save - how would it look like with create? And yes, I am watching lost of laracasts, but am inpatient and trying new things.

protected $fillable =  ['autor', 'tytul', 'rok', 'issue', 'plik', 'created_at', 'updated_at'];
rafalmietkiewicz's avatar

Oh, I've just read that create method accepts only plain php array, does it mean it will not accept files such as pdf? If so, how can I turn this into save method?

rafalmietkiewicz's avatar

Ok, I've come up with a save method, it process all the fields apart from the file ( plik ). It says it cannot be empty, and I'm choosing a file. Where to go from now? It seems like it does not process the file at all :(

public function store(Request $request)
    {

        $ijp = new Ijp;
        $ijp->autor = $request['autor'];
        $ijp->tytul = $request['tytul'];
        $ijp->rok = $request['rok'];
        $ijp->issue = $request['issue'];
        $ijp->plik = $request['plik'];

        $ijp->save();

rafalmietkiewicz's avatar

Maybe I should declare somehow, that the data is a pdf type of file? Here? Please help me.

$ijp->plik = $request['plik']; - here something like ('file type = pdf ' ) ?

Szyfr's avatar

here..

<!-- missing enctype="multipart/form-data" in your form tag. -->
<form method="POST" action="/ijps" role="form">
change to
<form method="POST" action="/ijps" enctype="multipart/form-data">
<!-- missing name="plik" in your input tag -->
<input type="file"  id="plik">
change to
<input type="file" id="plik" name="plik">

hope it helps..

Please or to participate in this conversation.