5

在查看索引视图时,我想为计算字段返回不同的结果,而不是查看资源的详细视图时。

基本上类似于下面的 viewIs() :

Text::make('Preview', function () {
    if($this->viewIs('index'){
        return \small_preview($this->image);
    }
    return \large_preview($this->image);
 })->asHtml(),
4

6 回答 6

6

您可以检查请求的类:

Text::make('Preview', function () use ($request) {
    if ($request instanceof \Laravel\Nova\Http\Requests\ResourceDetailRequest) {
        return \large_preview($this->image);
    }

    return \small_preview($this->image);
});

否则,您可以创建自己的 viewIs 函数:

// app/Nova/Resource.php

/**
 * Check the current view.
 *
 * @param  string  $view
 * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
 * @retrun bool
 */
public function viewIs($view, $request)
{
    $class = '\Laravel\Nova\Http\Requests\\Resource'.ucfirst($view).'Request';

    return $request instanceof $class;
}

然后你可以这样做:

Text::make('Preview', function () use ($request) {
    if ($this->viewIs('detail', $request) {
        return \large_preview($this->image);
    }

    return \small_preview($this->image);
});
于 2018-12-10T16:02:26.890 回答
4

不幸的是,@Chin 的回答对我不起作用,因为编辑视图请求类只是一个基本Laravel\Nova\Http\Request类。

我检查这是否是索引视图的解决方法如下:

/**
 * Check if the current view is an index view.
 *
 * @param  \Laravel\Nova\Http\Requests\NovaRequest $request
 * @return bool
 */
public function isIndex($request)
{
    return $request->resourceId === null;
}
于 2019-01-19T23:11:20.880 回答
2

该类NovaRequest将很快能够提供帮助,因为isResourceIndexRequestisResourceDetailRequest已经在掌握。

由于 Nova 存储库是私有的,我会及时通知你,什么时候可以使用。

与此同时,我将回到 Nova Resource 类 ( app/Nova/Resource.php) 上的辅助方法:

namespace App\Nova;

use Laravel\Nova\Http\Requests\ResourceDetailRequest;
use Laravel\Nova\Http\Requests\ResourceIndexRequest;
use Laravel\Nova\Resource as NovaResource;
use Laravel\Nova\Http\Requests\NovaRequest;

abstract class Resource extends NovaResource
{
    // [...]

    /**
     * Determine if this request is a resource index request.
     *
     * @return bool
     */
    public function isResourceIndexRequest($request)
    {
        return $request instanceof ResourceIndexRequest;
    }

    /**
     * Determine if this request is a resource detail request.
     *
     * @return bool
     */
    public function isResourceDetailRequest($request)
    {
        return $request instanceof ResourceDetailRequest;
    }
}

用法:

public function fields(Request $request)
{
    $fields = [
        // [...]
    ];

    if ($this->isResourceDetailRequest($request)) {
        if ($this->isResourceDetailRequest($request)) {
            $fields = array_merge($fields, [
                // [...]
            ]);
        }
    }

    return $fields;
}
于 2020-03-12T09:53:54.277 回答
1

我添加了这个小助手类

namespace App\Helpers;

class CurrentResourceAction {
    public static function isIndex($request)
    {
        return $request instanceof \Laravel\Nova\Http\Requests\ResourceIndexRequest;
    }
    public static function isDetail($request)
    {
        return $request instanceof \Laravel\Nova\Http\Requests\ResourceDetailRequest;
    }
    public static function isCreate($request)
    {
        return $request instanceof \Laravel\Nova\Http\Requests\NovaRequest &&
            $request->editMode === 'create';
    }
    public static function isUpdate($request)
    {
        return $request instanceof \Laravel\Nova\Http\Requests\NovaRequest &&
            $request->editMode === 'update';
    }
}

你可以在任何你需要的地方调用它

于 2020-06-27T16:30:17.023 回答
0

有点晚了,但是嘿!您可以检查NovaRequest属性editingeditMode('create'、'update'、'attach'等)

// Determine if you are creating a model. 
$request->editMode == 'create';

或者正如他们所说,“阅读来源卢克”,看看他们如何确定它。请参阅 Laravel\Nova\Http\Requests\NovaRequest,它包含类似的检查。

namespace Laravel\Nova\Http\Requests;

class NovaRequest extends FormRequest
{
    /**
     * Determine if this request is via a many to many relationship.
     *
     * @return bool
     */
    public function viaManyToMany();

    /**
     * Determine if this request is an attach or create request.
     *
     * @return bool
     */
    public function isCreateOrAttachRequest();

    /**
     * Determine if this request is an update or update-attached request.
     *
     * @return bool
     */
    public function isUpdateOrUpdateAttachedRequest();

    /**
     * Determine if this request is a resource index request.
     *
     * @return bool
     */
    public function isResourceIndexRequest();

    /**
     * Determine if this request is a resource detail request.
     *
     * @return bool
     */
    public function isResourceDetailRequest();

    /**
     * Determine if this request is an action request.
     *
     * @return bool
     */
    public function isActionRequest();
}

如果您可以NovaRequest在 Nova 资源fields()方法中键入提示而不是常规提示,那就太好了,但由于父资源正在扩展,因此不允许这样做。

于 2021-03-13T23:16:33.420 回答
0

您可以为索引和详细信息页面创建两个单独的字段。

// ----- For Index page
Text::make('Preview', function () {
    return \small_preview($this->image);
})
->onlyOnIndex()
->asHtml(),

// ----- For Detail page
Text::make('Preview', function () {
    return \large_preview($this->image);
})
->onlyOnDetail()
->asHtml(),
于 2018-12-10T10:51:28.150 回答