我想使用laravel显示与名称相关的描述

Hi I am a beginner at laravel, I have two tables and i want to show a description related to the name and I have already made the relation from the model but How to fetch data related to the name please help me thanks. mf_facilities_categories tablesm_facilities table Model Facility

                       class Facility extends Model
                       {
                       protected $table="sm_facilities";
                       protected $guarded = ['id','created_at','updated_at'];

                       public function categories()
                       {
                       return $this->hasMany('App\FacilityCategory','mf_facility_category_id','id');
                       }
                        }

示范设施类别

                      class FacilityCategory extends Model
                      {
                      protected $table = 'mf_facilities_categories';
                      public function facilities()
                      {
                      return $this->hasMany('App\Facility','mf_facility_category_id','id');
                      }

控制者

           class FacilityController extends Controller
           {
           public function index()
           {
           $data=[
         'facility'=>Facility::with('categories')->where('status',1)->get()
          ];
          return view('facility');
          }
          }
评论
  • qquas
    qquas 回复

    you can have one-to-one relationship

    class FacilityCategory extends Model {
        ....
        public function facility() {
             return this->hasOne('App\Facility');
        }
    
    }