Laravel-admin 資料來自外部 API

蕭風發表於2020-03-19

背景: 因為laravel-admin 文件缺少,同事又問了起來;所以在此做一個記錄,希望能幫助更多的phper

<?php

namespace App\Models\Movie;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;

class InTheater extends Model
{
   // 必須加上的
   public static function with($relations)
    {
        return new static;
    }

    public function paginate()
    {
        $perPage = Request::get('per_page', 10);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        $data = file_get_contents("https://api.douban.com/v2/movie/in_theaters?city=上海&start=$start&count=$perPage");

        $data = json_decode($data, true);

        extract($data);

        $movies = static::hydrate($subjects);

        $paginator = new LengthAwarePaginator($movies, $total, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    // 覆蓋`orderBy`來收集排序的欄位和方向
    public function orderBy($column, $direction = 'asc')
    {

    }

    // 覆蓋`where`來收集篩選的欄位和條件
    public function where($column, $operator = null, $value = null, $boolean = 'and')
    {

    }

    ...
}

呼叫 paginate 方法進行覆蓋

   protected function grid()
    {
       $grid->paginate();
   }
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章