robinthijsen/laravel-monday

Laravel 的 Monday API 支持

v1.1 2024-08-09 21:50 UTC

This package is auto-updated.

Last update: 2024-09-13 08:55:32 UTC


README

Latest Version on Packagist

安装

这是一个 Laravel 扩展包,用于轻松使用 Monday.com API。
您也可以在纯 PHP 中使用它。

首先通过 composer 安装该包

composer require robinthijsen/laravel-monday

(可选) 您可以将配置文件发布出来以更改默认配置。

# That's currently changing anything so don't do it
# I'm planning to add some config for future version
php artisan vendor:publish --tag="monday-config"

这是已发布配置文件的内容
您应该在 .env 文件中定义您的 Monday.com API 密钥。

return [
    'token' => env('MONDAY_API_TOKEN'),
    'version' => env('MONDAY_API_VERSION', '2024-04'),
];

使用方法

在开始使用此包之前,您应该从 Monday.com 获取您的 API 密钥
并查看 Monday.com API 文档 中关于 Monday 对象的文档。

好的,让我们开始使用这个包。
如果您对动态类和属性有些了解,这个包是这样工作的。

# This is a list of every Monday object you can use in this package
# Or find them in the src/Objects directory
use \RobinThijsen\LaravelMonday\Objects\Account;
use \RobinThijsen\LaravelMonday\Objects\AccountProduct;
use \RobinThijsen\LaravelMonday\Objects\Block;
use \RobinThijsen\LaravelMonday\Objects\Board;
use \RobinThijsen\LaravelMonday\Objects\Column;
use \RobinThijsen\LaravelMonday\Objects\ColumnValue;
use \RobinThijsen\LaravelMonday\Objects\Doc;
use \RobinThijsen\LaravelMonday\Objects\Group;
use \RobinThijsen\LaravelMonday\Objects\Icon;
use \RobinThijsen\LaravelMonday\Objects\Item;
use \RobinThijsen\LaravelMonday\Objects\Plan;
use \RobinThijsen\LaravelMonday\Objects\Team;
use \RobinThijsen\LaravelMonday\Objects\User;
use \RobinThijsen\LaravelMonday\Objects\Workspace;
use \RobinThijsen\LaravelMonday\Objects\WorkspaceSetting;

您可以通过在您想查询的对象上调用 ::query()::find() 方法来启动查询构建器(如果对象不接受查询和唯一查询,将抛出异常)。

use \RobinThijsen\LaravelMonday\Objects\Board;

# This is an example of querying all the boards
# The following params are for Board object
# Check params for other objects in the src/Objects directory
/**
* @param int|array|null $ids => default null
 * @param int|array|null $workspaceIds => default null
 * @param int $limit => default 25
 * @param int $page => default 1
 * @param string $kind => default BoardKind::PUBLIC
 * @param string $state => default State::ACTIVE
 * @return \RobinThijsen\LaravelMonday\Objects\Board[]
 */
$boards = Board::query();

::query()::find() 根据您查询的对象接受动态参数。

然后,您可以使用以下方法链式调用查询构建器: ->with()->withObject()

use \RobinThijsen\LaravelMonday\Objects\Board;
use \RobinThijsen\LaravelMonday\Objects\Item;

# This is an example of querying all the boards with the items
$boards = Board::query()
    ->with('id', 'name')
    ->withObject(Item::class, [], ['id', 'name']);
/**
 * FieldsName are the fields you want to get from the object
 * If the field is not in the object or if it is an object field, an Exception will be thrown
 * 
 * @param string ...$fieldNames
 */
public function with(...$fieldNames)
/**
 * Get an object with the given field names
 * 
 * @param string $fieldName
 * @param array|Closure $params
 * @param array $fields
 */
public function withObject($fieldName, $params = [], $fields = [])

要调用对象的对象,您需要使用 withObject 方法。
但需要用闭包方法替换参数 $params。

use \RobinThijsen\LaravelMonday\Objects\Board;
use \RobinThijsen\LaravelMonday\Objects\Item;
use \RobinThijsen\LaravelMonday\Objects\ColumnValue;

// In this exemple, I recover the board with id 123456
// with all his items and all the column values of each item with specific default fields for each object
$board = Board::find(123456)
    ->with('id', 'name')
    ->withObject(Item::class, function ($builder) {
        $builder->with('id', 'name')
        ->withObject(ColumnValue::class, [], ['text', 'value']);
    });

当您的查询准备好后,您可以调用 ->get() 方法来获取结果。

// This will return you an instance of Board class with asked fields as props
// For this exemple, it will be id and name
$board = Board::find(123456)
    ->with('id', 'name')
    ->get();

作者

Robin Thijsen

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 获取更多信息。