<tfoot id='EjEUM'></tfoot>

    1. <small id='EjEUM'></small><noframes id='EjEUM'>

      <legend id='EjEUM'><style id='EjEUM'><dir id='EjEUM'><q id='EjEUM'></q></dir></style></legend>
        <bdo id='EjEUM'></bdo><ul id='EjEUM'></ul>
    2. <i id='EjEUM'><tr id='EjEUM'><dt id='EjEUM'><q id='EjEUM'><span id='EjEUM'><b id='EjEUM'><form id='EjEUM'><ins id='EjEUM'></ins><ul id='EjEUM'></ul><sub id='EjEUM'></sub></form><legend id='EjEUM'></legend><bdo id='EjEUM'><pre id='EjEUM'><center id='EjEUM'></center></pre></bdo></b><th id='EjEUM'></th></span></q></dt></tr></i><div id='EjEUM'><tfoot id='EjEUM'></tfoot><dl id='EjEUM'><fieldset id='EjEUM'></fieldset></dl></div>
    3. 手动将项目添加到现有对象 [Laravel 5]

      时间:2023-09-24
        <tbody id='Dfesc'></tbody>

          <legend id='Dfesc'><style id='Dfesc'><dir id='Dfesc'><q id='Dfesc'></q></dir></style></legend>

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

                本文介绍了手动将项目添加到现有对象 [Laravel 5]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                这是我尝试做的:

                $q = Question::where('id',$id -> id)->get();
                $q[] = $q->push([ 'test' => true]); 
                dd($q);
                

                这将输出:

                Collection {#220 ▼
                  #items: array:3 [▼
                    0 => Question {#225 ▶}
                    1 => array:1 [▼
                      "test" => true
                    ]
                    2 => null
                  ]
                }
                

                所以 'test' =>true 将作为新键追加,但我想将它插入 Question 中,以便以后我可以像这样使用 foreach $q ->测试

                So 'test' => true will append as a new key, but I want to insert it in Question so latter I can access to it like this with foreach $q -> test

                所以这是我想要访问项目的方式:

                So here is how I want access to item:

                @foreach($q as $qq)
                {{ $qq->test }}
                @endforeach
                

                推荐答案

                可以通过使用 Eloquent Model (https://github.com/illuminate/database/blob/master/Eloquent/Model.php).
                如您所见,它使用 setAttribute() 将数据存储在 protected $attributes 中,当我们执行 $SomeModel->some_field 时,它使用魔术方法__get()attributes 数组中通过关联检索项目.

                这是您问题的解决方案:

                It can be done by using setAttribute() function of Eloquent Model (https://github.com/illuminate/database/blob/master/Eloquent/Model.php).
                As You can see it stores data in protected $attributes using setAttribute(), and when we do $SomeModel->some_field it uses magic method __get() to retrieve item by association from attributes array.

                Here is the resolution to Your question:

                $Question = Question::find($id);
                $Question->setAttribute('test', 'blablabla');
                

                这篇关于手动将项目添加到现有对象 [Laravel 5]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何从连接表中选择列:laravel eloquent 下一篇:两个外键,如何用laravel eloquent映射

                相关文章

                最新文章

                1. <small id='hWw0w'></small><noframes id='hWw0w'>

                  <tfoot id='hWw0w'></tfoot>
                    <bdo id='hWw0w'></bdo><ul id='hWw0w'></ul>

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