douwen5066 2018-07-15 04:10
浏览 29
已采纳

没找到LaravelClass'Тype'

Using laravel, I have products page with few categories. When I create a new product and click save it is giving me an error:

Class 'Тype' not found

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

Despite the error the date is saved into the datebase and appear into the products list page.

Product Model

<?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
    |--------------------------------------------------------------------------
    */
}

Product Controler

<?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;
    }
}

Type Model

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

      }

Using backpack 3.4 Laravel 5.6 mysql.5.6

展开全部

  • 写回答

2条回答 默认 最新

  • duanhuang2150 2018-07-15 11:47
    关注

    There is no class named Type. You are referring to a model name in those relationships. When referencing classes as strings, they are fully qualified names.

    Type is the fully qualified name for the class Type in the root namespace. There is no class in the root namespace named Type.

    I bet you have a App\Models\Type though.

    This is another reason to use the class constant and not string literals yourself, Class::class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部