What I am really trying to do is that i have company which has one Manager which is a customer. Now I want to Display the manager which belongs to that company when i want to show the details of the company. I am saving the Managers id as manager_id in the companies table. When I try to get the Manager through the managers function in the Company model it throws an error. So is there any solution to this problem. Or should I use the simple query method.
This is my company model
class Company extends Model
{
use 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');
}
}
This is my Customers Model
class Customer extends Model
{
use 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',
'preferred_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',
'preferred_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');
}
}
This is what I am trying to do
public function show($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);
}
Company Migration File
class CreateCompaniesTable extends 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');
}
}
Customer Migration
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('preferred_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');
}
}
But I get this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customers.manager_id' in 'where clause' (SQL: select * from
customers
wherecustomers
.manager_id
= 3 andcustomers
.manager_id
is not null andcustomers
.deleted_at
is null limit 1)
Your Company
model has this relationship:
public function customer()
{
return $this->hasOne('\App\Models\Customer', 'manager_id', 'id');
}
This suggests there is a manager_id
column in the customers
table, which there is not. I suspect this should be customer_id
.
You are being far more verbose than is needed. This could simply be defined like so:
public function customer()
{
return $this->hasOne('\App\Models\Customer');
}
Column names will be automatically calculated, saving you from errors.
Likewise, there is no need to enter every column in your $casts
property. Only those that require special casting within Laravel like boolean or array need to be specified. Laravel's timestamp columns (such as deleted_at
) will automatically cast to dates. $table
and $primaryKey
are also automatically determined.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.