使用 laravel 使用多图片上传器时如何从另一个帖子表单获取 id?

我有博客文章和多图片上传器两种形式,基本上我想在博客文章表单中使用多图片上传器,但是我需要从博客文章表单中获取 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'

        ]);


慕标琳琳
浏览 111回答 1
1回答

子衿沉夜

您可以将图像存储在您的PostController.PostController在您的(store()方法中)中删除此部分:&nbsp; &nbsp; &nbsp; &nbsp; if($request->hasFile('image'))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filenameWithExt = $request->file('image')->getClientOriginalName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $extension = $request->file('image')->getClientOriginalExtension();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fileNameToStore = $filename.'_'.time().'.'.$extension;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $path = $request->file('image')->storeAs('public/image', $fileNameToStore);&nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fileNameToStore = null;&nbsp; &nbsp; &nbsp; &nbsp; }并在创建post循环后保存图像。更改此行:$image->post_id = $request->post_id;对此:$image->post_id = $post->id;首先使用Images顶部的模型PostController。您的新循环应如下所示:&nbsp; &nbsp; if($request->hasfile('images'))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach($request->file('images') as $image)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filenameWithExt = $image->getClientOriginalName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $extension = $image->getClientOriginalExtension();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fileNameToStore = $filename.'_'.time().'.'.$extension;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $path = $image->storeAs('public/image', $fileNameToStore);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = new Images;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image->images = $fileNameToStore;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image->post_id = $post->id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image->save();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }然后(基于模型中的关系)您可以使用如下方式访问图像:@foreach($post->images as $image)&nbsp; &nbsp; <img src="public/image{{ $image->fileNameToStore">@endforeach
打开App,查看更多内容
随时随地看视频慕课网APP