Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何覆盖Laravel中的已加载属性?_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 如何覆盖Laravel中的已加载属性?

Php 如何覆盖Laravel中的已加载属性?,php,laravel,laravel-5,Php,Laravel,Laravel 5,如果我有这样的东西: $book = App\Book::with('authorInfo')->first(); $book->authorInfo = 'test'; <?php namespace App; use Illuminate\Database\Eloquent\Model; class Book extends Model { public function author() { return $this->be

如果我有这样的东西:

$book = App\Book::with('authorInfo')->first();
$book->authorInfo = 'test';
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    public function author()
    {
        return $this->belongsTo(Author::class);
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Author extends Model
{
    public function books()
    {
        return $this->hasMany(Book::class);
    }
}
如何将$book->authorInfo设置为新值? 我试着这样设置:

$book = App\Book::with('authorInfo')->first();
$book->authorInfo = 'test';
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    public function author()
    {
        return $this->belongsTo(Author::class);
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Author extends Model
{
    public function books()
    {
        return $this->hasMany(Book::class);
    }
}
但它并没有改变这个值

当我从控制器方法返回$book时,authorInfo属性将转换为snake-case

{
    book: {
        author_info: null
    }
}

您必须指向作者,然后更改此文件的名称

$book->author->author = 'test';
希望这能对你有所帮助


或者您可以尝试直接修改属性author\u info,方法是:$book->author\u info='test'

您必须指向作者,然后更改此文件的名称

$book->author->author = 'test';
希望这能对你有所帮助


或者您可以尝试直接修改属性author\u info,方法是:$book->author\u info='test'

简短版本:您可以使用雄辩模型提供的方法

假设您的App\Book模型如下所示:

$book = App\Book::with('authorInfo')->first();
$book->authorInfo = 'test';
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    public function author()
    {
        return $this->belongsTo(Author::class);
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Author extends Model
{
    public function books()
    {
        return $this->hasMany(Book::class);
    }
}
获取书籍并加载作者关系时,可以使用setRelation方法将此关系替换为作者模型的新实例:


希望这有帮助

简短版本:您可以使用雄辩模型提供的方法

假设您的App\Book模型如下所示:

$book = App\Book::with('authorInfo')->first();
$book->authorInfo = 'test';
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    public function author()
    {
        return $this->belongsTo(Author::class);
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Author extends Model
{
    public function books()
    {
        return $this->hasMany(Book::class);
    }
}
获取书籍并加载作者关系时,可以使用setRelation方法将此关系替换为作者模型的新实例:


希望这有帮助

$book->author是我试图更改的属性。当您使用With语句时,实际上需要执行额外的查询,检索与原始查询相关的额外数据。您的查询意味着您有一本书,希望检索另一个表中的作者我知道它正在用另一个查询加载数据,但在您的示例中,为什么在第一个作者属性上有一个作者属性?如果第一个表是book id,id_author,book,而第二个表是author id,author,您需要首先指明关系author表,然后指明属性author,但如果您的属性名为name,则必须是$book->author->name:我想覆盖所有的$book->author$book->author是我试图更改的属性。当您使用With语句时,您确实需要执行额外的查询,正在检索与原始查询相关的额外数据。您的查询意味着您有一本书,希望检索另一个表中的作者我知道它正在用另一个查询加载数据,但在您的示例中,为什么在第一个作者属性上有一个作者属性?如果第一个表是book id,id_author,book,而第二个表是author id,author,您需要首先指明关系作者表,然后指明属性作者,但如果您的属性名为name,则必须是$book->author->name:我想使用set-relationship覆盖所有$book->authorTried,但它不起作用。执行unset$book->author;然后设置$book->author='test';工作正常,但并不十分优雅。我不想保存新的相关模型,只在找不到该书的authorInfo时为该属性设置一些默认值。好的,您不必将其保存/存储在数据库中,无需调用$newAuthor->save。您甚至可以将任何值传递给setRelation方法:$book->setRelation'authorInfo','test',不必是Author实例。尝试使用set-relation,但无效。执行unset$book->author;然后设置$book->author='test';工作正常,但并不十分优雅。我不想保存新的相关模型,只在找不到该书的authorInfo时为该属性设置一些默认值。好的,您不必将其保存/存储在数据库中,无需调用$newAuthor->save。您甚至可以将任何值传递给setRelation方法:$book->setRelation'authorInfo','test',不必是Author实例。