【问题标题】:why images aren't stored in public path为什么图像不存储在公共路径中
【发布时间】:2021-07-22 06:54:46
【问题描述】:

我是 Laravel 初学者,我想用 Laravel 8 构建一个 API。

我有postsimages,我想存储更新它们。

我的store 方法有效,图像 保存在数据库中,public 路径保存在图像文件夹中,但在update 方法中我无法将其保存在文件夹中。

这些是我的代码:

PostController

public function store(Request $request )
{
    $data = $request->all();

    //validationg posts and images fields
    $validator = Validator::make($data, [
        'user_id' => 'required',
        'category_id' => 'required',
        'title' => 'required|max:150|unique:posts',
        'body' => 'required',
        'study_time' => 'required',
        'tags' => 'nullable|string',
        'image' => 'required',
    ]);

    if ($validator->fails()) {
        return response()->json(['error' => $validator->errors(), 'خطا در اعتبار سنجی']);
    }

    //separate tags
    $tags = explode(",", $request->tags);

    if ($request->hasfile('image')) {
        //getting post images from request
        $files = $request->file('image');

        //saving name and path of images
        foreach ($files as $file) {
            $imageName = time().rand(1,10000).'.'.$file->extension();
            $postTitle = $request->title; //post title for folder name and the images inside it
            $imagePath = public_path(). '/images/posts/'.$postTitle;

            $file->move($imagePath, $imageName);

            $image = new Image;
            $image->image = $imageName;
            $image->path = $imagePath;

            $images[] = $image; // make an array of uploaded images
        }
    }

    $post = Post::create($data);
    $post->images()->saveMany($images);//save imageas in image table
    $post->tag($tags);//save tags in tags table

    return response()->json([
        'success' => true,
        'message' => 'با موفقیت ثبت گردید ',
        'data' => $post
    ]);
}

public function update(Request $request, $id)
{
    $post_failed = Post::find($id);
    if (is_null($post_failed)) {
        return response()->json('پست  مورد نظر یافت نشد ', 404);
    }

    $data = $request->all();

    //validation posts and images fields
    $validator = Validator::make($data, [
        'user_id' => 'required',
        'category_id' => 'required',
        'title' => 'required|max:150|unique:posts',
        'body' => 'required',
        'study_time' => 'required',
        'tags' => 'nullable|string',
        'image' => 'required',
    ]);

    if ($validator->fails()) {
        return response()->json(['error' => $validator->errors(), 'خطا در اعتبار سنجی ']);
    }

    $tags = explode(",", $request->tags);

    if ($request->hasfile('image')) {
        $postTitle = $request->title; //post title for folder name and the images inside it

        //delete last Images from database for updating images
        Image::where('imageable_type', 'App\Models\Post')->where('imageable_id' , $id)->delete();

        //delete last images images folder
        File::delete(public_path('/images/posts/'.$postTitle));

        $files = $request->file('image');
        foreach ($files as $file) {
            $imageName = time().rand(1,10000).'.'.$file->extension();
            $imagePath = public_path(). '/images/posts/'.$postTitle;

            $image = new Image();
            $image->image = $imageName;
            $image->path = $imagePath;
            $images[] = $image;
        }
    }

    $post = Post::find($id);

    $post->user_id = $data['user_id'];
    $post->category_id = $data['category_id'];
    $post->title = $data['title'];
    $post->body = $data['body'];
    $post->study_time = $data['study_time'];
    $post->tags = $data['tags'];

    $post->save();
    $post->images()->saveMany($images);
    $post->tag($tags);


    return response()->json([
        'success' => true,
        'message' => 'با موفقیت ویرایش گردید ',
        'data' => $post
    ]);
}

postsimages 之间的关系是polymorphic 一对多,我用邮递员测试过。

邮递员

数据库

还有路径:

请帮忙。

【问题讨论】:

  • "但在更新方法中我无法将其保存在文件夹中。"解释这一点。它去了其他地方,无处可去,是否有错误,日志中的一些警告,任何其他信息?
  • 我的意思是我上传的图片,没有存储在公共路径中

标签: laravel api postman


【解决方案1】:

store() 方法中,您使用以下方法将图像保存在磁盘上

$imagePath = public_path(). '/images/posts/'.$postTitle;
$file->move($imagePath, $imageName);

update() 你删除了它们

File::delete(public_path('/images/posts/'.$postTitle));

并确定新文件的路径

$imagePath = public_path(). '/images/posts/'.$postTitle;

但在此之后没有任何反应。在整个update() 方法中,没有可以在存储中执行某些操作的代码,因此文件夹中当然不会出现任何内容;)

所以再次使用$file->move()Storage 门面来保存文件。

提示

同样,重复这样的长代码逻辑也是不好的做法。最好将其提取并在存储/更新之间共享。

【讨论】:

  • 如果我删除此行 File::delete(public_path('/images/posts/'.$postTitle)); 不会再出现。这个FILE::DLETE 根本不起作用
猜你喜欢
  • 2021-12-31
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 2013-05-25
  • 2017-11-11
相关资源
最近更新 更多