1. <tfoot id='Kg2W4'></tfoot>
      <i id='Kg2W4'><tr id='Kg2W4'><dt id='Kg2W4'><q id='Kg2W4'><span id='Kg2W4'><b id='Kg2W4'><form id='Kg2W4'><ins id='Kg2W4'></ins><ul id='Kg2W4'></ul><sub id='Kg2W4'></sub></form><legend id='Kg2W4'></legend><bdo id='Kg2W4'><pre id='Kg2W4'><center id='Kg2W4'></center></pre></bdo></b><th id='Kg2W4'></th></span></q></dt></tr></i><div id='Kg2W4'><tfoot id='Kg2W4'></tfoot><dl id='Kg2W4'><fieldset id='Kg2W4'></fieldset></dl></div>

      <small id='Kg2W4'></small><noframes id='Kg2W4'>

    2. <legend id='Kg2W4'><style id='Kg2W4'><dir id='Kg2W4'><q id='Kg2W4'></q></dir></style></legend>

        <bdo id='Kg2W4'></bdo><ul id='Kg2W4'></ul>
    3. Laravel 获取与其属性同名的 Eloquent 关系

      时间:2023-09-22
    4. <tfoot id='Aqf4O'></tfoot>

      <i id='Aqf4O'><tr id='Aqf4O'><dt id='Aqf4O'><q id='Aqf4O'><span id='Aqf4O'><b id='Aqf4O'><form id='Aqf4O'><ins id='Aqf4O'></ins><ul id='Aqf4O'></ul><sub id='Aqf4O'></sub></form><legend id='Aqf4O'></legend><bdo id='Aqf4O'><pre id='Aqf4O'><center id='Aqf4O'></center></pre></bdo></b><th id='Aqf4O'></th></span></q></dt></tr></i><div id='Aqf4O'><tfoot id='Aqf4O'></tfoot><dl id='Aqf4O'><fieldset id='Aqf4O'></fieldset></dl></div>
        • <bdo id='Aqf4O'></bdo><ul id='Aqf4O'></ul>

          <small id='Aqf4O'></small><noframes id='Aqf4O'>

              <tbody id='Aqf4O'></tbody>
                <legend id='Aqf4O'><style id='Aqf4O'><dir id='Aqf4O'><q id='Aqf4O'></q></dir></style></legend>
                本文介绍了Laravel 获取与其属性同名的 Eloquent 关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有这样的数据库表:

                shoot: id, name, programme
                programme: id, name
                

                拍摄中的雄辩关系是这样定义的:

                The eloquent relationship in the shoot is defined like this:

                public function programme() {
                    return $this->belongsTo('AppProgramme', 'programme', 'id');
                }
                

                使用 dd() 时,我可以看到这是正常工作的:

                When using dd(), I can see this is working correctly:

                dd(Shoot:where('id','=',1)->with('programme')->first());
                // prints the object with programme listed under the relationship
                

                但是,当我急切加载拍摄并尝试获取程序对象时,我反而检索了拍摄属性程序".例如:

                However when I eager-load the shoot and attempt to get the programme object, I retrieve the shoot attribute "programme" instead. E.g.:

                $shoot = Shoot:where('id','=',1)->with('programme')->first();
                echo $shoot->programme; // returns 1, not AppProgramme object.
                

                有没有无需重写大量代码库的解决方案?

                Is there a solution to this without having to rewrite masses of the codebase?

                推荐答案

                relationshipcolumn 名称不应使用相同的名称,否则您将将始终收到 column 名称,因此尝试编辑其中之一,我认为这里最简单的是 relationship 名称:

                You shouldn't use the same name for the both relationship and column name, else you'll receive always the column name so try to edit one of them, I think the easiest one here is the relationship name :

                public function programmeObj() {
                    return $this->belongsTo('AppProgramme', 'programme', 'id');
                }
                

                然后将其称为:

                echo $shoot->programmeObj;
                

                注意:但如果你想遵循约定,你应该用 programme_id 替换 name 属性,所以:

                NOTE : But if you want to follow conventions you should replace the name attribute by programme_id so :

                public function programme() {
                    return $this->belongsTo('AppProgramme', 'programme_id', 'id');
                }
                

                希望这会有所帮助.

                这篇关于Laravel 获取与其属性同名的 Eloquent 关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel UTF-8 转数据库 下一篇:Hash::make('password') 在每次调用时返回不同的

                相关文章

                最新文章

                  <i id='8h0mz'><tr id='8h0mz'><dt id='8h0mz'><q id='8h0mz'><span id='8h0mz'><b id='8h0mz'><form id='8h0mz'><ins id='8h0mz'></ins><ul id='8h0mz'></ul><sub id='8h0mz'></sub></form><legend id='8h0mz'></legend><bdo id='8h0mz'><pre id='8h0mz'><center id='8h0mz'></center></pre></bdo></b><th id='8h0mz'></th></span></q></dt></tr></i><div id='8h0mz'><tfoot id='8h0mz'></tfoot><dl id='8h0mz'><fieldset id='8h0mz'></fieldset></dl></div>
                1. <legend id='8h0mz'><style id='8h0mz'><dir id='8h0mz'><q id='8h0mz'></q></dir></style></legend>

                  <tfoot id='8h0mz'></tfoot>

                    <small id='8h0mz'></small><noframes id='8h0mz'>

                    • <bdo id='8h0mz'></bdo><ul id='8h0mz'></ul>