aboutsummaryrefslogtreecommitdiff
path: root/app/Models/BookmarkSite.php
blob: 3c9cc5dd345c3d9dc191eb60be0db0b9dea190a7 (plain)
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
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BookmarkSite extends Model {
    use HasFactory;
    protected $table = "bookmark__sites";
    protected $fillable = ['name', 'description', 'url', 'category'];

    public function category() {
        return $this->belongsTo(BookmarkCategory::class, 'category');
    }
    public static function insertBookmark(string $name, string $url, int $category) {
        $category = BookmarkCategory::where('id', $category)->firstOrFail();
        $newBookmark = new BookmarkSite;
        $newBookmark->name = $name;
        $newBookmark->url = $url;
        $newBookmark->category = $category->id;
        $newBookmark->save();
    }

    public static function importBookmark(array $data) {
        foreach ($data as $site) {
            $newBookmark = new BookmarkSite;
            $newBookmark->name = $site['name'];
            $newBookmark->description = $site['description'];
            $newBookmark->url = $site['url'];
            $newBookmark->category = $site['category_id'];
            $newBookmark->save();
        }
    }
}