国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

重寫後的標(biāo)題為:What causes a "possible infinite loop" when accessing another model attribute in an appended attribute of a model?
P粉786432579
P粉786432579 2023-09-04 17:47:10
0
1
647
<p>我的 <strong>Laravel 9</strong> 應(yīng)用程式有兩個(gè)模型: <code>brand</code> 模型和 <code>product</code> 模型。每<code>product</code> 屬於一個(gè)<code>brand</code>,而一個(gè)<code>brand</code> 又屬於多個(gè)<code>products</code>(1: n 關(guān)係)。 <code>product</code> 模型應(yīng)提供一個(gè)名為 <code>title_medium</code> 的「計(jì)算」(附加)屬性,該屬性根據(jù)請(qǐng)求連接品牌標(biāo)題和產(chǎn)品標(biāo)題。 </p> <p>一旦我嘗試在產(chǎn)品模型的<code>getTitleMediumAttribute()</code> 方法中訪問<code>$this->brand</code> , <code>xdebug</code>/code> , <code>xdebug</code>就會(huì)拋出<code>possibleInfiniteloop</code> 異常並取消執(zhí)行(N次迭代後) 。我認(rèn)為這與關(guān)係和加載序列(渴望加載)有關(guān),但到目前為止我找不到解決方案。 </p> <h2>品牌模型</h2> <p><code>brand</code> 模型有屬性 <code>title</code> 並且有許多屬於 <code>brand</code> 的 <code>products<brand</code> 的 <code>products<brand</code> 的 <code>products<brand</code> 的 <code>products< </p> <pre class="brush:php;toolbar:false;">namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Str; class Brand extends Model { use HasFactory; /*** 該模型的附加屬性*/ protected $appends = [ 'prices' ]; protected $fillable = [ 'title' ]; /***“啟動(dòng)”模型的方法。 * * @回傳無效*/ protected static function booted() { static::creating(function ($brand) { $brand->slug = Str::slug($brand->title, '-', 'de'); }); } /*** 回饋某個(gè)品牌的所有產(chǎn)品 * * @return 有很多*/ public function products(): HasMany { return $this->hasMany(Product::class); } }</pre> <h2>產(chǎn)品型號(hào)</h2> <p>每個(gè)<code>產(chǎn)品</code>都屬於一個(gè)<code>品牌</code>。附加屬性 <code>title_medium</code> 應(yīng)連接品牌標(biāo)題和產(chǎn)品標(biāo)題。</p>
 命名空間 App\Models;

產(chǎn)品類別擴(kuò)展模型
{
    使用 HasFactory、可搜尋、可過濾;

    受保護(hù)的$可填充= [
        '標(biāo)題',
        '品牌 ID',
        '圖像'
    ];

    /*** 該模型的附加屬性*/
    受保護(hù)的 $appends = [
        '標(biāo)題長',
        '最低價(jià)格',
        '最高折扣百分比價(jià)格',
        '最新價(jià)格日期',
        '價(jià)格計(jì)數(shù)'
    ];

    /***“啟動(dòng)”模型的方法。
     *
     * @回傳無效*/
    受保護(hù)的靜態(tài)函數(shù) booted()
    {
        靜態(tài)::建立(函數(shù)($產(chǎn)品){
            $product->slug = Str::slug($product->title_long, '-', 'de');
        });
    }

    /*** 產(chǎn)品屬於一個(gè)品牌*/
    公共功能品牌():BelongsTo
    {
        返回 $this->belongsTo(Brand::class);
    }

    /*** 取得產(chǎn)品和品牌的組合標(biāo)題*/
    公用函數(shù) getTitleMediumAttribute(): 字串
    {
        // 這會(huì)導(dǎo)致“可能的無限循環(huán)異?!痹?xdebug 中
        返回 $this->brand->title 。 ''。 $這個(gè)->標(biāo)題;
    }
}</pre></p>            
P粉786432579
P粉786432579

全部回覆(1)
P粉306523969

嘗試使用 屬性 而不是 getTitleMediumAttribute,像這樣並告訴我是否仍然遇到相同的錯(cuò)誤(使用此方法而不是 `getTitleMediumAttribute):

public function titleMedium(): Attribute
{
    return Attribute::get(
        fn () => "{$this->brand->title} $this->title",
    );
}

屬性\Illuminate\Database\Eloquent\Casts\Attribute

#
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板