app/Http/Controllers/AnnoncesController.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)
    {
        $annonce = Annonce::create([
            'user_id'       => Auth::id(),
            'fichier_id'       => 1,
            'categ_annonce_id'       => $request->input('categ_annonce_id'),
            'titre'       => $request->input('titre'),
            'description'       => $request->input('description'),
            'date_pub'       => now(),
            'cout'       => $request->input('cout')??0,
            'quantite'       => $request->input('quantite')??0,
            'disponibilite'       => $request->input('disponibilite')??now(),
            'statut_ann'       => 0,
        ]);
        if ($annonce) {
            if($request['image'] != null){
                $file = $request->file('image');
                $image = $annonce->id.'.'.$file->getClientOriginalExtension();
                if (!file_exists(public_path('images/annonces'))) {
                    mkdir(public_path('images/annonces'));
                }
                $file->move(public_path('images/annonces'), $image);
                $annonce->image = "images/annonces/".$image;
                $annonce->save();
            }
            return redirect()->route('annonces.show', $annonce->id);
        }
    }
public function store ( Request $ request ) { $ annonce = Annonce :: create ( [ 'user_id' => Auth :: id ( ) , 'fichier_id' => 1 , 'categ_annonce_id' => $ request -> input ( 'categ_annonce_id' ) , 'titre' => $ request -> input ( 'titre' ) , 'description' => $ request -> input ( 'description' ) , 'date_pub' => now ( ) , 'cout' => $ request -> input ( 'cout' ) ?? 0 , 'quantite' => $ request -> input ( 'quantite' ) ?? 0 , 'disponibilite' => $ request -> input ( 'disponibilite' ) ?? now ( ) , 'statut_ann' => 0 , ] ) ; if ( $ annonce ) { if ( $ request [ 'image' ] != null ) { $ file = $ request -> file ( 'image' ) ; $ image = $ annonce -> id . '.' . $ file -> getClientOriginalExtension ( ) ; if ( ! file_exists ( public_path ( 'images/annonces' ) ) ) { mkdir ( public_path ( 'images/annonces' ) ) ; } $ file -> move ( public_path ( 'images/annonces' ) , $ image ) ; $ annonce -> image = " images/annonces/ " . $ image ; $ annonce -> save ( ) ; } return redirect ( ) -> route ( 'annonces.show' , $ annonce -> id ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .
/*dd($request->all());*/