SQLSTATE [42S22]:找不到列:1054未知列,其中列存在于我的数据库中

时间:2017-11-21 11:26:14

标签: php laravel-5

我真正想要做的是,我有一个公司,其中有一个经理是客户。现在,当我想显示公司的详细信息时,我想显示属于该公司的经理。我将Managers id保存为companies表中的manager_id。当我尝试通过公司模型中的管理器功能获取管理器时,它会抛出错误。那么这个问题是否有任何解决方案。或者我应该使用简单的查询方法。

这是我的客户模型

类公司扩展了Model {     使用SoftDeletes;

public $table = 'companies';

protected $primaryKey = 'id';
protected $dates = ['deleted_at'];


public $fillable = [
    'name',
    'address_id',
    'street_address',
    'manager_id',
    'service_id'
];

/**
 * The attributes that should be casted to native types.
 *
 * @var array
 */
protected $casts = [
    'name' => 'string',
    'address_id' => 'integer',
    'street_address' => 'string',
    'manager_id' => 'integer',
    'service_id' => 'integer'
];

/**
 * Validation rules
 *
 * @var array
 */
public static $rules = [
    'name' => 'required',
    'street_address' => 'required'
];

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 **/
public function address()
{
    return $this->belongsTo(\App\Address::class, 'address_id', 'id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 **/
public function customer()
{
    return $this->hasOne('\App\Models\Customer', 'manager_id', 'id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 **/
public function service()
{
    return $this->hasOne(\App\Models\Service::class, 'service_id', 'id');
}

}

这是我的客户模型

类Customer扩展Model {     使用SoftDeletes;

public $table = 'customers';

protected $primaryKey = 'id';

protected $dates = ['deleted_at'];


public $fillable = [
    'title',
    'first_name',
    'last_name',
    'email',
    'password',
    'question_id',
    'answer',
    'address_id',
    'street_address',
    'street_address',
    'picture_url',
    'preffered_language',
    'designation',
    'contact_number',
    'company_id'
];

/**
 * The attributes that should be casted to native types.
 *
 * @var array
 */
protected $casts = [
    'title' => 'string',
    'first_name' => 'string',
    'last_name' => 'string',
    'email' => 'string',
    'password' => 'string',
    'question_id' => 'integer',
    'answer' => 'string',
    'address_id' => 'integer',
    'street_address' => 'string',
    'street_address' => 'string',
    'picture_url' => 'string',
    'preffered_language' => 'string',
    'designation' => 'string',
    'contact_number' => 'string',
    'company_id' => 'integer'
];

/**
 * Validation rules
 *
 * @var array
 */
public static $rules = [
    'title' => 'required',
    'first_name' => 'required',
    'last_name' => 'required',
    'email' => 'required',
    'answer' => 'required',
    'contact_number' => 'required'
];

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 **/
public function question()
{
    return $this->belongsTo(\App\Models\Question::class, 'question_id', 'id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 **/
public function address()
{
    return $this->belongsTo(\App\Address::class, 'address_id', 'id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 **/
public function company()
{
    return $this->belongsTo(\App\Models\Company::class, 'company_id', 'id');
}

}

这就是我想要做的事情

公共功能节目($ id)     {         $ company = $ this-> companyRepository-> findWithoutFail($ id);

    if (empty($company)) {
        Flash::error('Company not found');

        return redirect(route('companies.index'));
    }
    $manager=$company->customer;
    return view('companies.show')->with('company', $company);
}

Comapny迁移文件

类CreateCompaniesTable扩展了Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('companies', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('address_id')->unsigned();
        $table->string('street_address');
        $table->integer('manager_id')->unsigned();
        $table->integer('service_id')->unsigned();
        $table->timestamps();
        $table->softDeletes();
        $table->foreign('address_id')->references('id')->on('addresses');
        $table->foreign('manager_id')->references('id')->on('customers');
        $table->foreign('service_id')->references('id')->on('services');
    });
}

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

}

客户迁移

public function up()
{
    Schema::create('customers', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email');
        $table->string('password');
        $table->integer('question_id')->unsigned();
        $table->string('answer', 100);
        $table->integer('address_id')->unsigned();
        $table->text('street_address');
        $table->text('picture_url');
        $table->string('preffered_language');
        $table->string('designation');
        $table->string('contact_number');
        $table->integer('company_id')->unsigned();
        $table->timestamps();
        $table->softDeletes();
        $table->foreign('question_id')->references('id')->on('questions');
        $table->foreign('address_id')->references('id')->on('addresses');
        $table->foreign('company_id')->references('id')->on('companies');
    });
}

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

}

但我收到此错误

SQLSTATE [42S22]:找不到列:1054未知列' customers.manager_id'在' where子句' (SQL:select {from customers customersmanager_id = 3和customersmanager_id不为空且customers。{{1是空限制1)

0 个答案:

没有答案