-1

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);
    $company->delete();

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

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

Transcendent
  • 5,598
  • 4
  • 24
  • 47
sudeepsth
  • 42
  • 2
  • 14
  • kindly learn the relationship in eloquent from https://laravel.com/docs/5.5/eloquent-relationships because the way you are using it is not the proper way to use it. – Dhaval Purohit Oct 26 '17 at 06:16
  • You can just return it not needed to store it in `$data` variable. – Ajay Makwana Oct 26 '17 at 06:18
  • This link might help https://stackoverflow.com/questions/14174070/automatically-deleting-related-rows-in-laravel-eloquent-orm – Ajay Makwana Oct 26 '17 at 06:19

1 Answers1

0

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' );
    }
Onix
  • 2,129
  • 2
  • 17
  • 26