【问题标题】:octobercms api Image pathoctobercms api 图片路径
【发布时间】:2018-03-31 11:36:38
【问题描述】:

我想从我的插件创建 API,但 API 返回所有我需要的图像路径。你知道任何人如何获取图像路径并在我的 API 中返回吗?

在我的 routes.php 中有

<?php
use Mycompany\ArubaPlaces\Models\Places;

Route::get('api/places', function(){
$places = Places::All();
return $places;
});

这是我的模型我有这个关系。我的 API 中需要“placeimages”路径:

public $attachMany = [
        'placesimages' => 'System\Models\File'
];

fields.yaml

fields:
name:
    label: Title
    span: auto
    required: 1
    type: text
slug:
    label: Slug
    span: auto
    preset:
        field: name
        type: slug
    type: text
description:
    label: Description
    size: small
    oc.commentPosition: ''
    span: full
    type: richeditor
latitude:
    label: Latitude
    span: auto
    required: 1
    type: text
longitude:
    label: Longitude
    span: auto
    required: 1
    type: text
placesimages:
    label: 'Place images'
    mode: image
    useCaption: true
    thumbOptions:
        mode: crop
        extension: auto
    span: full
    type: fileupload

active:
    label: Aktívne
    span: auto
    type: switch

【问题讨论】:

  • 你得到什么回应?你也可以添加响应对象吗?
  • { "0": { "id": 29, "latitude": "12.574850", "longitude": "-70.043459", "name": "Arubiana Office", "description": "

    虽然...

    ", "category": "category1", "slug": "arubiana", "sort_order": 1, "routes_id": null, "route_id": null, "活动“:1,}}

标签: laravel rest api octobercms


【解决方案1】:

1) 第一个选项是将protected $with = ['placesimages']; 添加到您的Places 模型以预先加载所需的关系,这里您可以默认添加多个关系。

2) 如果您不想在默认情况下预先加载所有文件,您可以在查询中进行预先加载,例如 Places::with('placesimages')-&gt;get();。同样在这里with 接受一系列关系。

如果您确定每次在您的应用中查询要获取图像的地点,请选择选项一,否则,选项 2 仅在您在查询构建器中指定时才会获取图像。

阅读更多关于eager loading

【讨论】:

    猜你喜欢
    • 2017-02-11
    • 2016-11-24
    • 2017-09-17
    • 2017-01-01
    • 2012-06-11
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多