incridible
2 months ago
243
5
Laravel

How to save image using array saving method

Posted 2 months ago by incridible

in laravel 6 video the developer is using this method to save data ( but how to store image using this way, like 2nd method):

  Post::create($request->validate([
        'post_name'         => 'required|unique:posts',
        'post_description'  => 'required',
        'post_slug'         => 'required',
        'post_image'        => 'required|image',
    ]));      

   or

  $post = Post::create($request->all());

this is another method which is saving image in specified location :

  $request->validate([
        'post_name'         => 'required|unique:posts',
        'post_description'  => 'required',
        'post_slug'         => 'required',
        'post_image'        => 'required|image',
    ]);

    $post = new Post;
    $post->post_name = $request->post_name;
    $post->post_description = $request->post_description;
    $post->post_slug = $request->post_slug;
    $path = $request->file('post_image');
    $image = $path->getClientOriginalName();
    $path->move(public_path('images/post_images'), $image);
    $post->post_image = $image;
    $post->save();

    return redirect('/posts');

Please sign in or create an account to participate in this conversation.