首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Laravel 5.6.17Model hasOne支持多个表

Laravel 5.6.17Model hasOne支持多个表
EN

Stack Overflow用户
提问于 2018-10-17 21:49:43
回答 1查看 1.8K关注 0票数 2

我对Laravel非常陌生,现在我正在尝试将以前的应用程序的一部分从一个小型的自编写框架迁移到Laravel中。地址簿是多语种的,因此表结构有点复杂。

db-structure

这是我的源代码:

  • AddressBookController.php

        namespace App\Http\Controllers;

        use App\AddressBook as AB;
        use Illuminate\Http\Request;
        use Illuminate\Support\Facades\Validator;

        class AddressBookController extends Controller
        {
            /**
             * Display a listing of the resource.
             *
             * @return \Illuminate\Http\Response
             */
            public function index()
            {
                $entries = AB::all();

                return view('addressBook')->with([
                    'class' => __CLASS__,
                    'function' => __FUNCTION__,
                    'line' => __LINE__,
                    'entries' => $entries,
                ]);
            }
        }

  • 模型AddressBook.php

命名空间应用;使用照明\数据库\Eloquent\模型;类AddressBook扩展模型{ protected $table = 'address';protected $primaryKey = 'address_id';protected $keyType = 'int';public $incrementing = true;public $timestamps = false;protected $searchable =[‘=> 'address.address_surname’=> 10,'address.address_company‘=> 5,'address.address_vatid’=> 2,,];公共函数country() { return $this->hasOne('country','country_id','country_id');}公共函数addresstype() { return $this->hasOne('addresstype','addresstype_id','addresstype_id');}}

  • 模型Country.php

命名空间应用;使用照明\数据库\Eloquent\模型;类国家扩展模型{ protected $table = ' Country ';protected $primaryKey = 'country_id';protected $keyType = 'int';public $incrementing = true;public $timestamps = false;public function translation() { return $this->hasOne('translations','translations_id','translations_id');} public function addressbook() { return $this->belongsTo('address','country_id','country_id');}}

  • 模型AddressType

命名空间应用;使用照明\数据库\Eloquent\模型;类转换扩展模型{ protected $table = ' AddressType ';protected $primaryKey = 'addresstype_id';protected $keyType = 'int';public $incrementing = true;public $timestamps = false;public function translation() { return $this->hasOne('translations','translations_id','translations_id');}公共函数addressbook() { return $this->belongsTo('address','addresstype_id','addresstype_id');}}

  • 模型Translation.php

命名空间应用;使用照明\数据库\Eloquent\模型;类转换扩展模型{ protected $table = 'translations';protected $primaryKey = 'translations_id';protected $keyType = 'int';public $incrementing = true;public $timestamps = false;public function country() { return $this-> extends (‘country’,'translations_id','translations_id');} public function addresstype() { return $this->belongsTo('addresstype','translations_id','translations_id');} }

请求"$entries = AB::all();“通常可以工作,但我得到了id,也许我在这里完全错了,但我认为来自外键的数据将被相应的模型替换(如果配置正确)。所以我的问题是:

答:我是否在配置过程中犯了错误?如果是,错误具体在哪里?

我用对象替换id的假设是完全错误的吗?

提前感谢!史蒂夫

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-18 00:16:14

Laravel Eloquent模型不会用关系数据替换活动记录的外键,它只附加一个与关联类的方法具有相同名称的新属性,并在该属性中放置结果查询的所有Model实例,这仅当您访问该属性时才被称为急切加载。

It is explained here (Ofiicial Documentation)

$addressBook = App\AddressBook::find(1); //this only will return the active record with the id 1  and nothig more.

$addressBook->country; // The property "country" does not exist in the AddressBook Classs but eloquent models will return a "fake" property with the value of the result of the query by the method with the same name (only if the method returns an Eloquent Relation).

这种雄辩的行为是自然的,是一种非常聪明的方式来最小化查询的数量,如果没有必要,Eloquent永远不会加载关系。

如果您希望在从数据库检索模型的同时加载集合模型中的关系,则需要显式指定要加载的关系。

$addressBook = App\AddressBook::with(['country', 'addresstype', 'anotherRelation'])->get(); // this will retrive all the addressBook models and in each one will attach the relations specified.

编辑后,您必须将相关模型类的整个命名空间放在关系方法中,因此您需要替换为:

    class Translation extends Model
    {
        protected $table = 'translations';
        protected $primaryKey = 'translations_id';
        protected $keyType = 'int';
        public $incrementing = true;
        public $timestamps = false;

        // ****** You need to put the entire namespace of the Model class
        public function country() {
            return $this->belongsTo('App\Country', 'translations_id', 'translations_id');
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52856522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档