laravel-vue多个图像不存储在数据库中

发布于 2025-02-08 06:53:02 字数 3182 浏览 1 评论 0原文

我正在尝试上传多个图像。选择图像后,当我提交表格时,我得到了 -

“ {消息:“给定数据无效。”,错误:{媒体:[“需要媒体字段。”] }}“

看起来“媒体[]”字段没有得到该值。但是我在媒体数组中获得了数据(请参阅图像)

“

如何解决此

我的productController代码 -

`public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:255',
            'price' => 'required|integer',
        
        'media'=>'required',

    ]);


    $product = Product::create([
        'title' => $request->title,
        'price' => $request->price,
        
    ]);

    if ($request->image) {
        $imageName = time() . '_' . uniqid() . '.' . $request->image->getClientOriginalExtension();
        $request->image->move(public_path('storage/product'), $imageName);
        $product->image = '/storage/product/' . $imageName;
        $product->save();
    }

    foreach($request->media as $image){
        $from = public_path('tmp/uploads/'.$image);
        $to = public_path('product_images/'.$image);

        File::move($from, $to);
        $product->images()->create([
          'name' => $image,
        ]);
      }
  

    return response()->json($product, 200);
}`

我的ImageController代码是 -

public function store(Request $request){

    $path = public_path('tmp/uploads');

    if (!file_exists($path)) {
      mkdir($path, 0777, true);
    }

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

    $name = uniqid() . '_' . trim($file->getClientOriginalName());

    $file->move($path, $name);

    return ['name'=>$name];
  }

  public function getImages(Product $product){
    $images = $product->images;
    return ['media'=>$images];
  }

api.php--

Route::resource('product','ProductController');
Route::post('/upload', [ImageController::class, 'store'])->name('upload');
Route::get('/media/{product}', [ImageController::class, 'getImages'])->name('product.images');

php---------php-------

import axios from 'axios'
export default {
    data(){
        return{
            media:[],
            loading:false,
        }
    },
    methods:{
        async fileChange(event){
            this.loading=true
            let files = event.target.files
            for(var i=0; i < files.length; i++){
                let formData = new FormData
                let url = URL.createObjectURL(files[i])
                formData.set('image', files[i])
                const {data} = await axios.post(this.server, formData)
                
            this.media.push({url:url, name:data.name, size:files[i].size, type:files[i].type});
                
                
        }
        this.loading=false
        this.media_emit()
    },
    remove(index){
        this.media.splice(index,1)
        this.media_emit()
    },
    media_emit(){
        this.$emit('media',this.media)
    }
},
mounted() {
    this.$emit('media',this.media)
},

i am trying to upload multiple images.after selecting images when i submit the form then i got this--

"{message: "The given data was invalid.", errors: {media: ["The media field is required."]}}"

it looks like the "media[]" field is not getting the value. But i got the data in media array(see the image)

vue devtool

how can i solve this

my productController code-

`public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:255',
            'price' => 'required|integer',
        
        'media'=>'required',

    ]);


    $product = Product::create([
        'title' => $request->title,
        'price' => $request->price,
        
    ]);

    if ($request->image) {
        $imageName = time() . '_' . uniqid() . '.' . $request->image->getClientOriginalExtension();
        $request->image->move(public_path('storage/product'), $imageName);
        $product->image = '/storage/product/' . $imageName;
        $product->save();
    }

    foreach($request->media as $image){
        $from = public_path('tmp/uploads/'.$image);
        $to = public_path('product_images/'.$image);

        File::move($from, $to);
        $product->images()->create([
          'name' => $image,
        ]);
      }
  

    return response()->json($product, 200);
}`

my imageController code is--

public function store(Request $request){

    $path = public_path('tmp/uploads');

    if (!file_exists($path)) {
      mkdir($path, 0777, true);
    }

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

    $name = uniqid() . '_' . trim($file->getClientOriginalName());

    $file->move($path, $name);

    return ['name'=>$name];
  }

  public function getImages(Product $product){
    $images = $product->images;
    return ['media'=>$images];
  }

api.php--

Route::resource('product','ProductController');
Route::post('/upload', [ImageController::class, 'store'])->name('upload');
Route::get('/media/{product}', [ImageController::class, 'getImages'])->name('product.images');

vue component---

import axios from 'axios'
export default {
    data(){
        return{
            media:[],
            loading:false,
        }
    },
    methods:{
        async fileChange(event){
            this.loading=true
            let files = event.target.files
            for(var i=0; i < files.length; i++){
                let formData = new FormData
                let url = URL.createObjectURL(files[i])
                formData.set('image', files[i])
                const {data} = await axios.post(this.server, formData)
                
            this.media.push({url:url, name:data.name, size:files[i].size, type:files[i].type});
                
                
        }
        this.loading=false
        this.media_emit()
    },
    remove(index){
        this.media.splice(index,1)
        this.media_emit()
    },
    media_emit(){
        this.$emit('media',this.media)
    }
},
mounted() {
    this.$emit('media',this.media)
},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2025-02-15 06:53:02

您可以尝试:

$this->validate($request, [
    'title' => 'required|max:255',
    'price' => 'required|integer',        
    'media.*' => 'required|mimes:jpg,jpeg,png|max:20000']);

它还验证它是具有这些扩展名的图像。

You could try:

$this->validate($request, [
    'title' => 'required|max:255',
    'price' => 'required|integer',        
    'media.*' => 'required|mimes:jpg,jpeg,png|max:20000']);

It also validates it is a an image with those extensions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文