SQLSTATE[42000]:语法错误或访问冲突:1064 将数据播种到数据库时,您的 SQL 语法错误出现错误

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax error when seeding data to db

当我尝试将数据播种到数据库中时出现此错误。我有预订 table 和一个预订包 table、播种机、工厂和模型,还有一个链接它们的枢轴 table。

这是我的预订包播种机

<?php

namespace Database\Seeders;

use App\Models\bookingpackage;
use Illuminate\Database\Seeder;

class BookingpackageSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {   
        bookingpackage::create(['package_name'=>'Dj and Full Sound System',
                                'package_description'=>'Dj and Full Sound System.Dj and Full Sound System',
                                'package_price'=>'sh 80,000']);
    }
}

这是 bookingseeder

$fullpackage=bookingpackage::where('package_name','Dj and Full Sound System',
                                            'package_description','Dj and Full Sound System.Dj and Full Sound System',
                                            'package_price','sh 80,000')->first();
    
        $fullsetpackage=Bookings::create([
            'full_name'=>'Best Promoters',
            'location'=>'Nairobi',
            'phone'=>'0792492584',
            'is_booking'=>2,
            'package_id'=>1,
            'email'=>'Werupromoter@admin.com',
            'date'=>'07/02/2021',
            'event_id'=>'5',
            'event_details'=>'we would like to book you for a wedding'

        ]);
        
        $fullpackage->bookingpack()->attach($fullsetpackage);

我的预订套餐 table 看起来像这样

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBookingpackagesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('bookingpackages', function (Blueprint $table) {
            $table->id();
            $table->string('package_name');
            $table->text('package_description');
            $table->string('package_price');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('bookingpackages');
    }
}

我的代码哪里可能出错了。

标题很模糊,但我认为错误在这一行

$fullpackage->bookingpack()->attach($fullsetpackage);

首先检查 bookingpack() 方法存在于 Bookings 模型中

然后修改这一行:

$fullpackage->bookingpack()->attach($fullsetpackage->id);

之后将 bookingseeder 中的第一个查询替换为:

$fullpackage=bookingpackage::where("package_name", 'Dj and Full Sound System')
    ->where("package_description", "Dj and Full Sound System.Dj and Full Sound System")
    ->where("package_price", "sh 80,000")
    ->first();

或者你可以用另一种方式写“where”查询:

$fullpackage=bookingpackage::where([
    ['package_name', "=", 'Dj and Full Sound System'],
    ['package_description', "=", 'Dj and Full Sound System.Dj and Full Sound System'],
    ['package_price', "=", 'sh 80,000'],
])->first();