Laravel lists with relationship for a dropdown
我有两张桌子:
课程
- ID
- 名称
- 季节ID
- 教师编号
- 日期ID
-
描述
…
course_dates
- ID
- course_id
- 工作日
- 开始
-
停止
…
第一张表是课程的基本内容。在第二个表中,我只存储课程的日期(即星期一的工作日 1,开始 -> 开始和停止的时间 -> 结束)。课程可以有多个日期。
现在我想为这样的下拉列表创建一个列表:
- 课程名称(天,从到)
- 相同的课程名称(另一天,从到到)
- 另一个课程名称(天,从到到)
我完成一个小辅助函数的那一天的名称。
如何使用访问器操作列表方法的参数?
感谢您的帮助!
课程模型
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<?php
class Course extends \\Eloquent { protected $guarded = [‘id’, ‘created_at’, ‘updated_at’]; //Relationship Holidays //Relationship Teacher //Relationship User //Relationship Dates //Accessor for Dates //Validation Rules
//Custom Attribute Names //Change Price Format Get //Change Price Format Set //Unserialize Target Get //Serialize Target Set |
日期模型
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php
class Dates extends \\Eloquent { protected $guarded = [‘id’, ‘created_at’, ‘updated_at’]; protected $table = ‘course_dates’; public function courses() //Validation Rules //Custom Attribute Names } |
辅助函数
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Display Weekday
function showWeekday($id) { $weekday = array( ‘1’ => ‘Montag’, ‘2’ => ‘Dienstag’, ‘3’ => ‘Mittwoch’, ‘4’ => ‘Donnerstag’, ‘5’ => ‘Freitag’, ‘6’ => ‘Samstag’, ‘0’ => ‘Sonntag’ ); return $weekday[$id]; |
- 你能告诉我们你已经拥有的代码吗?也可以,您可以使用访问器操作 lists() 的属性。
- 感谢您的代码。但是,我仍然不清楚这些下拉列表应该是什么样子/它们应该包含哪些数据……
- 我想要这种格式(例如网球星期一 12:55 – 13:55)。但是使用访问器时,我每次都会获得操纵值,但我只需要它用于下拉菜单。更好的是我手动创建数组。
如果我理解正确的话,可以这样做:
课程日期模型
|
1
2 3 |
public function getNameAndTimeAttribute(){
return $this->course->name . ‘ ‘ . $this->attributes[‘start’] . ‘ – ‘ . $this->attributes[‘stop’]; } |
然后:
|
1
|
$dropdown = CourseDate::with(‘course’)->get()->lists(‘nameAndTime’, ‘id’);
|
- 我什至不想理解 OP 问题,因为它太不清楚了,但我想你是对的 – 所以只有一件事: get 在 lists 之前,这很好。
- 为什么需要 get()?
来源:https://www.codenong.com/28888280/
