簡體   English   中英

如何將多個文件名保存到數據庫?

[英]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);

如果關系的“許多”部分有很多實例,則可以使用saveMany()

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

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

上面的解決方案應該非常適合您的需求。

另外,建議您使用單數來重命名模型。 不是圖片 ,而是圖片 使用約定將更加容易。

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

編輯

關於你的情況。

首先,我將

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

for循環之前。 創建產品后,我將在您的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);

是關鍵的段落。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM