Laravel 列表与下拉列表的关系
Laravel lists with relationship for a dropdown
我有两个 table:
课程
- id
- 姓名
- season_id
- teacher_id
- dates_id
- 描述
...
course_dates
- id
- course_id
- 工作日
- 开始
- 停止
...
前 table 是课程的基本内容。在第二个 table 中,我只存储课程的日期(即工作日 1 为星期一,开始 -> 开始时间和停止时间 -> 结束时间)。课程可以有多个日期。
现在我想为下拉列表创建一个列表,如下所示:
- 课程名称(日期,从-到)
- 相同的课程名称(另一天,从-到)
- 另一个课程名称(天,从-到)
我克服了一点辅助功能的那一天的名字。
如何使用访问器操作列表方法的参数?
感谢帮助!
课程模型
<?php
class Course extends \Eloquent {
protected $guarded = ['id', 'created_at', 'updated_at'];
//Relationship Holidays
public function holidays()
{
return $this->hasMany('Holiday');
}
//Relationship Teacher
public function teacher()
{
return $this->belongsTo('Teacher');
}
//Relationship User
public function bookings()
{
return $this->hasMany('Booking');
}
//Relationship Dates
public function dates()
{
return $this->hasMany('Dates');
}
//Accessor for Dates
public function getWeekdayAttribute()
{
//???
}
//Validation Rules
public static $rules = [
'name' => 'required',
'teacher_id' => 'required',
'description' => 'required',
'room' => 'required',
'price' => array('required', 'regex:/^\d*(\,\d{2})?$/'),
'maxuser' => 'required|numeric',
'target' => 'required'
];
//Custom Attribute Names
public static $names = [
'name' => 'Kursname',
'teacher_id' => 'Kursleiter',
'description' => 'Beschreibung',
'room' => 'Raum/Ort',
'price' => 'Preis',
'maxuser' => 'Max. Anzahl Teilnehmer',
'target' => 'Zielgruppe'
];
//Change Price Format Get
public function getPriceAttribute($price)
{
return number_format($price, 2, ',', '');
}
//Change Price Format Set
public function setPriceAttribute($price)
{
$this->attributes['price'] = str_replace(',', '.', $price);
}
//Unserialize Target Get
public function getTargetAttribute($target)
{
return unserialize($target);
}
//Serialize Target Set
public function setTargetAttribute($target)
{
$this->attributes['target'] = serialize($target);
}
}
日期模型
<?php
class Dates extends \Eloquent {
protected $guarded = ['id', 'created_at', 'updated_at'];
protected $table = 'course_dates';
public function courses()
{
return $this->belongsTo('Course')->orderBy('name');
}
//Validation Rules
public static $rules = [
'weekday' => 'required',
'start' => 'required|date_format:H:i',
'stop' => 'required|date_format:H:i'
];
//Custom Attribute Names
public static $names = [
'weekday' => 'Wochentag',
'start' => 'Von',
'stop' => 'Bis'
];
}
辅助函数
//Display Weekday
function showWeekday($id)
{
$weekday = array(
'1' => 'Montag',
'2' => 'Dienstag',
'3' => 'Mittwoch',
'4' => 'Donnerstag',
'5' => 'Freitag',
'6' => 'Samstag',
'0' => 'Sonntag'
);
return $weekday[$id];
}
如果我理解你的意思是这样的:
CourseDate 模型
public function getNameAndTimeAttribute(){
return $this->course->name . ' ' . $this->attributes['start'] . ' - ' . $this->attributes['stop'];
}
然后:
$dropdown = CourseDate::with('course')->get()->lists('nameAndTime', 'id');
我有两个 table:
课程
- id
- 姓名
- season_id
- teacher_id
- dates_id
- 描述 ...
course_dates
- id
- course_id
- 工作日
- 开始
- 停止 ...
前 table 是课程的基本内容。在第二个 table 中,我只存储课程的日期(即工作日 1 为星期一,开始 -> 开始时间和停止时间 -> 结束时间)。课程可以有多个日期。
现在我想为下拉列表创建一个列表,如下所示:
- 课程名称(日期,从-到)
- 相同的课程名称(另一天,从-到)
- 另一个课程名称(天,从-到)
我克服了一点辅助功能的那一天的名字。
如何使用访问器操作列表方法的参数?
感谢帮助!
课程模型
<?php
class Course extends \Eloquent {
protected $guarded = ['id', 'created_at', 'updated_at'];
//Relationship Holidays
public function holidays()
{
return $this->hasMany('Holiday');
}
//Relationship Teacher
public function teacher()
{
return $this->belongsTo('Teacher');
}
//Relationship User
public function bookings()
{
return $this->hasMany('Booking');
}
//Relationship Dates
public function dates()
{
return $this->hasMany('Dates');
}
//Accessor for Dates
public function getWeekdayAttribute()
{
//???
}
//Validation Rules
public static $rules = [
'name' => 'required',
'teacher_id' => 'required',
'description' => 'required',
'room' => 'required',
'price' => array('required', 'regex:/^\d*(\,\d{2})?$/'),
'maxuser' => 'required|numeric',
'target' => 'required'
];
//Custom Attribute Names
public static $names = [
'name' => 'Kursname',
'teacher_id' => 'Kursleiter',
'description' => 'Beschreibung',
'room' => 'Raum/Ort',
'price' => 'Preis',
'maxuser' => 'Max. Anzahl Teilnehmer',
'target' => 'Zielgruppe'
];
//Change Price Format Get
public function getPriceAttribute($price)
{
return number_format($price, 2, ',', '');
}
//Change Price Format Set
public function setPriceAttribute($price)
{
$this->attributes['price'] = str_replace(',', '.', $price);
}
//Unserialize Target Get
public function getTargetAttribute($target)
{
return unserialize($target);
}
//Serialize Target Set
public function setTargetAttribute($target)
{
$this->attributes['target'] = serialize($target);
}
}
日期模型
<?php
class Dates extends \Eloquent {
protected $guarded = ['id', 'created_at', 'updated_at'];
protected $table = 'course_dates';
public function courses()
{
return $this->belongsTo('Course')->orderBy('name');
}
//Validation Rules
public static $rules = [
'weekday' => 'required',
'start' => 'required|date_format:H:i',
'stop' => 'required|date_format:H:i'
];
//Custom Attribute Names
public static $names = [
'weekday' => 'Wochentag',
'start' => 'Von',
'stop' => 'Bis'
];
}
辅助函数
//Display Weekday
function showWeekday($id)
{
$weekday = array(
'1' => 'Montag',
'2' => 'Dienstag',
'3' => 'Mittwoch',
'4' => 'Donnerstag',
'5' => 'Freitag',
'6' => 'Samstag',
'0' => 'Sonntag'
);
return $weekday[$id];
}
如果我理解你的意思是这样的:
CourseDate 模型
public function getNameAndTimeAttribute(){
return $this->course->name . ' ' . $this->attributes['start'] . ' - ' . $this->attributes['stop'];
}
然后:
$dropdown = CourseDate::with('course')->get()->lists('nameAndTime', 'id');