72 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						||
 | 
						||
namespace App\Models;
 | 
						||
 | 
						||
use Illuminate\Database\Eloquent\Model;
 | 
						||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 | 
						||
 | 
						||
class EventMeta extends Model
 | 
						||
{
 | 
						||
    protected $table = 'event_meta';
 | 
						||
    protected $primaryKey = 'event_id';
 | 
						||
    public $incrementing = false;
 | 
						||
 | 
						||
    protected $fillable = [
 | 
						||
        'event_id',
 | 
						||
        'title',
 | 
						||
        'description',
 | 
						||
        'location',
 | 
						||
        'all_day',
 | 
						||
        'category',
 | 
						||
        'reminder_minutes',
 | 
						||
        'is_private',
 | 
						||
        'start_at',
 | 
						||
        'end_at',
 | 
						||
        'extra',
 | 
						||
    ];
 | 
						||
 | 
						||
    protected $casts = [
 | 
						||
        'all_day'     => 'boolean',
 | 
						||
        'is_private'  => 'boolean',
 | 
						||
        'start_at'    => 'datetime',
 | 
						||
        'end_at'      => 'datetime',
 | 
						||
        'extra'       => 'array',
 | 
						||
    ];
 | 
						||
 | 
						||
 | 
						||
 | 
						||
    /**
 | 
						||
     * convenience wrapper that mimics an “UPSERT” for meta rows.
 | 
						||
     *
 | 
						||
     * @param  int    $eventId     calendarobjects.id
 | 
						||
     * @param  array  $attrs       keyed the same way you’d pass to updateOrCreate
 | 
						||
     * @return static
 | 
						||
     */
 | 
						||
    public static function upsertForEvent(int $eventId, array $attrs): self
 | 
						||
    {
 | 
						||
        $values = array_merge($attrs, ['event_id' => $eventId]);
 | 
						||
 | 
						||
        return static::updateOrCreate(
 | 
						||
            ['event_id' => $eventId], // lookup
 | 
						||
            $values                   // insert/update
 | 
						||
        );
 | 
						||
    }
 | 
						||
 | 
						||
    /**
 | 
						||
     *
 | 
						||
     * relationships
 | 
						||
     */
 | 
						||
 | 
						||
    /* back-reference to Sabre’s calendarobjects row */
 | 
						||
    public function event(): BelongsTo
 | 
						||
    {
 | 
						||
        return $this->belongsTo(Event::class, 'event_id');
 | 
						||
    }
 | 
						||
 | 
						||
    /* back-reference to location record */
 | 
						||
    public function location(): BelongsTo
 | 
						||
    {
 | 
						||
        return $this->belongsTo(Location::class, 'location_id');
 | 
						||
    }
 | 
						||
}
 |