app/Http/Controllers/ArticlesController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
      $this->validate($request,[
      'name'          =>'required',
      'cijena'        =>'required',
      'kolicina'      =>'required',
      'category_id'   =>'required|integer',
      'opis'          =>'required',
      'slika'         => 'image|nullable|max:1999'
    ]);
    if($request->hasFile('slika'))
    {
      $fileNameWithExt = $request->file('slika')->getClientOriginalName();
      $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
      $extension = $request->file('slika')->getClientOriginalExtension();
      $fileNameToStore = $filename.'_'.time().'.'.$extension;
      $path = $request->file('slika')->storeAs('public/slike',$fileNameToStore);
    }else{
      $fileNameToStore = 'noimage.jpg';
    }
    $article = new Article;
    $article->name = $request->input('name');
    $article->cijena = $request->input('cijena');
    $article->kolicina = $request->input('kolicina');
    $article->category_id = $request->category_id;
    $article->opis = $request->input('opis');
    $article->user_id = auth()->user()->id;
    $article->slika = $fileNameToStore;
    $article->save();
    return redirect('/articles')->with('success', 'Artikl spremljen');
    }
public function store ( Request $ request ) { $ this -> validate ( $ request , [ 'name' => 'required' , 'cijena' => 'required' , 'kolicina' => 'required' , 'category_id' => 'required|integer' , 'opis' => 'required' , 'slika' => 'image|nullable|max:1999' ] ) ; if ( $ request -> hasFile ( 'slika' ) ) { $ fileNameWithExt = $ request -> file ( 'slika' ) -> getClientOriginalName ( ) ; $ filename = pathinfo ( $ fileNameWithExt , PATHINFO_FILENAME ) ; $ extension = $ request -> file ( 'slika' ) -> getClientOriginalExtension ( ) ; $ fileNameToStore = $ filename . '_' . time ( ) . '.' . $ extension ; $ path = $ request -> file ( 'slika' ) -> storeAs ( 'public/slike' , $ fileNameToStore ) ; } else { $ fileNameToStore = 'noimage.jpg' ; } $ article = new Article ; $ article -> name = $ request -> input ( 'name' ) ; $ article -> cijena = $ request -> input ( 'cijena' ) ; $ article -> kolicina = $ request -> input ( 'kolicina' ) ; $ article -> category_id = $ request -> category_id ; $ article -> opis = $ request -> input ( 'opis' ) ; $ article -> user_id = auth ( ) -> user ( ) -> id ; $ article -> slika = $ fileNameToStore ; $ article -> save ( ) ; return redirect ( '/articles' ) -> with ( 'success' , 'Artikl spremljen' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
//Za sliku //Dohvaćanje filename-a sa extenzijom //Dohvati samo $fileName //dohvati samo ekstenziju //Ime za pohranu //Upload slike