【问题标题】:Object of Class App\PageStatus (DateTime?) could not be converted to int类 App\PageStatus (DateTime?) 的对象无法转换为 int
【发布时间】:2020-09-22 13:17:34
【问题描述】:

我正在使用nova-dependency-container,但收到以下错误Object of Class App\PageStatus could not be converted to int。在 Laravel Nova 中创建新页面/资源或尝试在 Laravel Nova 中查看页面/资源时会出现此错误。

但是,仍然会创建页面/资源...但是在尝试查看页面/资源时,我只是收到此错误并看到一个空白页面...

这是我的app/Nova/Page.php 模型

<?php

namespace App\Nova;

use Epartment\NovaDependencyContainer\HasDependencies;
use Epartment\NovaDependencyContainer\NovaDependencyContainer;
use Froala\NovaFroalaField\Froala;
use Gwd\SeoMeta\SeoMeta;
use Illuminate\Http\Request;
use Inspheric\Fields\Indicator;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\DateTime;
use App\PageStatus;

class Page extends Resource
{
    use HasDependencies;

    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Page::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'title';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'title',
        'slug'
    ];

    /**
     * The logical group associated with the resource.
     *
     * @var string
     */
    public static $group = 'Pages & Posts';

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make('ID', 'id')->asBigInt(),

            Text::make('Title')
                ->rules('required', 'max:255'),

            Text::make('Slug', 'slug')
                ->hideFromIndex()
                ->creationRules('unique:pages,slug')
                ->rules('required', 'alpha_dash', 'max:80'),

            Froala::make('Content')
                ->hideFromIndex()
                ->rules('required'),

            Indicator::make('Status', function() {
                return $this->pageStatus->status;
            })
                ->labels([
                    'publish' => 'Publish',
                    'future' => 'Future',
                    'draft' => 'Draft',
                    'pending' => 'Pending',
                    'private' => 'Privat'
                ])
                ->colors([
                    'publish' => 'green',
                    'future' => 'purple',
                    'draft' => 'blue',
                    'pending' => 'orange',
                    'private' => 'red'
                ]),

            BelongsTo::make('Status', 'pageStatus', 'App\Nova\PageStatus')
                ->onlyOnForms(),

            NovaDependencyContainer::make([
                DateTime::make('When to Publish', 'publish_at')
                    ->format('DD.MM.YYYY @ HH:MM:SS')
                    ->rules('required', 'date_format:Y-m-d H:i:s')
            ])->dependsOn('pageStatus', PageStatus::getIdByStatus('future')),

            BelongsTo::make('Author', 'user', 'App\Nova\User'),

            SeoMeta::make('SEO', 'seo_meta'),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}

app/Nova/PageStatus.php模特:

<?php

namespace App\Nova;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

class PageStatus extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\PageStatus::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'label';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'status', 'label'
    ];

    /**
     * Default ordering for index query.
     *
     * @var array
     */
    public static $sort = [
        'id' => 'asc'
    ];

    /**
     * The logical group associated with the resource.
     *
     * @var string
     */
    public static $group = 'Pages & Posts';

    /**
     * Build an "index" query for the given resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public static function indexQuery(NovaRequest $request, $query)
    {
        if (empty($request->get('orderBy'))) {
            $query->getQuery()->orders = [];

            return $query->orderBy(key(static::$sort), reset(static::$sort));
        }

        return $query;
    }

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Text::make('Status', 'status')
                ->required(),

            Text::make('Label', 'label')
                ->required(),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}

App/Page.php模特:

<?php

namespace App;

use Gwd\SeoMeta\Traits\SeoSitemapTrait;
use Illuminate\Database\Eloquent\Model;
use Gwd\SeoMeta\Traits\SeoMetaTrait;

class Page extends Model
{
    use SeoMetaTrait, SeoSitemapTrait;

    /*
     * In order to have set the auth user as default user when creating a page
     */
    public function __construct(array $attributes = [])
    {
        if (! isset($attributes['user_id']) && auth()->check()) {
            $attributes['user_id'] = auth()->user()->id;
        }

        parent::__construct($attributes);
    }

    /**
     * @Protected_variables
     */

    protected $table = 'pages';

    protected $guarded = ['id'];

    protected $casts = [
        'publish_at' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    /**
     * @Public_variables
     */

    /**
     * @Relationships
     */

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function pageStatus()
    {
        return $this->belongsTo('App\PageStatus');
    }

    /**
     * @Attributes
     */

    /**
     * @Custom_functions
     */

    /**
     * Get the Page url by item
     *
     * @return string
     */
    public function getSitemapItemUrl(): String
    {
        return route('page.show', $this->slug);
    }

    /**
     * Query all the Page items which should be
     * part of the sitemap (crawlable for google).
     *
     * @return Page[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getSitemapItems()
    {
        return static::all();
    }
}

App/PageStatus.php模特:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PageStatus extends Model
{
    /**
     * @Protected_variables
     */

    protected $table = 'page_statuses';

    protected $guarded = ['id'];

    /**
     * @Public_variables
     */

    /**
     * @Relationships
     */

    public function products()
    {
        return $this->hasMany('App\Product');
    }

    /**
     * @Attributes
     */

    /**
     * @Custom_functions
     */

    public static function getPageStatusesFilterArray()
    {
        $page_statuses = PageStatus::all('id', 'label');

        $array = array();

        foreach($page_statuses as $page_status){
            $array[$page_status->label] = $page_status->id;
        }

        return $array;
    }

    public static function getIdByStatus($status)
    {
        return self::where('status', $status)->first()->id;
    }
}

更新 pageStatus迁移文件:

class CreatePageStatusesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('page_statuses', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('status')->index()->unique();
            $table->string('label');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('page_statuses');
    }
}

【问题讨论】:

  • 检查迁移文件,可能有问题。检查mysql中列的类型。
  • 我已经检查了一切,一切都是正确的。我也会添加迁移文件
  • 只是猜测 - 它是函数 getIdByStatus($status),从返回 return self::where('status', $status)-&gt;first(); 的末尾删除 id 并调整您的资源。
  • 我发现了问题。我会回答我的问题。如果您有兴趣,请阅读。

标签: php laravel laravel-nova


【解决方案1】:

我发现了问题。我必须将-&gt;dependsOn('pageStatus', PageStatus::getIdByStatus('future')) 更改为-&gt;dependsOn('pageStatus.id', PageStatus::getIdByStatus('future'))

.id 缺少指定 postStatus 的列。

【讨论】:

    猜你喜欢
    • 2022-08-16
    • 2019-05-02
    • 1970-01-01
    • 2017-03-08
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    相关资源
    最近更新 更多