Laravel Class 未找到“Type”

Laravel Class 'Тype' not found

使用 laravel,我的产品页面只有几个类别。 当我创建一个新产品并单击保存时,出现错误:

Class 'Тype' not found

`protected function newRelatedInstance($class)
    {
        return tap(new $class, function ($instance) {
            if (! $instance->getConnectionName()) {
                $instance->setConnection($this->connection);
            }
        });
    }`

尽管有错误,日期仍保存到数据库中并显示在产品列表页面中。

产品型号

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;

class Product extends Model
{
    use CrudTrait;
    use Sluggable, SluggableScopeHelpers;

    /*
    |--------------------------------------------------------------------------
    | GLOBAL VARIABLES
    |--------------------------------------------------------------------------
    */

    protected $table = 'products';
    // protected $primaryKey = 'id';
    // public $timestamps = false;
    // protected $guarded = ['id'];
    protected $fillable = ['name','description','slug', 'type_id', 'productcat_id', 'material_id', 'enviroment_id', 'manifacture_id'];
    // protected $hidden = [];
    // protected $dates = [];

    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'slug_or_name',
            ],
        ];
    }

    /*
    |--------------------------------------------------------------------------
    | FUNCTIONS
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */

    public function type(){

      return $this->belongsTo('Тype');
    }

    public function productcat(){

      return $this->belongsTo('Productcat');
    }

    public function material(){

      return $this->belongsTo('Material');
    }

    public function environment(){

      return $this->belongsTo('Environment');
    }

    public function manifacture(){

      return $this->belongsTo('Manifacture');
    }

    /*
    |--------------------------------------------------------------------------
    | SCOPES
    |--------------------------------------------------------------------------
    */

    public function scopeFirstLevelItems($query)
    {
        return $query->where('depth', '1')
                    ->orWhere('depth', null)
                    ->orderBy('lft', 'ASC');
    }

    /*
    |--------------------------------------------------------------------------
    | ACCESORS
    |--------------------------------------------------------------------------
    */

    // The slug is created automatically from the "name" field if no slug exists.
    public function getSlugOrNameAttribute()
    {
        if ($this->slug != '') {
            return $this->slug;
        }

        return $this->name;
    }

    /*
    |--------------------------------------------------------------------------
    | MUTATORS
    |--------------------------------------------------------------------------
    */
}

产品控制员

<?php

namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Http\Controllers\CrudController;

// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\ProductRequest as StoreRequest;
use App\Http\Requests\ProductRequest as UpdateRequest;

/**
 * Class ProductCrudController
 * @package App\Http\Controllers\Admin
 * @property-read CrudPanel $crud
 */
class ProductCrudController extends CrudController
{
    public function setup()
    {
        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
        */
        $this->crud->setModel('App\Models\Product');
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/product');
        $this->crud->setEntityNameStrings('product', 'products');

        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
        */

        //$this->crud->setFromDb();

        // ------ CRUD COLUMNS
        $this->crud->addColumn([
                                'name' => 'name',
                                'label' => 'Name',
                            ]);
        $this->crud->addColumn([
                                'name' => 'slug',
                                'label' => 'Slug',
                            ]);



        // ------ CRUD FIELDS
         $this->crud->addField([    // SELECT
                                'label' => 'Тип',
                                'type' => 'select',
                                'name' => 'type_id',
                                'entity' => 'type',
                                'attribute' => 'name',
                                'model' => "App\Models\Type",
                                'wrapperAttributes' => ['class' => 'col-md-4'],
        ]);

         $this->crud->addField([    // SELECT
                                'label' => 'Продукти',
                                'type' => 'select',
                                'name' => 'productcat_id',
                                'entity' => 'productcat',
                                'attribute' => 'name',
                                'model' => "App\Models\Productcat",
                                'wrapperAttributes' => ['class' => 'col-md-4'],
        ]);

         $this->crud->addField([    // SELECT
                                'label' => 'Материал',
                                'type' => 'select',
                                'name' => 'material_id',
                                'entity' => 'material',
                                'attribute' => 'name',
                                'model' => "App\Models\Material",
                                'wrapperAttributes' => ['class' => 'col-md-4'],
        ]);

         $this->crud->addField([    // SELECT
                                'label' => 'Среда',
                                'type' => 'select',
                                'name' => 'environment_id',
                                'entity' => 'envirnment',
                                'attribute' => 'name',
                                'model' => "App\Models\Environment",
                                'wrapperAttributes' => ['class' => 'col-md-4'],
        ]);

         $this->crud->addField([    // SELECT
                                'label' => 'Производител',
                                'type' => 'select',
                                'name' => 'manifacture_id',
                                'entity' => 'manifacture',
                                'attribute' => 'name',
                                'model' => "App\Models\Manifacture",
                                'wrapperAttributes' => ['class' => 'col-md-4'],
        ]);

        $this->crud->addField([
                                'name' => 'name',
                                'label' => 'Заглавие (Име на продукта с кратко описание)',
                                'wrapperAttributes' => ['class' => 'col-md-6'],
                            ]);

        $this->crud->addField([
                                'name' => 'slug',
                                'label' => 'Slug (URL)',
                                'type' => 'text',
                                'hint' => 'Will be automatically generated from your name, if left empty.',
                                'wrapperAttributes' => ['class' => 'col-md-6'],
                            ]);

        $this->crud->addField([    // WYSIWYG
                                'name' => 'description',
                                'label' => 'Описание',
                                'type' => 'textarea',

                            ]);





        // add asterisk for fields that are required in ProductRequest
        $this->crud->setRequiredFields(StoreRequest::class, 'create');
        $this->crud->setRequiredFields(UpdateRequest::class, 'edit');


    }

    public function store(StoreRequest $request)
    {
        // your additional operations before save here
        $redirect_location = parent::storeCrud($request);
        // your additional operations after save here
        // use $this->data['entry'] or $this->crud->entry
        return $redirect_location;
    }

    public function update(UpdateRequest $request)
    {
        // your additional operations before save here
        $redirect_location = parent::updateCrud($request);
        // your additional operations after save here
        // use $this->data['entry'] or $this->crud->entry
        return $redirect_location;
    }
}

类型型号

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class Type extends Model
{
    use CrudTrait;

    /*
    |--------------------------------------------------------------------------
    | GLOBAL VARIABLES
    |--------------------------------------------------------------------------
    */

    protected $table = 'types';
    // protected $primaryKey = 'id';
    // public $timestamps = false;
    // protected $guarded = ['id'];
    protected $fillable = ['name'];
    // protected $hidden = [];
    // protected $dates = [];

    /*
    |--------------------------------------------------------------------------
    | FUNCTIONS
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */

    public function product() {

        return $this->hasMany('Product');

      }

使用背包3.4Laravel 5.6mysql.5.6

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
class Type extends Model
{
  use CrudTrait;

/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/

protected $table = 'types';
// protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
protected $fillable = ['name'];
// protected $hidden = [];
// protected $dates = [];

/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/

public function product() {

    return $this->hasMany('Product');

  }
}

我认为您在导致问题的 class 类型中缺少一个右括号。请检查您是否在 class 正确使用关闭 }

没有名为 Type 的 class。您指的是这些关系中的模型名称。当引用 classes 作为字符串时,它们是完全限定的名称。

Type 是根命名空间中 class Type 的完全限定名称。在名为 Type.

的根命名空间中没有 class

不过我打赌你有 App\Models\Type

这是使用 class 常量而不是自己使用字符串文字的另一个原因,Class::class