找不到Laravel类“Тype”

时间:2018-07-15 12:10:05

标签: php laravel backpack-for-laravel

使用laravel,我的产品页面中的类别很少。 当我创建新产品并单击保存时,它给我一个错误:

  

未找到“Тype”类

`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.4 Laravel 5.6 mysql.5.6

2 个答案:

答案 0 :(得分:1)

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

Type是根名称空间中类Type的完全限定名称。根命名空间中没有名为Type的类。

我敢打赌,尽管您有App\Models\Type

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

答案 1 :(得分:0)

<?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');

  }
}

我认为您在导致该问题的类型类中缺少一个结束符。请检查您是否正在正确使用课程结束}