Home » Php » Getting error on one to one function on laravel

Getting error on one to one function on laravel

Posted by: admin October 26, 2017 Leave a comment


I want to join tbl_company_list and tbl_commpany_detail using one to one function on Laravel.

@ompany_list table

id (primary_key) | symbol | companyname | sectorid | status

@company_detail table

id (primary_key) | companyid | shares | paidup | baseprice | basepriceRB | operationdate | phone_no | address | website | email | status

I want to join ‘id’ of company_list table to ‘companyid’ of company_detail table.

I want to make one to one relation such that if I delete companay_list then it will automatically delete commpany_detail. And I also want to get companyname of company_list table from company_detail table.

@AdminCompany Model

public static function companydetail(){
    $data = $this->hasOne('App\admin\AdminCompanyDetail');
    return $data; 

@AdminCommpanyDetail Model

public static function company(){
    $data = $this->belongsTo('App\admin\AdminCompany');
    return $data; 

But I am getting error as PHP error:

Using $this when not in object context in
C:\xampp\htdocs\laravel5.5\app\admin\AdminCompanyDetail.php on line 30

@AdminCompany Controller

public function destroy($id)
    $company = AdminCompany::findOrFail($id);

    return redirect(route('company.index'));

What code should I need to add to this controller so that I can delete the companydetail also.


Try this fro AdminCompany model:

public function companydetail()
        return $this->belongsTo(AdminCompanyDetail::class, 'companyid');

And this for the AdminCommpanyDetail

 public function company()
        return $this->hasOne(AdminCompany:class, 'companyid', 'id' );