调用模型[App \\ Models \\ MageProduct]

时间:2018-11-18 05:47:15

标签: laravel backpack-for-laravel

我对Laravel和Laravel背包很陌生。我昨天安装了演示版本,并试图创建我的第一个对象。

我的工作将由6个从属选择数据库下拉列表和一个字段组成。

我已禁用我的6个字段,只能使用一个选择下拉菜单“ product_type_id”进行测试。

我已经根据文档创建了

数据库表 楷模 控制者 路线 菜单项

当我加载我的CRUD时,我的CRUD已加载,但是出现以下错误:“错误加载页面。请刷新页面”。用Chrome Inspect功能,我可以看到Search POST生成了一个错误:

“消息”:“调用模型[App \ Models \ MageProduct]上未定义的关系[product_type]。”

无论我尝试什么,我总是会收到此错误。我当然会缺少一些东西,因此,在这里我将提供一些帮助。我当然已经阅读了有关StackOverflow上未定义关系的其他类似问题,但我自己无法解决。请在此后找到我的型号和控制器。预先感谢您的支持,干杯,马克

模型图像产品

<?php

namespace App\Models;

use Backpack\CRUD\CrudTrait;
use Backpack\CRUD\ModelTraits\SpatieTranslatable\HasTranslations;
use Illuminate\Database\Eloquent\Model;
use App\models\MageProductType;

class MageProduct extends Model
{
use CrudTrait;
use HasTranslations;

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

protected $table = 'mage_products';
protected $primaryKey = 'id';
public $timestamps = true;
// protected $guarded = ['id'];
protected $fillable = ['product_type_id'];
// protected $hidden = [];
// protected $dates = [];
// public $translatable = [];

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

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

public function ProductTypes()
{
    return $this->hasMany('App\Models\MageProductType', 'product_type_id');
    // return $this->hasMany(MageProductType::class);
}

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

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

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

模型图像产品类型

<?php

namespace App\Models;

use Backpack\CRUD\CrudTrait;
use Illuminate\Database\Eloquent\Model;
use App\models\MageProduct;

class MageProductType extends Model
{

use CrudTrait;

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

protected $table = 'mage_product_types';
protected $primaryKey = 'id';
// protected $guarded = [];
// protected $hidden = ['id'];
protected $fillable = ['product_type'];
public $timestamps = true;

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

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

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

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

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

控制器

<?php

namespace App\Http\Controllers\Admin;

// use App\Http\Requests\TagRequest as StoreRequest;
// VALIDATION: change the requests to match your own file names if you need form validation
// use App\Http\Requests\TagRequest as UpdateRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;

use App\Models\MageProductType;

class MageProductCrudController extends CrudController
{
public function setup()
{
    /*
    |--------------------------------------------------------------------------
    | CrudPanel Basic Information
    |--------------------------------------------------------------------------
    */

    $this->crud->setModel('App\Models\MageProduct');
    $this->crud->setRoute(config('backpack.base.route_prefix').'/produit');
    // $this->crud->setRoute("admin/produitsmage");
    $this->crud->setEntityNameStrings('produit', 'produits');

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

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

    // ------ CRUD COLUMNS

    $this->crud->addColumn([
                            'label' => 'Type',
                            'type' => 'select',
                            'name' => 'product_type_id',
                            'entity' => 'product_type',
                            'model' => 'App\Models\MageProductType',
                            ]);

    // ------ CRUD FIELDS

    $this->crud->addField([    // SELECT
                            'label' => 'Type',
                            'type' => 'select',
                            'name' => 'product_type_id',
                            'entity' => 'product_type',
                            'attribute' => 'product_type',
                            'model' => 'App\Models\MageProductType',
                        ]);


}

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

0 个答案:

没有答案