1

我正在创建一个在多个商店(即亚马逊、eBay 等)具有产品及其多个价格的表单,以比较价格并向消费者提供最低价格。

我在构建这种类型的表单时遇到了问题。我无法使用背包的表字段将数据存储在数据库中,所以我需要帮助。

CRUD 控制器:

<?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\MenRequest as StoreRequest;
use App\Http\Requests\MenRequest as UpdateRequest;
class MenCrudController extends CrudController
{
    public function setup()
    {
        $this->crud->setModel('App\Models\Men');
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/men');
        $this->crud->setEntityNameStrings('men', 'mens');
        //$this->crud->setFromDb();
        // ------ CRUD FIELDS
        // $this->crud->addField($options, 'update/create/both');
        // $this->crud->addFields($array_of_arrays, 'update/create/both');
        $this->crud->addFields([
            [ // Table
              'name' => 'mens',
              'label' => 'Options',
              'type' => 'table',
              'entity_singular' => 'option', // used on the "Add X" button
              'columns' => [
                  'name' => 'Name',
                  'price'=>'Price',
              ],
              'max' => 5, // maximum rows allowed in the table
               'min' => 0 // minimum rows allowed in the table
            ],
        ]);
        // $this->crud->removeField('name', 'update/create/both');
        // $this->crud->removeFields($array_of_names, 'update/create/both');
        // ------ CRUD COLUMNS
        // $this->crud->addColumn(); // add a single column, at the end of the stack
        // $this->crud->addColumns(); // add multiple columns, at the end of the stack
        $this->crud->addColumns(
            [
                [
                    'label'  =>'Product Name',
                    'name'   =>'name',
                ],
            ]);
        // $this->crud->removeColumn('column_name'); // remove a column from the stack
        // $this->crud->removeColumns(['column_name_1', 'column_name_2']); // remove an array of columns from the stack
        // $this->crud->setColumnDetails('column_name', ['attribute' => 'value']); // adjusts the properties of the passed in column (by name)
        // $this->crud->setColumnsDetails(['column_1', 'column_2'], ['attribute' => 'value']);
        // ------ CRUD BUTTONS
        // possible positions: 'beginning' and 'end'; defaults to 'beginning' for the 'line' stack, 'end' for the others;
        // $this->crud->addButton($stack, $name, $type, $content, $position); // add a button; possible types are: view, model_function
        // $this->crud->addButtonFromModelFunction($stack, $name, $model_function_name, $position); // add a button whose HTML is returned by a method in the CRUD model
        // $this->crud->addButtonFromView($stack, $name, $view, $position); // add a button whose HTML is in a view placed at resources\views\vendor\backpack\crud\buttons
        // $this->crud->removeButton($name);
        // $this->crud->removeButtonFromStack($name, $stack);
        // $this->crud->removeAllButtons();
        // $this->crud->removeAllButtonsFromStack('line');

        // ------ CRUD ACCESS
        // $this->crud->allowAccess(['list', 'create', 'update', 'reorder', 'delete']);
        // $this->crud->denyAccess(['list', 'create', 'update', 'reorder', 'delete']);
        // ------ CRUD REORDER
        // $this->crud->enableReorder('label_name', MAX_TREE_LEVEL);
        // NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('reorder');

        // ------ CRUD DETAILS ROW
        // $this->crud->enableDetailsRow();
        // NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('details_row');
        // NOTE: you also need to do overwrite the showDetailsRow($id) method in your EntityCrudController to show whatever you'd like in the details row OR overwrite the views/backpack/crud/details_row.blade.php
        // ------ REVISIONS
        // You also need to use \Venturecraft\Revisionable\RevisionableTrait;
        // Please check out: https://laravel-backpack.readme.io/docs/crud#revisions
        // $this->crud->allowAccess('revisions');
        // ------ AJAX TABLE VIEW
        // Please note the drawbacks of this though:
        // - 1-n and n-n columns are not searchable
        // - date and datetime columns won't be sortable anymore
        // $this->crud->enableAjaxTable();
        // ------ DATATABLE EXPORT BUTTONS
        // Show export to PDF, CSV, XLS and Print buttons on the table view.
        // Does not work well with AJAX datatables.
        // $this->crud->enableExportButtons();
        // ------ ADVANCED QUERIES
        // $this->crud->addClause('active');
        // $this->crud->addClause('type', 'car');
        // $this->crud->addClause('where', 'name', '==', 'car');
        // $this->crud->addClause('whereName', 'car');
        // $this->crud->addClause('whereHas', 'posts', function($query) {
        //     $query->activePosts();
        // });
        // $this->crud->addClause('withoutGlobalScopes');
        // $this->crud->addClause('withoutGlobalScope', VisibleScope::class);
        // $this->crud->with(); // eager load relationships
        // $this->crud->orderBy();
        // $this->crud->groupBy();
        // $this->crud->limit();
    }
    public function store(StoreRequest $request)
    {
        // your additional operations before save here
        $redirect_location = parent::storeCrud();
        // 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();
        // 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 Men extends Model
{
    use CrudTrait;
      protected $table = 'mens';
      protected $primaryKey = 'id';
       public $timestamps = false;
       //protected $guarded = ['id'];
       protected $fillable = ['category','name','price','shipping','mrp','product_id'];
    // protected $hidden = [];
    // protected $dates = [];
       protected function castAttribute($key, $value)
  {
    switch ($this->getCastType($key))
    {
      case 'int':
      case 'integer':
        return (int) $value;
      case 'real':
      case 'float':
      case 'double':
        return (float) $value;
      case 'string':
        return (string) $value;
      case 'bool':
      case 'boolean':
        return (bool) $value;
      case 'object':
        return json_decode($value);
      case 'array':
      case 'json':
        return json_decode($value, true);
      default:
        return $value;
    }
  }

}

4

0 回答 0