简体   繁体   English

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

[英]How to save multiple file name to database?

Here is my code: 这是我的代码:

ProductController.php 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 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 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 架构图

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']);
});

Now how can I attach multiple images with product ? 现在如何在产品上附加多张图像? Please give me the solution and if it needs to change anything please let me know. 请给我解决方案,如果需要更改任何内容,请告诉我。 Thank you..... 谢谢.....

I have updated to this code 我已更新为此代码

 $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);

Now how can I attach multiple images with product ? 现在如何在产品上附加多张图像? I am getting only the last image id. 我只得到最后一个图像ID。 But it should be a array of id of 2 or more images IDs. 但是它应该是2个或更多图像ID的ID数组。 Please give me the solution and if it needs to change anything please let me know. 请给我解决方案,如果需要更改任何内容,请告诉我。 Thank you. 谢谢。

You have many ways to do that. 您有很多方法可以做到这一点。 The easier one is using relationships methods and then save() . 比较容易的一种是使用关系方法,然后使用save()

Here's an example from the Laravel documentation. 这是Laravel文档中的示例。

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

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

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

If you have many instances of the "many" part of the relation, you can use saveMany() . 如果关系的“许多”部分有很多实例,则可以使用saveMany()

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

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

This solution above should be perfect for your needs. 上面的解决方案应该非常适合您的需求。

Also, I suggest you to rename your models using the singular. 另外,建议您使用单数来重命名模型。 Not Images , but Image . 不是图片 ,而是图片 It will be more easier working with conventions. 使用约定将更加容易。

More info here: http://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models 此处提供更多信息: http : //laravel.com/docs/5.1/eloquent-relationships#inserting-related-models

EDIT 编辑

About your situation. 关于你的情况。

First of all, I would move the 首先,我将

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

right before the for cycle. for循环之前。 After the Product creation, I would simply do, in your for cycle, this: 创建产品后,我将在您的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);

By doing this you automatically set-up your relationship. 这样,您可以自动建立关系。

That

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

is the key passage. 是关键的段落。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM