vendredi 27 septembre 2019

Request->file returns null why?

So I'm trying to create a Video store of sorts in which you can see the image of the movie, title, lenght and the option to delete it. The problem is when I try to upload an image it always returns null. Also I use Laravel 5.8.35

I tried with getClientOriginalExtension but it can't work since file returns null

Slika means Image

Žanr means Genre

Naslov means Title

Godina means Year

Filmovi means Movies

Trajanje means Lenght

Akcija means Action

index.blade.php

<style>
    table,tr,td {
        border: 1px solid black;
    }

    table {
        border-collapse: collapse;
        width: 800px;
        font-size: 30px;
        font-weight: bold;
        text-align: center;
        align-self: center;
    }
</style>

<table>
    <tr style="background-color:lightgray">
        <td>Slika</td>
        <td>Naslov filma</td>
        <td>Godina</td>
        <td>Trajanje</td>
        <td>Akcija</td>
    </tr>
    @foreach($filmovi as $film)
    <tr>
    <td><img src="" alt=""></td>
        <td></td>
        <td></td>
        <td> minuta</td>
        <td> <a href="" data-method="delete">[obriši]</a></td>

    </tr>
    @endforeach

    <div class="">
        <a href="" class="btn btn-primary">Dodaj novi film</a>
    </div>

create.blade.php

<div>

        <form action="" method="post">
 Naslov: <br>
 <input type="text" class="form-control" id="naslov" name="naslov">
 <br>
 Žanr: <div class="select">
    <select name="zanr" id="zanr">
        @foreach($zanr as $z )
    <option value=""></option>
        @endforeach
      </select>
</div>

  Godina: <div class="select" style="width:200px;">
    <select name="godina" id="godina">
         @for($i = (int)date("1900"); $i<=(int)date("Y"); $i++)
        <option value="" name="godina" ></option>
         @endfor
    </select>
  </div>

  Trajanje: <br>
  <input type="text" class="form-control" id="trajanje" name="trajanje">
  <br>

  Slika: <br>
  <form action="" method="post" enctype="multipart/form-data">
  <input type="file" name="slika" class="form-control" enctype="multipart/form-data">(max. 64 KB)
  <div class="form-group">
        <a href="" class="btn btn-primary">Natrag</a>
        <button type="submit" class="btn btn-success float-right">Objavi</button>
    </div>
  </form>
        </form>
 </div>

controller

<?php

namespace App\Http\Controllers;

use App\Filmovi;
use App\Zanr;
use Illuminate\Http\Request;
use App\Traits\UploadTrait;

class FilmoviController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

     use UploadTrait;
    public function index()
    {
        $filmovi = Filmovi::all();

        return view('filmovi.index', compact('filmovi'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $zanr = Zanr::all();

        return view('filmovi.create', compact('zanr'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {


        $filmovi = Filmovi::create([
            'naslov'   => request('naslov'),
            'id_zanr'    => request('zanr'),
            'godina'  => request('godina'),
            'trajanje' => request('trajanje'),
            'slika' => request('slika'),
        ]);

        if($request->has('slika'))
    {
        dd($slike = $request->file("slika"));

        $dirpath = '/uploads/slike/';

        $path=$dirpath . $slike;

        $this->uploadOne($slike, $dirpath, 'public');

        $filmovi->slika = $path;

    }
    $filmovi->save();

        return redirect()->route('filmovi.index');    
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Filmovi  $filmovi
     * @return \Illuminate\Http\Response
     */
    public function show(Filmovi $filmovi)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Filmovi  $filmovi
     * @return \Illuminate\Http\Response
     */
    public function edit(Filmovi $filmovi)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Filmovi  $filmovi
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Filmovi $filmovi)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Filmovi  $filmovi
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $filmovi = Filmovi::find($id);
        $filmovi->delete();

    //   \Mail::to($post->user)->send(new PostDeleted($post));

        return redirect()->route('filmovi.index');
    }

}

UploadTrait

<?php 
namespace App\Traits;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

trait UploadTrait
{
    public function uploadOne(UploadedFile $uploadedFile, $dirpath = null, $disk = 'public')
    {
        $file = $uploadedFile->storeAs($dirpath, $uploadedFile,$disk);

        return $file;
    }
}
?>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire