vendredi 2 mars 2018

Laravel - Undefined property: Illuminate\Http\UploadedFile::$image_path

I want to save my images with save_image() function (I created this function on my media model)

I don't have $image_path variable in my controller, When I saving my images it gives me that error. (İmages saves fine, but I can't get returning $image variable (from media model))

here ise error log;

local.ERROR: Undefined property: Illuminate\Http\UploadedFile::$image_path {"userId":1,"email":"cervantess_23@gmail.com","exception":"[object] (ErrorException(code: 0): Undefined property: Illuminate\Http\UploadedFile::$image_path at /var/www/parti/app/Http/Controllers/MediaController.php:32)

my Media model

class Media extends Model
    {
   public function save_image($file)
       {
      $realname = str_slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME));
      $extension = $file->getClientOriginalExtension();
      $new_name = str_slug($realname) . "-" . time() . "." . $extension;
      $file->move(public_path('uploads'), $new_name);

      $image = DB::create([
         'image' => $new_name,
         'image_path' => "uploads/" . $new_name,
         'image_alt_name' => $realname
         ]);
         return $image;
        }
    }

and my MediaController

public function storeMedia(Request $request)

{
    $this->validate($request,[
        'image'=> 'required|mimes:jps,png,gif,jpeg'
    ]);

    $image=$request->file('image');
    $media = new Media();
    $media->save_image($image);

    return response()->json([
        'url'=> env('APP_URL')."/".$image->image_path,
        'alt' => $image->image_alt_name,
        'id' => $image->id,
    ]);
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire