app/Http/Controllers/CategoryController.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)
    {
        $request->validate([
            'name' => 'required|unique:categories'
        ]);
        if($request->file()){
            $fileName = rand().'_'.$request->photo->getClientOriginalName();
            $filePath = $request->file('photo')->storeAs('categoryimg',$fileName,'public');
            $path = '/storage/'.$filePath;
        }
        $category = new Category;
        $category->name = $request->name;
        if($request->file()){
            $category->photo = $path;
        }else{
            $path = '/storage/categoryimg/category.png';
            $category->photo = $path;
        }
        $category->save();
        return redirect()->route('categories.index');
    }
public function store ( Request $ request ) { $ request -> validate ( [ 'name' => 'required|unique:categories' ] ) ; if ( $ request -> file ( ) ) { $ fileName = rand ( ) . '_' . $ request -> photo -> getClientOriginalName ( ) ; $ filePath = $ request -> file ( 'photo' ) -> storeAs ( 'categoryimg' , $ fileName , 'public' ) ; $ path = '/storage/' . $ filePath ; } $ category = new Category ; $ category -> name = $ request -> name ; if ( $ request -> file ( ) ) { $ category -> photo = $ path ; } else { $ path = '/storage/categoryimg/category.png' ; $ category -> photo = $ path ; } $ category -> save ( ) ; return redirect ( ) -> route ( 'categories.index' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// dd($request); // validation // upload file // 1665003670_290012.png // categoryimg/1665003670_290012.png // data insert // redirect