JOHNMAC's avatar

How to save image using array saving method

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');
0 likes
5 replies
jlrdw's avatar

In this line

$path->move(public_path('images/post_images'), $image);

Make sure it's your path not a tutorial path, but should work if you have an applicable path.

JOHNMAC's avatar

its my folder path and working fine but m just asking that, using this method:

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

or

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

how we can store image

jlrdw's avatar

Just like you did above, in your original post.

ismaile's avatar

Post::create is used to create a new post or in other words to add a row to the database. For images, we usually store the image itself in the file system (in a dedicated directory in the server) and we store an image path in the database. So, this would be your code:

// Validating the data
$request->validate([
        'post_name'         => 'required|unique:posts',
        'post_description'  => 'required',
        'post_slug'         => 'required',
        'post_image'        => 'required|image',
    ]);
// Storing the image
$path = $request->file('post_image');
$image = $path->getClientOriginalName();
$path->move(public_path('images/post_images'), $image);
// Storing the post in the DB including the image path
Post::create([
        'post_name'         => $request->post_name,
        'post_description'  => $request->post_description,
        'post_slug'         => $request->post_slug,
        'post_image'        => 'images/post_images/'.$image,
    ]);

A little note on this code. For the post_image value, I used: 'images/post_images/'.$image because when retrieving and displaying your image, in your HTML, it would be easier to directly provide $post->post_image as a src for your img without worrying about where it is located. If you only store the image name, then you would have to specify the path in the HTML and potentially, do it multiple times if you use your image in different part of your site.

So, the code above should work. But I suggest you to read this part of the documentation regarding the storage directory. https://laravel.com/docs/6.x/filesystem#the-public-disk That's where the files should be stored.

PS: in your post, you use post_name, post_description, ..., I suggest you to rename these attributes removing the prefix post_. At some point, you are going to need to access your post data and $post->name is already explicit, so no need to call it $post->post_name

jlrdw's avatar

In your above code, this part

    $image = $path->getClientOriginalName();
    $path->move(public_path('images/post_images'), $image);  //saving the actual image
    $post->post_image = $image;  // image name saved to db field.

So what is the problem or error.

Please or to participate in this conversation.