如何将多个文件名保存到数据库?

How to save multiple file name to database?

这是我的代码:

ProductController.php

$this->validate($request, [ 'name' => 'required', 'size' => 'required', 'color' => 'required', 'description' => 'required', 'category' => 'required', 'images' => 'required', ]);

    $id = [];
    $a = [];   

    for($i = 0; $i < count($input['images']); $i++){
      $name = rand(1,1000).'.'.$input['images'][$i]->getClientOriginalExtension();
      $path = $input['images'][$i]->move('products/images', $name);

      $images->name = $name;
      $images->path = $path;

      $images->save();

      $a[] = $name;    
    }

    $d = Product::create($input);
    $d->category()->attach($request->get('category'));

    $x = [];

    for($j = 0; $j < count($a); $j++){
        $x = $images->where('name', '=', $a[$j])->get();
        // $x[] = $a[$j];
    }

    dd($x);
    // $d->images()->attach($x);

Images.php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Images extends Model { protected $table = 'images';

protected $fillable = ['name','path'];

public function product()
{
  return $this->belongsToMany('App\Product', 'product_images', 'pro_id', 'img_id');
}
}

Product.php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Product extends Model { protected $table = 'product';

protected $fillable = ['name', 'description', 'size', 'color'];

public function category()
{
  return $this->belongsToMany('App\Category', 'product_category', 'pro_id', 'cat_id');
}

public function images()
{
  return $this->belongsToMany('App\Images', 'product_images', 'pro_id', 'img_id');
}
}

架构

Schema::create('images', function(Blueprint $table){
        $table->increments('id');
        $table->string('name');
        $table->string('path');
        $table->timestamps();
});

Schema::create('product_images', function(Blueprint $table){
        $table->integer('pro_id')->unsigned();
        $table->integer('img_id')->unsigned();

        $table->foreign('pro_id')
              ->references('id')
              ->on('product')
              ->onUpdate('cascade')
              ->onDelete('cascade');

        $table->foreign('img_id')
              ->references('id')
              ->on('images')
              ->onUpdate('cascade')
              ->onDelete('cascade');

        $table->primary(['pro_id', 'img_id']);
});

现在如何为产品附加多张图片?请给我解决方案,如果需要更改任何内容,请告诉我。谢谢.....

我已经更新到这个代码

 $d = Product::create($input);
        $d->category()->attach($request->get('category'));

        $a = [];

        for($i = 0; $i < count($input['images']); $i++){
          $name = rand().'.'.$input['images'][$i]->getClientOriginalExtension();
          $path = $input['images'][$i]->move('products/images', $name);

          $images->name = $name;
          $images->path = $path;

          $images->save();

          $a[] = $images->id;

        }

        dd($a);

        $d->images()->sync($a);

现在如何为产品附加多张图片?我只得到最后一个图像 ID。但它应该是 2 个或更多图像 ID 的 id 数组。请给我解决方案,如果需要更改任何内容,请告诉我。谢谢。

你有很多方法可以做到这一点。更简单的方法是使用关系方法,然后 save().

这是 Laravel 文档中的示例。

$comment = new App\Comment(['message' => 'A new comment.']);

$post = App\Post::find(1);

$comment = $post->comments()->save($comment);

如果关系的 "many" 部分有很多实例,您可以使用 saveMany().

$post = App\Post::find(1);

$post->comments()->saveMany([
    new App\Comment(['message' => 'A new comment.']),
    new App\Comment(['message' => 'Another comment.']),
]);

上述解决方案应该能完美满足您的需求。

此外,我建议您使用单数重命名您的模型。不是 Images,而是 Image。使用约定会更容易。

更多信息在这里:http://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models

编辑

关于你的情况。

首先,我会移动

$d = Product::create($input);

就在 for 循环之前。在创建 Product 之后,我会简单地在您的 for 周期中这样做:

$d = Product::create($input);
$imagesIdsArray = [];

for($i = 0; $i < count($input['images']); $i++){
    $name = rand(1,1000).'.'.$input['images'][$i]->getClientOriginalExtension();
    $path = $input['images'][$i]->move('products/images', $name);

    $images = new Image;

    $images->name = $name;
    $images->path = $path;

    $images->save();

    $imagesIdsArray[] = $images->id;
}

$d->images()->sync($imagesIdsArray);

通过这样做,您自动建立了您的关系。

那个

$d->images()->sync($imagesIdsArray);

是关键段落。

希望对您有所帮助。