- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 laravel backpack 来构建后台管理,但我一直坚持这一点,这可能是一个非常基本的疑问。
我的情况是:
表事件:
id - 名称 - themeID - 更多字段...
表格主题:
id - 名字
所以,一个主题可以有不同的事件,一个事件可以有不同的主题,我对如何实现这一点有一些疑问,我做了什么:
Theme.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
class Theme extends Model
{
use CrudTrait;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'themes';
//protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
protected $fillable = ['name'];
// protected $hidden = [];
// protected $dates = [];
/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function Theme() {
return $this->hasMany('App\Models\Event');
}
/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| ACCESORS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
}
Event.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
class Event extends Model
{
use CrudTrait;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'events';
//protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
protected $fillable = ['name','theme','schedules','themeID','countyID','event_typeID','offered_conditionID','alone_with_typeID','tagID',
'advertiserID','event_languageID','special_priceID','start_date','end_date','price','photos','space_name','address','description','facebook_url',
'external_url1','featured_image','available_vacancies','gps_coordinates','external_url2','featured','status','start_featured_date','end_featured_date'];
// protected $hidden = [];
// protected $dates = [];
/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function Event() {
return $this->hasOne('App\Models\EventSubType');
return $this->hasMany('App\Models\SpecialPrice');
return $this->hasMany('App\Models\PriceTypes');
return $this->hasMany('App\Models\Tag');
// return $this->hasMany('App\Models\County');
return $this->hasMany('App\Models\County');
return $this->hasMany('App\Models\EventHasSpecialPrice');
}
public function themes()
{
return $this->belongsToMany('App\Models\Theme');
}
/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| ACCESORS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
}
EventCrudController.php
<?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\EventRequest as StoreRequest;
use App\Http\Requests\EventRequest as UpdateRequest;
class EventCrudController extends CrudController
{
public function setUp()
{
/*
|--------------------------------------------------------------------------
| BASIC CRUD INFORMATION
|--------------------------------------------------------------------------
*/
$this->crud->setModel('App\Models\Event');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/events');
$this->crud->setEntityNameStrings('event', 'events');
/*
|--------------------------------------------------------------------------
| BASIC CRUD INFORMATION
|--------------------------------------------------------------------------
*/
/* $this->crud->addColumn([
'label' => "Theme", // Table column heading
'type' => "select_multiple",
'name' => 'themes', // the column that contains the ID of that connected entity;
'entity' => 'themes', // the method that defines the relationship in your Model
'attribute' => "name", // foreign key attribute that is shown to user
'model' => 'App\Models\Theme', // foreign key model
'pivot' => true,
]); */
$this->crud->setFromDb();
$this->crud->addFields([
// This is the field im trying to get working
[
'label' => 'Theme',
'type' => 'select_multiple',
'name' => 'themeID',
'entity'=> 'Event',
'attribute' => 'name', // Column which user see in select box
'model' => 'App\Models\Theme' // Model which contain FK
],
[
'label' => 'District',
'type' => 'select2',
'name' => 'countyID',
'entity'=> 'district',
'attribute' => 'name', // Column which user see in select box
'model' => 'App\Models\District' // Model which contain FK
],
[
'label' => 'Event Type',
'type' => 'select2',
'name' => 'event_typeID',
'entity'=> 'EventType',
'attribute' => 'name', // Column which user see in select box
'model' => 'App\Models\EventType' // Model which contain FK
],
[
'label' => 'Offered Conditions',
'type' => 'select2',
'name' => 'offered_conditionID',
'entity'=> 'OfferedCondition',
'attribute' => 'name', // Column which user see in select box
'model' => 'App\Models\OfferedCondition' // Model which contain FK
],
[
'label' => 'Alone With Types',
'type' => 'select2',
'name' => 'alone_with_typeID',
'entity'=> 'AloneWithType',
'attribute' => 'name', // Column which user see in select box
'model' => 'App\Models\AloneWithType' // Model which contain FK
],
[
'label' => 'Tags',
'type' => 'select2',
'name' => 'tagID',
'entity'=> 'Tag',
'attribute' => 'name', // Column which user see in select box
'model' => 'App\Models\Tag' // Model which contain FK
],
[
'label' => 'Advertiser',
'type' => 'select2',
'name' => 'advertiserID',
'entity'=> 'Advertiser',
'attribute' => 'name', // Column which user see in select box
'model' => 'App\Models\Advertiser' // Model which contain FK
],
[
'label' => 'Event Language',
'type' => 'select2',
'name' => 'event_languageID',
'entity'=> 'Event_language',
'attribute' => 'name', // Column which user see in select box
'model' => 'App\Models\EventLanguage' // Model which contain FK
],
[
'label' => 'Special Price',
'type' => 'select2',
'name' => 'special_priceID',
'entity'=> 'SpecialPrice',
'attribute' => 'name', // Column which user see in select box
'model' => 'App\Models\SpecialPrice' // Model which contain FK
]
]);
/* $this->crud->addField([
'label' => "Images",
'name' => "photos",
'type' => 'image_multiple',
'upload' => true,
'crop' => true, // set to true to allow cropping, false to disable
'aspect_ratio' => 0, // ommit or set to 0 to allow any aspect ratio
]); */
// $this->crud->addFields($array_of_arrays, 'update/create/both');
// $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->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);
// ------ 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)
{
/*
// Setup storage
$attribute_name = "photos";
$disk = "uploads";
$destination_path = "/uploads/projects";
// Then get images from request
$input = $request->all();
$images = $input[$attribute_name];
$imageArray = [];
// Now iterate images
foreach ($images as $value) {
// Store on disk and add to array
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the path to the database
array_push($imageArray, $destination_path.'/'.$filename);
}
}
// Update $request with new array
$request->request->set($attribute_name, $imageArray);
*/
// Save $request
$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)
{
// Setup storage
$attribute_name = "images";
$disk = "uploads";
$destination_path = "/uploads/projects";
// Then get images from request
$input = $request->all();
$images = $input[$attribute_name];
$imageArray = [];
// Now iterate images
foreach ($images as $value) {
// Store on disk and add to array
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the path to the database
array_push($imageArray, $destination_path.'/'.$filename);
} else {
array_push($imageArray, $value);
}
}
// Update $request with new array
$request->request->set($attribute_name, $imageArray);
// 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;
}
/* 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;
} */
}
我需要数据透视表吗?我错过了什么?我找不到这方面的文档。
我收到的错误如下:
数组到字符串的转换,他试图保存在字段array('1','2')上,错误发生在那里。
最佳答案
正如@tabacitu 所解释的,您的关系是错误的。你做对了其中一个,但每个关系都应该像下面这样定义,而不是像你那样在构造方法上定义:
public function themes()
{
return $this->belongsToMany('App\Models\Theme');
}
阅读有关 Eloquent Relationship 的 Laravel 文档,您可以找到所需的所有信息。 .
一旦你有了这个设置,你就可以让它工作了
$this->crud->addFields([
// This is the field im trying to get working
[
'label' => 'Theme',
'type' => 'select_multiple',
'name' => 'themeID',
'entity'=> 'themes', // <-- this is the relation method name
'attribute' => 'name', // Column which user see in select box
'model' => 'App\Models\Theme' // Model which contain FK
],
关于php - Laravel背包关系疑惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44704525/
我已经用 Scala 中的每一项编写了有界背包问题的答案,并尝试将其转换为 Haskell,结果如下: knapsack :: [ ( Int, Int ) ] -> [ ( Int, Int ) ]
我正在解决这个动态编程问题,并且我一直坚持用 Java 编写迭代解决方案 目标是找到花费 X 美分所需的最少卡路里数。如果我们不能恰好花费 X 美分,那么就没有解决方案。我们给定了 N 个元素,每个元
根据维基百科和我浏览过的其他资源,您需要矩阵m[n][W]; n - 元素数量和 W - 背包的总容量。这个矩阵变得非常大,有时大到无法在 C 程序中处理。我知道动态规划的基础是节省内存时间,但是,有
我发现此代码使用蛮力机制解决背包问题(这主要是为了学习,因此无需指出动态更有效)。我得到了可以工作的代码,并且了解了大部分代码。最多。这是问题: 我注意到这两个条件,我不知道它们如何工作以及为什么在代
我必须实现背包问题的以下变体。背包中的每件元素都有优先级和重量。现在我指定一个权重 X。我必须知道计算权重总和至少为 X 并且具有最低优先级的最小项目集。每个项目只能选择一次。示例: Knap
所以我想知道如何计算背包问题的所有解。也就是说,我有兴趣从一组最大大小为 K 的数字中找出可能的子集数。 例如,我们有一组大小为 {3, 2, 5, 6, 7} 的项目,最大大小为 K = 13。因此
我在执行任务时遇到了问题。我有一个数据库,其中包含所有具有“价格”值的项目。它们连接到不同的“轮次”,“轮次”有一个“总值(value)”,其中这些项目“价格”-值(value)全部放在一起定义了“总
我遇到的问题如下: 给定一个包含 N 个元素的队列,每个元素都有一个重量,以及一个包含 K 个容器的队列。我们需要按照元素来的顺序将元素分成容器。例如,第一个项目只能进入第一个容器,第二个可以进入第一
问题如下: You have n trip lengths in km which should be divided among m number of days such that the max
我昨晚在开发一个应用程序时遇到了一个特定的问题,我确信它可能有一个有效的算法来解决它。谁能推荐一下? 问题: TL;DR:也许图片会有所帮助:http://www.custom-foam-insert
如何获取下拉列表,其中占位符显示“选择类别”作为默认选择? 以下代码没有渲染占位符 $this->crud->addField([ // Select2 'label'
刚刚带背包的新品。我在官方网站上搜索并用谷歌搜索,但没有找到答案 在 laravel 7 中,使用 Backpack 4.1 我的数据模型是:客户有很多地址 关系在客户模型中配置: public fu
据我所知(如果我错了请纠正我),背包只处理 $fillable 字段。整个 laravel 的事情不就是 $fillable 和 $guarded 之间的分离吗? MWE: 在 User.php 中:
看到this之后讲座我创建了以下背包代码。在讲座中,教授说从最优值(19:00 分钟)确定集合很容易,但我找不到如何去做。我在代码中提供了一个将值相加为 21 的示例,我如何根据该值确定集合(在本例中
为什么贪心法适用于连续背包问题而不适用于 0-1 背包问题? 最佳答案 对于连续背包,在最佳解决方案中,您不能有 q > 0 的每单位成本为 c 的元素,同时留下 q' > 0 成本为 c' > c
我在理解动态规划时遇到了一些困难,尽管我已经通读了很多资源试图理解。 我理解使用斐波那契算法给出的动态规划示例。我明白如果你使用分而治之的方法,你最终会多次解决一些子问题,而动态编程通过解决这些重叠的
假设一个经典的 0-1 背包问题,但您可以上溢/下溢背包并受到一些惩罚。每单位溢出(重量超过最大容量)扣除X利润,每单位下溢(重量低于最大容量)扣除Y利润。 我想按利润与重量的比率对所有元素进行排序,
我正在编写具有多个约束的背包 0-1 的变体。除了重量限制外,我还有数量限制,但在这种情况下,我想解决背包问题,因为我的背包中需要恰好有 n 件元素,重量小于或等于 W。我我目前正在为基于 Roset
给定一个无限正整数数组或一个正整数流,找出总和为 20 的前五个数。 通过阅读问题陈述,它首先似乎是 0-1 Knapsack 问题,但我很困惑 0-1 Knapsack algo 可以在流上使用的整
我想解决一个3维背包问题。 我有许多不同宽度、高度、长度和值(value)的盒子。我有一个指定的空间,我想把盒子放在那个空间里,这样我就能获得最优的利润。我想使用暴力来做到这一点。 我正在用 Java
我是一名优秀的程序员,十分优秀!