Laravel:SQLSTATE[42S22]:未找到列:1054 未知列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40600550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:44:48  来源:igfitidea点击:

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

phpmysqllaraveleloquentdatabase-migration

提问by Yousef Altaf

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_productswhere services_products.services_product_id= 3)

SQLSTATE[42S22]:未找到列:1054 未知列“services_products.services_product_id”在“where 子句”中(SQL:select * from services_productswhere 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

services_products 迁移

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

回答by Mayank Pandeyz

The error is self explanatory, there is no column with name services_product_idpresent in services_productstable. That's why it is showing the error.

错误是不言自明的,表中不services_product_id存在具有名称的列services_products。这就是它显示错误的原因。

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

我认为条件中的列名就像services_products.id是因为通常我们在主列上连接表,其主列是id

回答by Ronald

Posting my comment as answer:

发表我的评论作为答案:

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

它应该只是“services_products.id”吗?我没有看到您在迁移中提到的字段。