mlazuardy's avatar

Save Image/file naming

in my previous project, i use hashName to turn the uploaded file original name to hashname. but what if i want to store the image like 'post'.+$post->id? so if the id of the post is 1, name of the image turn to post1 ?

0 likes
26 replies
Snapey's avatar

you will need to show some code. You can rename the file whatever you want

mlazuardy's avatar

sorry about that. here it is . in the store method of BlogController

 if ($request->hasFile('image')) {
            $request->file('image')->store('public/blogs/' . $request->user()->username);
            $blog->image = $request->file('image')->hashName('blogs/' . $request->user()->username . '/');
        }

so maybe i want to change after the $request->user()->username on $blog->image right after /, to give the post+$blog->id. like that

shez1983's avatar
            $request->file('image')->store('public/blogs/' . $request->user()->username);

instead of the username.. just do what you want to do ie post + $blog->id...

why is this complicated?

Snapey's avatar

store and storeAs use your default disk

what is this set to?

the first parameter to storeAs is the folder on your default disk, and the second parameter is the required filename

mlazuardy's avatar

so how exactly code to store the image ? and what i want is that image will store to the storage/blogs/post1.jpg . like that. @Snapey @shez1983

Snapey's avatar

If you save in storage/blogs/post1.jpgthen this will not be visible on your website since only the contents of the public folder should be 'public'

mlazuardy's avatar

that what i want sir. i mean public/storage/blogs/post1.jpg but also storage/app/public/blogs/post1.jpg, because symlink is configured @Snapey

Snapey's avatar

Look in config/filesystems.php

by default it will say that the default disk is 'local'

further down it will say that local points to the storage/app folder.

so when storing you need to use, as you had;

$request->file('image')->store('public/blogs/' . $request->user()->username);

and this will create a folder with the username and then store the file with a random name within that folder.

do you have a column called username in users table? default is name

If you want to store it with a specific name;

$request->file('image')->storeAs('public/blogs/', 'post' . $post->id);
mlazuardy's avatar

im adding 'username' to my users table already. because that i use $request->user()->username); for preventing folder with space. but when i use as your code show above

$request->file('image')->storeAs('public/blogs/', 'post' . $post->id);

the image column is null @Snapey

Snapey's avatar

My code does not touch the database. It just stores the file ?

I think the storeAs method returns the filename so try;

$filename = $request->file('image')->storeAs('public/blogs/', 'post' . $post->id);

$blog->image = $filename;

mlazuardy's avatar

that store to the public/blogs/post without id of $post and without extension too. just post @Snapey

Snapey's avatar

Well does $post exist at this point?

I can't see the rest of your code?

You have to do some of it for yourself you know. Its not complicated.

$ext = $request->file('image')->extension();

$filename = $request->file('image')->storeAs('public/blogs/', 'post' . $post->id . '.' . $ext);

$blog->image = $filename;
mlazuardy's avatar

the variable is $blog not $post hehe, okay just need to change the 'post' to 'blog' for now @Snapey

next maybe this is just a little error. if i use storeAs and the path is public/blogs/

the image is store to the directory/storage/app/public/blogs/blog.jpg without id, next problem is, in the symlink it store to the ( public/storage/blogs/blog.jpg) , without id too. so whats the problem ? the problem is in the databases table of blogs. it store the path public/blogs/blog.jpg and when i use laravel helper in blade

src="{{asset('storage/'.$blog->image)}}". it return 404 because that's not the exact path of the database table @Snapey  
Snapey's avatar

The problem is probably that $blog does not yet have an ID. You need to save it first or choose some other unique identifier.

Look, you asked for it to be called post+post->id but now you say hehe - its not post its blog. How do I know its even blog?

either show all your code or work it out for yourself

mlazuardy's avatar
   public function store(Request $request)
    {
        $this->validate(request(),[
            'body' => 'required',
            'title'=>'required',
            'image' => 'max:500',
           
        ]);

        $blog = new Blog;
        $blog->title = $request->title;
        $blog->body = $request->body;
        $blog->category_id = 1;
        $blog->user_id = auth()->id();
        $blog->slug = str_slug($blog->title).'-'.str_random(10);
        $blog->description = strip_tags(str_limit($blog->body, 100));
        if ($request->hasFile('image')) {
            $ext = $request->file('image')->extension();

            $filename = $request->file('image')->storeAs('blogs/', 'blog' . $blog->id . '.' . $ext);

            $blog->image = $filename;
        }
        $tags_id = [];
        if ($request->tags) {
            $tags = explode(',', $request->tags);
            foreach ($tags as $tag) {
                $tag_ref = Tag::firstOrCreate(['name' => str_slug($tag, '-')]);
                $tags_id[] = $tag_ref->id;
            }
        }
        $blog->save();
        $blog->tags()->sync($tags_id);
        return redirect('blog');

    }

you right about the id , but dont mind it . sorry about that. now i just want to store the file in exact path @Snapey

Snapey's avatar

blog does not have an id until here $blog->save();

work it out

mlazuardy's avatar

i know that now sir. but can you help me to fix the store method ? @Snapey they store to wrong path in database table

Snapey's avatar

they store to wrong path in database table

What does that mean? I'm not psychic

Snapey's avatar

the image

clearly you think I have super powers... sorry to disappoint

try to expend just a little more energy....

mlazuardy's avatar

should i have to repeat the reply before? @Snapey ,

$ext = $request->file('image')->extension();

$filename = $request->file('image')->storeAs('public/blogs/', 'post' . $post->id . '.' . $ext);

$blog->image = $filename;

this is the code before. image column in blogs table is store public/blogs/blog.jpg., and this is the wrong path to use helper in blade like

src="{{url('storage/'.$blog->image)}}

Because, the url helper is located in public folder. so image is return 404 because the if i inspect that in chrome. the full path url to the image is localhost/storage/public/blogs. that's wrong. i didnt have public/blogs folder inside public folder. i just have directory/public/storage/blogs/blog.jpg

Snapey's avatar
Snapey
Best Answer
Level 122

so now you are explaining the problem. Yes, I see you want the path to be the public version. Why can you not work it out? You seem to know what the URL should be.

Try this

$ext = $request->file('image')->extension();

$filename = $request->file('image')->storeAs('public/blogs/', 'post' . $post->id . '.' . $ext);

$blog->image = 'blogs/post' . $post->id . '.' . $ext);
2 likes
mlazuardy's avatar

you're the best sir thank you it work now. Thank you again and sorry for making you angry @Snapey

Please or to participate in this conversation.