How to update requests where multiple db are use and images are involve

大城市里の小女人 提交于 2019-12-13 05:08:50

问题


Tried creating API where a user creates a post that includes the title and cook time of food and they can post up to 2 images in two tables posts and post_images in two different databases. the store function is working perfectly fine but I have no idea regarding update function. I can update all posts data through the postman but images are not getting updated

public function store(Request $request)
{
    $post = new Post;
    $post->setConnection('mysql1');
    $user = User::where('id', $request->id)->first();
    if ($user) {
        $post =  Auth::user()->posts()->create([
            'user_id' => Auth::id(),
            'post_id' => rand(),
            'title' => $request->title,
            'cooked_time' => strtotime($request->cooked_time),
        ]);
        $images = new PostImage;
        $destination_path = public_path('/images');
        if ($request->hasFile('image1')) {
            $image1 = $request->file('image1');
            $image_size1 = $image1->getClientSize();
            $image_ext1 = $image1->getClientOriginalExtension();
            $new_image_name1 = rand(123456, 99999999) . "." . $image_ext1;
            $image1->move($destination_path, $new_image_name1);
            $images->image_name1 = $new_image_name1;
        }
        if ($request->hasFile('image2')) {
            $image2 = $request->file('image2');
            $image_size2 = $image2->getClientSize();
            $image_ext2 = $image2->getClientOriginalExtension();
            $new_image_name2 = rand(123456, 99999999) . "." . $image_ext2;
            $image2->move($destination_path, $new_image_name2);
            $images->image_name2 = $new_image_name2;
        }
        $images->post_id = $post->post_id;
        $images->save();
        $post = json_decode($post);
        $post->image1 = $images->image_name1;
        $post->image2 = $images->image_name2;
        if ($post && $images) {
            return $this->successResponse($post);
        } else {
            return $this->errorResponse("Post Failed", 200);
        }
    }
}

My update code is kind of similar

public function update(Request $request, $post_id)
{
    $posts = Post::findorFail($post_id);
    if (Auth::user()->id !== $posts->user_id) {
        return response()->json(['status' => 'error', 'message' => 'unauthorized'], 401);
    }
    $post1 = $request->all();
    $posts->fill($post1)->save();
    $image = PostImage::where('post_id', $posts->post_id)->get()->first();
    $images = PostImage::findorFail($image->id);
    if ($request->hasFile('image1')) {
        $image1 = $request->file('image1');
        $image_size1 = $image1->getClientSize();
        $image_ext1 = $image1->getClientOriginalExtension();
        $new_image_name1 = $post_image['image_name1'] . "." . $image_ext1;
        $image1->move($destination_path, $new_image_name1);
        $images->image_name1 = $new_image_name1;
    }
    if ($request->hasFile('image2')) {
        $image2 = $request->file('image2');
        $image_size2 = $image2->getClientSize();
        $image_ext2 = $image2->getClientOriginalExtension();
        $new_image_name2 = $post_image['image_name2'] . "." . $image_ext2;
        $image2->move($destination_path, $new_image_name2);
        $images->image_name2 = $new_image_name2;
    }
    $images->post_id = $post->post_id;
    $images->save();
    return response()->json(['status' => 'success', 'updated_post' => $posts, 'images' => $images], 200);
}

I wanted to achieve update request to work that updates both images and posts if there is an image parameter passed through the postman.

来源:https://stackoverflow.com/questions/55593703/how-to-update-requests-where-multiple-db-are-use-and-images-are-involve

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!