Search code examples
laravellaravel-seeding

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


I am getting this error when I try to seed data into the database. I have the bookings table and a bookings package table, seeder, factory and models, and also a pivot table that links them.

here is my bookingpackage seeder

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

here's 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'=>'[email protected]',
            'date'=>'07/02/2021',
            'event_id'=>'5',
            'event_details'=>'we would like to book you for a wedding'

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

my bookingpackages table looks like this

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

where might have I gone wrong in my code.


Solution

  • The title is very vague but i think error is in this line

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

    first check bookingpack() method is exist in Bookings model

    and then modify this line with:

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

    after that replace first query in bookingseeder with:

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

    or you can write "where" query another way like:

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