Laravel : SQLSTATE[42S22]: Column not found: 1054 Unknown column

25,402

Solution 1

The error is self explanatory, there is no column with name services_product_id present in services_products table. That's why it is showing the error.

I think the column name in condition is like services_products.id because generally we join table on there primary column and its primary column is id

Solution 2

Posting my comment as answer:

Should it only be "services_products.id"? I did not see the field you mentioned in your migration.

Share:
25,402
Yousef Altaf
Author by

Yousef Altaf

Updated on May 16, 2020

Comments

  • Yousef Altaf
    Yousef Altaf almost 4 years

    I have three tables (services, services_products, services_product_translation)

    when try to insert new product I get this error

    SQLSTATE[42S22]: Column not found: 1054 Unknown column 'services_products.services_product_id' in 'where clause' (SQL: select * from services_products where services_products.services_product_id = 3)

    here is my migrations services migration

    Schema::create('services', function(Blueprint $table)
                {
                    $table->increments('id');
                    $table->binary('image');
                    $table->timestamps();
                });
    

    services_products migration

    Schema::create('services_products', function(Blueprint $table)
            {
                $table->increments('id');
                $table->integer('service_id')->unsigned();
                $table->binary('image');
                $table->binary('pdf');
    
                $table->foreign('service_id')->references('id')->on('services')->onDelete('cascade');
                $table->timestamps();
            });
    

    and this is the translation table

    Schema::create('services_product_translations', function(Blueprint $table)
            {
                $table->increments('id');
                $table->integer('product_id')->unsigned();
                $table->string('title', 150);
                $table->longText('details');
                $table->string('locale')->index();
    
                $table->unique(['product_id', 'locale']);
                $table->foreign('product_id')->references('id')->on('services_products')->onDelete('cascade');
            });
    

    and this is my models

    Service

    class Service extends \Eloquent
    {
    
        use \Dimsav\Translatable\Translatable;
    
        public $translatedAttributes = ['title', 'brief'];
        public $translationModel = 'ServicesTranslation';
    
        public function servicesPro()
        {
            return $this->hasMany('ServicesProduct', 'service_id');
        }
    }
    

    ServicesProduct

    class ServicesProduct extends \Eloquent
    {
        use \Dimsav\Translatable\Translatable;
    
        public $translatedAttributes = ['title', 'details'];
        public $translationModel = 'ServicesProductTranslation';
    
    
        public function services()
        {
            return $this->belongsTo('Service', 'service_id');
        }
    
        public function proImage()
        {
            return $this->hasMany('ServicesProductImage', 'image_id');
        }
    
        public function proVideo()
        {
            return $this->hasMany('ServicesProductVideo', 'video_id');
        }
    

    and this is my controller I used to store

    public function store()
        {
            $sev_id = Input::get('category');
            $file = Input::file('image');
            $pdf = Input::file('pdf');
    
            $destination_path = 'images/servicesProductsImages/';
            $filename = str_random(6) . '_' . $file->getClientOriginalName();
            $file->move($destination_path, $filename);
    
            $destination_path_pdf = 'images/servicesProductsPdf/';
            $filenamePdf = str_random(6) . '_' . $pdf->getClientOriginalName();
            $pdf->move($destination_path_pdf, $filenamePdf);
    
    
            $newSerPro = new ServicesProduct();
    
            $newSerPro->service_id = $sev_id;
            $newSerPro->image = $filename;
            $newSerPro->pdf = $filenamePdf;
            $newSerPro->save();
    
            $localization = Input::get('localization');
            $locales = array_keys($localization);
            foreach ($locales as $locale) {
                if (!in_array($locale, array('en', 'ar'))) {
                    Session::flash('message', 'Lang Error');
                    return Redirect::to('admin/create-service-sub-category');
                }
            }