Попытка получить свойство не-объекта Laravel hasMany Relation - PullRequest
1 голос
/ 05 июля 2019

Я довольно новичок в laravel, и я узнаю об отношениях в eloquent.У меня есть 3 модели

Report
ReportModule
ReportSubModule

Отношения такие

SubModule hasMany Module
Report hasMany ReportModule
Report hasMany ReportSubModule

Когда я пытаюсь получить объект ReportSubModule из Report на мой взгляд, у меня нет ошибки, но когда я пытаюсьчтобы получить ReportModule от Report объекта, на мой взгляд, я получаю ошибку Trying to get property of non-object.Если я печатаю объект ReportModule, я вижу json и думаю, что получаю объект, но не могу получить его свойство

Вот мой код

Пространство имен Report (Model) App;

use Illuminate\Database\Eloquent\Model;

class Report extends Model
{
    public function report_module(){
        //module_id is column in reports table that corresponds to 
        //id column in report_modules table
        return $this->belongsTo(ReportModule::class,'module_id');
    }


    public function sub_module(){
        //sub_module_id is column in reports table that corresponds to 
        //id column in report_modules table
        return $this->belongsTo(ReportSubModule::class,'sub_module_id');
    }


}

ReportModule (модель)

namespace App;

use Illuminate\Database\Eloquent\Model;

class ReportModule extends Model
{
    public function subModules(){
        return $this->hasMany(ReportSubModule::class);
    }

    public function reports(){
        return $this->hasMany(Report::class);
    }
}

ReportSubModule (модель)

namespace App;

use Illuminate\Database\Eloquent\Model;

class ReportSubModule extends Model
{
    public function module(){
        return $this->belongsTo(ReportModule::class);
    }
    public function reports(){
        return $this->hasMany(Report::class);
    }
}

view

<a href="{{route('reports.edit',$report->id)}}"><h4> {{$report ->report_name}} </h4></a>
            <small>{{ $report->sub_module->sub_module_name }} </small><br/><br/>
            <?php
                $m = $report->report_module;
                <!-- i get error on below line -->
                 echo $m->module_name;
            ?>

Что я делаю неправильноздесь. Я должен иметь возможность получить свойство объекта ReportModule, как и ReportSubModule, но это не так. Пожалуйста, помогите мне решить эту проблему.

1 Ответ

1 голос
/ 05 июля 2019
I assume you have the relationship like this - 

Report id(primary key) -> ReportModule report_id(foreign key)
ReportModule id(primary key) -> ReportSubModule report_module_id(foreign key)

You will have the relation ship in each Model like below -

1) Report Model - 
public function reportModule(){
 return $this->hasMany(ReportModule::class,'report_id'); //you can use has one as well if required
}

2) Report Model - 
//relationship with report table
public function module(){
 return $this->belongsTo(Report::class,'report_id');
}

//relationship with report sub module
public function reportSubModule(){
 return $this->hasMany(ReportSubModule::class,'report_module_id); // you can use has one if required
}

и поэтому вы можете создавать отношения ..

надеюсь, что это сработает для вас

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...