我正在使用laravel背包来构建一个后台管理,并且我坚持这一点,这可能是一个非常基本的疑问。
我遇到的情况是:
表格事件:
id - name - themeID - 更多字段......
表主题
id - name
所以,一个主题可以有各种各样的事件,一个事件可以有各种主题,我对如何实现这一点有所怀疑,我做了什么:
Theme.php
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableViewCell
let task = toDolist[indexPath.row]
cell.task = task
return cell
}
Event.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
|--------------------------------------------------------------------------
*/
}
EventCrudController.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
|--------------------------------------------------------------------------
*/
}
我需要一个数据透视表吗?我错过了什么?我找不到这方面的文件。
我得到的错误如下:
数组到字符串转换,他试图保存在字段数组上(&#39; 1&#39;,&#39; 2&#39;)并且错误发生在那里。
答案 0 :(得分:0)
正如@tabacitu所解释的,您的关系是错误的。您正确地选择了其中之一,但是每个关系都应像下面这样定义,而不是像您那样在construct方法上定义:
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
],