Reputation: 457
I have two tables:
In the first table are the basic stuff for the course. In the second table i only store the dates of the course (i.e. weekday 1 for monday, start -> the time of beginning and stop -> for the ending). The course can have more than one date.
Now i want to create a list for a dropdown like so:
The name for the day i get over a little helper function.
How can i manipulate the parameter for the lists method, with an accessor?
Thanks for help!
course model
<?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);
}
}
Dates 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');
}
//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'
];
}
Helper function
//Display Weekday
function showWeekday($id)
{
$weekday = array(
'1' => 'Montag',
'2' => 'Dienstag',
'3' => 'Mittwoch',
'4' => 'Donnerstag',
'5' => 'Freitag',
'6' => 'Samstag',
'0' => 'Sonntag'
);
return $weekday[$id];
}
Upvotes: 4
Views: 5746
Reputation: 153030
If I understood you right something like this will do:
CourseDate model
public function getNameAndTimeAttribute(){
return $this->course->name . ' ' . $this->attributes['start'] . ' - ' . $this->attributes['stop'];
}
And then:
$dropdown = CourseDate::with('course')->get()->lists('nameAndTime', 'id');
Upvotes: 5