0

[submit function][1]error message while insertingfunction for inserting tagserror while inserting into database many tags using foreign key .I am getting problem in array , it is unable to insert many tags

 public function uploadSubmit(UploadRequest $request)
{
    $product = new Product();
    $product->name = $request['name'];
    $product->save();



    //$product = Product::create($request->all());
    foreach ($request->photos as $photo) {
        $filename = $photo->store('photos');
        ProductsPhoto::create([
            'product_id' => $product->id,
            'filename' => $filename
        ]);

    }
    $tags = array();
    $tags = array($request['tagname[]']);
    foreach ($tags as $tag) {


        ProductTag::create([
            'tag_id' => $product->id,
            'tagname' => $tag,
        ]);
    }
    $message = 'Upload successful!';
    return redirect('/upload')->with('msg' , $message);
}

[Environment & details:[][2]][sql error 3]

4

1 回答 1

0

好的 - 所以在了解了更多关于您当前设置的信息后,我认为您将从将其重新构建为以下内容中受益:

  1. 模型Producttags()关系方法:
public function tags(): MorphToMany
{
    return $this->morphToMany(Tag::class, 'taggable');
}
  1. 添加中间表taggable
php artisan make:migration create_taggables_table --create=taggables
class CreateTaggablesTable extends Migration
{
    public function up()
    {
        Schema::create('taggables', function (Blueprint $table) {
            $table->unsignedInteger('tag_id');
            $table->unsignedInteger('taggable_id');
            $table->string('taggable_type');
        });
    }

    public function down()
    {
        Schema::dropIfExists('taggables');
    }
}
  1. 更新您的控制器以添加新标签,然后将它们与产品关联:
public function uploadSubmit(UploadRequest $request)
{
    $product = new Product;
    $product->name = $request->name;
    $product->save();


    $photos = collect($request->photos)->map(function($photo) use ($product) {
        return [
            'product_id' => $product->id,
            'filename' => $photo->store('photos')
        ];
    })->toArray();

    ProductsPhoto::insert($photos);


    $tagsInUse = Tag::whereIn('name', $request->tagname);

    if (count($request->tagname) > $tagsInUse->count()) {

        $newTags = collect($request->tagname)->diff($tagsInUse->pluck('name')->values())->map(function (string $tag) {
            return ['name' => $tag];
        });

        Tag::insert($newTags->toArray());

        $tagIds = Tag::whereIn('name', $request->tagname)->pluck('id')->values();

    } else {

        $tagIds = $tagsInUse->pluck('id')->values();

    }

    $product->tags()->attach($tagIds);


    return redirect('/upload')->with('msg' , 'Upload successful!');
}

这是此设置的测试:

public function test()
{
    factory(Tag::class)->create(['name' => 'one']);
    factory(Tag::class)->create(['name' => 'two']);
    factory(Tag::class)->create(['name' => 'three']);

    $this->assertEquals(
        ['one', 'two', 'three'],
        Tag::all()->pluck('name')->toArray()
    );


    $requestTags = new Collection(['one', 'three', 'four', 'five']);

    $inUse = Tag::whereIn('name', $requestTags)->pluck('name')->values();

    $newTags = collect($requestTags)->diff($inUse)->map(function(string $tag) {
        return ['name' => $tag];
    });

    Tag::insert($newTags->toArray());

    $this->assertEquals(
        ['one', 'two', 'three', 'four', 'five'],
        Tag::all()->pluck('name')->toArray()
    );


    $product = factory(Product::class)->create(['name' => 'Bike']);

    $this->assertEmpty($product->tags);


    $tagIds = Tag::whereIn('name', $requestTags)->pluck('id')->values();

    $product->tags()->attach($tagIds);

    $this->assertEquals(
        ['one', 'three', 'four', 'five'],
        $product->fresh('tags')->tags->pluck('name')->toArray()
    );
}

参考:https ://laravel.com/docs/5.6/eloquent-relationships#many-to-many-polymorphic-relations

于 2018-06-05T04:35:42.460 回答