【问题标题】:October cms file attachment with only path?十月 cms 文件附件只有路径?
【发布时间】:2019-08-19 05:29:14
【问题描述】:

我是 10 月 CMS 的新手。现在我有一个文件附件的问题。在模型中:

public $attachOne = [
        'cover_image' => ['System\Models\File']
    ];

现在在控制器中:

$comic = Comic::with('cover_image)->find($id);

返回:

{
"id": 24,
"title": "Comic example detail",
"description": "Comic example detail",
"total_page": "14",
"cover_image": {
"id": 165,
"disk_name": "5d5a21ca68bf9280597931.png",
"file_name": "img06.png",
"file_size": 142958,
"content_type": "image/png",
"title": null,
"description": null,
"field": "cover_image",
"sort_order": 165,
"created_at": "2019-08-19 04:12:58",
"updated_at": "2019-08-19 04:15:23",
"path": "http://localhost:10080/storage/app/uploads/public/5d5/a21/ca6/5d5a21ca68bf9280597931.png",
"extension": "png"
}
} 

我希望cover_image 只返回一些字段,如下所示:

"cover_image": {
"id": 165,
"file_name": "img06.png",
"path": "http://localhost:10080/storage/app/uploads/public/5d5/a21/ca6/5d5a21ca68bf9280597931.png",
}

我该怎么做?

【问题讨论】:

  • 你检查我的答案了吗?

标签: laravel octobercms octobercms-backend


【解决方案1】:

使用约束预加载

见文档https://octobercms.com/docs/database/relations#constraining-eager-loads

where 语句解释,但你可以使用select 语句,你只能得到必需的attributes

我相信它会帮助你。

【讨论】:

    猜你喜欢
    • 2017-06-14
    • 2017-09-27
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多