如何从背包中的select中保存数据?

时间:2019-02-13 19:43:07

标签: backpack-for-laravel

我购买了背包的商业许可证。首先,我正在当地发展。我需要有关选择字段的帮助。我有两个表Company和Products。我为Company创建了crud,仅提供了ID和名称。但是在Crud for Products中有两个字段:选择字段Company和name。 我可以从选择字段中选择公司,但不要保存在数据库中,错误:公司字段为必填项。我尝试删除验证中所需的内容,但Sql中有错误:不是默认值。

可以说我吗?在哪里可以找到文档或解决方案?

谢谢。

Models \ Ofertas

    class Oferta extends Model
{
    use CrudTrait;

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

    protected $table = 'ofertas';
    // protected $primaryKey = 'id';
    public $timestamps = true;
    // protected $guarded = ['id'];
    protected $fillable = ['company', 'producto'];
    // protected $hidden = [];
    // protected $dates = [];
/*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */
    /**
     * Get the Company for the Ofertas.
     */
    public function company(){
        return $this->belongsTo('App\Models\Company');
    }

OfertaCrudController

// Fields
        $this->crud->addField([  // Select
            'label' => "Compañia",
            'type' => 'select',
            'name' => 'company_id', // the db column for the foreign key
            'entity' => 'company', // the method that defines the relationship in your Model
            'attribute' => 'name', // foreign key attribute that is shown to user
            'model' => "App\Models\Company",

            // optional
            'options'   => (function ($query) {
                 return $query->get();
             }), // force the related options to be a custom query, instead of all(); you can use this to filter the results show in the select
         ]);

        $this->crud->addField(['name' => 'producto', 'type' => 'text', 'label' => 'Producto']);

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

模型公司

class Company extends Model
{
    use CrudTrait;

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

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

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

    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */
    /**
     * Get the ofertas for the Company.
     */
    public function ofertas()
    {
        return $this->hasOne('App\Models\Oferta');
    }

/ *

CompanyCrudController | --------------------------------------------- -----------------------------         | CrudPanel配置         | ------------------------------------------------- -------------------------         * /

    // TODO: remove setFromDb() and manually define Fields and Columns
    //$this->crud->setFromDb();

    // Columns
    $this->crud->addColumn(['name' => 'name', 'type' => 'text', 'label' => 'Nombre']);

    // Fields
    $this->crud->addField(['name' => 'name', 'type' => 'text', 'label' => 'Nombre']);

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

0 个答案:

没有答案