jeudi 2 avril 2020

uploading image and got error in laravel "Undefined variable: data"

I am trying to update the product and for that I am updating the pictures. I meant if user want to add more pictures to it but when I do this I get below error Undefined variable: data

Also I want to restrict the total uploading pictures to max 3 pictures or 2 pictures and 1 video When I add a video it does not show like it does not play Any help would be great. Thank for the help in advance

public function editProduct($id, Request $request) {
    if(Auth::check()) {

        $pn = Input::get('pn');
        $desc = Input::get('desc');
        $price = Input::get('price');

        // $products = Product::all();

        $products = Product::where('id', $id)->first();

        // foreach($products as $p) {
        if(($products->user_id == Auth::user()->id) && ($products->id == $id)) {

            $this->validate($request, [

                'filename.*' => 'required|max:5000',
                // 'filename.*' => 'image|mimes:jpeg,png,jpg,mp4|max:5000'

        ]);

        if($request->hasfile('filename'))
        {
            foreach($request->file('filename') as $image)
            {
                $name = $image->getClientOriginalName();

                $filename = time(). '-' . $image;
                $file_path = $image->move(public_path().'/assets/images/', $name);

                $data[] = $name;  

            }
         }

            $new_name = json_encode($data);
            $products = Product::where('user_id', Auth::user()->id)
                                ->where('id', $id)->update([

            'product_name' => $pn,
            'description' => $desc,
            'price' => $price,
            'filename' => $new_name,
        ]);
}
} else {
        Session::flash("message", "OOPS! You dont have permission to edit the items. Please login first.");
        return redirect("/register-user");

    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire