【问题标题】:Trying to output a filename through a one to many relationship in Laravel尝试通过 Laravel 中的一对多关系输出文件名
【发布时间】:2021-06-09 17:04:00
【问题描述】:

我收到了错误

在布尔值上调用成员函数documents()

当我试图获取卡片上传文档的文件名时。我在CardsCardDocuments 之间建立了关系:

卡片.php:

public function documents()
{
    return $this->hasMany(CardDocuments::class);
}

CardDocuments.php:

public function files()
{
    return $this->belongsTo(Card::class, 'card_id');
}

之后我在视图中使用了这个:

@foreach ($cards as $card)
    {{ $card->documents()->filename }}
@endforeach

还有 card_document 迁移:

Schema::create('card_documents', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedBigInteger('card_id')->unsigned()->index();
    $table->foreign('card_id')->references('id')->on('cards');
    $table->string('filename')->nullable();
    $table->timestamps();
});

不知道为什么这会显示布尔错误,因为文件提交有效并且文件已成功保存到数据库中,因此无法理解为什么我无法访问 documents() 以获取要输出的文件名。

【问题讨论】:

    标签: php laravel file-upload


    【解决方案1】:

    我认为文档是嵌套数组:

            @foreach ($cards as $card)
                @foreach ($card->documents as $document)
                    {{ $document->filename }}
                @endforeach
            @endforeach
    

    【讨论】:

      猜你喜欢
      • 2020-12-30
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 2014-10-14
      • 1970-01-01
      • 2020-04-16
      相关资源
      最近更新 更多