Php 类App\PageStatus(DateTime?)的对象无法转换为int

Php 类App\PageStatus(DateTime?)的对象无法转换为int,php,laravel,laravel-nova,Php,Laravel,Laravel Nova,我正在使用,但遇到以下错误:类App\PageStatus的对象无法转换为int。在Laravel Nova中创建新页面/资源或尝试在Laravel Nova中查看页面/资源时,会出现此错误 但是,仍然会创建页面/资源。。。但是当我试图查看页面/资源时,我只是得到了这个错误,并看到一个空白页面 这是我的app/Nova/Page.php模型 <?php namespace App\Nova; use Epartment\NovaDependencyContainer\HasDepend

我正在使用,但遇到以下错误:类App\PageStatus的对象
无法转换为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/Page.php
model:

<?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 [];
    }
}
<?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();
    }
}
<?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');
    }
}

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


缺少指定postStatus列的
.id

请检查迁移文件,可能有问题。检查mysql中列中的类型。我已经检查了所有内容,所有内容都是正确的。我将把迁移文件添加到just gusting中——它是函数getIdByStatus($status),从return的末尾删除id
return self::where('status',$status)->first()并调整您的资源。我发现了问题。我会回答我的问题。如果你感兴趣,请仔细阅读。