php - 拉维尔 : SQLSTATE[42S22]: Column not found: 1054 Unknown column

标签 php mysql laravel eloquent database-migration

我有三个表(services、services_products、services_product_translation)

当尝试插入新产品时出现此错误

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)

这是我的迁移 服务迁移

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

服务_产品迁移

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

这是翻译表

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

这是我的模型

服务

class Service extends \Eloquent
{

    use \Dimsav\Translatable\Translatable;

    public $translatedAttributes = ['title', 'brief'];
    public $translationModel = 'ServicesTranslation';

    public function servicesPro()
    {
        return $this->hasMany('ServicesProduct', 'service_id');
    }
}

服务产品

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

这是我用来存放的 Controller

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

最佳答案

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

我认为 condition 中的列名类似于 services_products.id 因为通常我们在主列上连接表,它的主列是 id

关于php - 拉维尔 : SQLSTATE[42S22]: Column not found: 1054 Unknown column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40600550/

相关文章:

php mysqli 受影响的行在删除语句后返回错误结果

mysql - 无法创建 FOREIGN KEY MySQL 错误 150

javascript - 混合内容 jQuery ajax HTTPS 请求已在 Laravel 上被阻止

php - 在 Laravel 4 中刷新所有缓存

mysql - 访问托管在 minikube 集群之外的 Mysql 数据库

javascript - 如何提交 "vue-multiselect "选定选项 - Vue.js

php - 把自己设定为初学者练习PHP有什么好的项目?

php - 可以将 Smarty 输出到 PHP 常量吗?

php - 我应该在 php 中缓存整个查询还是应该只缓存行的主键?

php - 如何触发 EventSource SSE 事件?