【问题标题】:How to get the id from another post form when using a multiple image uploader using laravel?使用laravel上传多张图片时如何从另一个帖子表单中获取id?
【发布时间】:2020-06-10 17:03:58
【问题描述】:

我有两种形式的博客文章和多图片上传器,基本上我想在博客文章表单中使用多图片上传器,但是我需要从博客文章表单中获取 ID,所以每个博客文章都有自己独特的一组图片。我知道您可以使用外键来建立两个表之间的链接,但我不确定如何执行此操作。现在博客文章表单只上传单个文件,所以我想要一种方法将多图像上传器逻辑使用到 PostController 中,然后保存到图像表中。非常感谢您的帮助。

图像控制器

    public function store(Request $request)
    {
        if(!$this->validate($request, [
            'id' => 'integer',
            'images.*' => 'sometimes|image|nullable|mimes:jpeg,png,jpg,gif,svg,webp|max:25000',
            'post_id' => 'required'
        ])) {
            return redirect()->back()->with('errors');
        }

        if($request->hasfile('images'))
        {
            foreach($request->file('images') as $image)
            {
                $filenameWithExt = $image->getClientOriginalName();

                $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);

                $extension = $image->getClientOriginalExtension();

                $fileNameToStore = $filename.'_'.time().'.'.$extension;

                $path = $image->storeAs('public/image', $fileNameToStore);

                $image = new Images;
                $image->images = $fileNameToStore;
                $image->post_id = $request->post_id;
                $image->save();
            }
        }

        return back()->with('Images have been uploaded!');
    }

后控制器

    public function store(Request $request)
    {
        // Validate posted form data
        $validated = $request->validate([
            'id' => 'integer',
            'vehicle'    => 'required|string',
            'h1' => 'required|string',
            'page_title' => 'required|string',
            'meta_description' => 'required|string',
            'image' => 'sometimes|image|nullable|max:5000',
            'content' => 'required|string',
            'active' => 'integer',
            'user_id' => 'required'
        ]);

        // Create slug from title
        $validated['slug'] = Str::slug($validated['vehicle'], '-');

        $validated['active'] = isset($request->active[0]) ? 1 : 0;

            if($request->hasFile('image'))
            {
                $filenameWithExt = $request->file('image')->getClientOriginalName();

                $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);

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

                $fileNameToStore = $filename.'_'.time().'.'.$extension;

                $path = $request->file('image')->storeAs('public/image', $fileNameToStore);
            }else {
                $fileNameToStore = null;
            }

        // Create and save post with validated data
        $post = new Post;
        $post->id = $request->input('id');
        $post->vehicle = $request->input('vehicle');
        $post->slug = $request->input('slug');
        $post->h1 = $request->input('h1');
        $post->page_title = $request->input('page_title');
        $post->meta_description = $request->input('meta_description');
        $post->image = $fileNameToStore;
        $post->content = $request->input('content');
        $post->active = $validated['active'];
        $post->user_id = $request->input('user_id');
        $post->slug =  $validated['slug'];
        $post->save();

        // Redirect the user to the created post with a success notification
        return redirect(route('admin.posts.show', $post))->with('notification', 'Post created!');
    }

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您可以将图像存储在您的PostController

    在您的PostController 中删除此部分(在store() 方法中):

            if($request->hasFile('image'))
            {
                $filenameWithExt = $request->file('image')->getClientOriginalName();
    
                $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
    
                $extension = $request->file('image')->getClientOriginalExtension();
    
                $fileNameToStore = $filename.'_'.time().'.'.$extension;
    
                $path = $request->file('image')->storeAs('public/image', $fileNameToStore);
            }else {
                $fileNameToStore = null;
            }
    

    在创建post 之后,放入循环以保存图像。更改此行:

    $image->post_id = $request->post_id;
    

    到这里:

    $image->post_id = $post->id;
    

    首先使用PostController顶部的Images模型。您的新循环应如下所示:

        if($request->hasfile('images'))
        {
            foreach($request->file('images') as $image)
            {
                $filenameWithExt = $image->getClientOriginalName();
    
                $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
    
                $extension = $image->getClientOriginalExtension();
    
                $fileNameToStore = $filename.'_'.time().'.'.$extension;
    
                $path = $image->storeAs('public/image', $fileNameToStore);
    
                $image = new Images;
                $image->images = $fileNameToStore;
                $image->post_id = $post->id;
                $image->save();
            }
        }
    

    然后(根据模型中的关系)您可以通过以下方式访问图像:

    @foreach($post->images as $image)
        <img src="public/image{{ $image->fileNameToStore">
    @endforeach
    

    【讨论】:

    • 但是 $post->id 不是一个未定义的变量吗?我已经进行了这些更改,然后我会将表单从图像视图复制到博客文章视图中吗?
    • 你已经保存了帖子,所以$post-&gt;id 有一个值,它肯定不是未定义的。是的,您可以将图片部分移至帖子表单。
    • 我现在遇到的问题是 post_id 为空,所以它没有检索 ID
    • 保存帖子后可以返回$post-&gt;id吗?
    • 得到它的工作它返回正确的 ID,这是一个缓存问题。所以 images 和 id 正在存储数据库。我收到此图像错误您无权访问此服务器上的 /storage/image/。我正在使用存储链接来存储图像。
    猜你喜欢
    • 2020-04-21
    • 2020-03-23
    • 2011-12-28
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 2017-07-27
    相关资源
    最近更新 更多