当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to save multiple file name to database?

问题描述:

Here is my code:

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

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. But it should be a array of id of 2 or more images IDs. 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().

Here's an example from the Laravel documentation.

$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().

$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

EDIT

About your situation.

First of all, I would move the

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

right before the for cycle. After the Product creation, I would simply do, in your for cycle, this:

$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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: