I have two tables:
Of course
- ID
- name
- season_id
- teacher_id
- dates_id
- Description ...
course_dates
- ID
- course_id
- weekday
- start
- stop ...
In the first table, the main things for the course. In the second table, I save only the course dates (i.e., Weekday 1 on Monday, start → start and stop time → for end). A course may have more than one date.
Now I want to create a list for a dropdown:
- course name (day, from-to)
- course name (another day, from-to)
- name of another course (day, from-to)
The name for the day I get a little helper function.
How can I manipulate the parameter of a list method with an accessor?
Thanks for the help!
course model
<?php
class Course extends \Eloquent {
protected $guarded = ['id', 'created_at', 'updated_at'];
public function holidays()
{
return $this->hasMany('Holiday');
}
public function teacher()
{
return $this->belongsTo('Teacher');
}
public function bookings()
{
return $this->hasMany('Booking');
}
public function dates()
{
return $this->hasMany('Dates');
}
public function getWeekdayAttribute()
{
}
public static $rules = [
'name' => 'required',
'teacher_id' => 'required',
'description' => 'required',
'room' => 'required',
'price' => array('required', 'regex:/^\d*(\,\d{2})?$/'),
'maxuser' => 'required|numeric',
'target' => 'required'
];
public static $names = [
'name' => 'Kursname',
'teacher_id' => 'Kursleiter',
'description' => 'Beschreibung',
'room' => 'Raum/Ort',
'price' => 'Preis',
'maxuser' => 'Max. Anzahl Teilnehmer',
'target' => 'Zielgruppe'
];
public function getPriceAttribute($price)
{
return number_format($price, 2, ',', '');
}
public function setPriceAttribute($price)
{
$this->attributes['price'] = str_replace(',', '.', $price);
}
public function getTargetAttribute($target)
{
return unserialize($target);
}
public function setTargetAttribute($target)
{
$this->attributes['target'] = serialize($target);
}
}
Date model
<?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');
}
public static $rules = [
'weekday' => 'required',
'start' => 'required|date_format:H:i',
'stop' => 'required|date_format:H:i'
];
public static $names = [
'weekday' => 'Wochentag',
'start' => 'Von',
'stop' => 'Bis'
];
}
Helper function
function showWeekday($id)
{
$weekday = array(
'1' => 'Montag',
'2' => 'Dienstag',
'3' => 'Mittwoch',
'4' => 'Donnerstag',
'5' => 'Freitag',
'6' => 'Samstag',
'0' => 'Sonntag'
);
return $weekday[$id];
}