andrewmile/fm-laravel

本包最新版本(0.4.1)没有提供许可证信息。

Laravel对PHP的FileMaker API友好的包装器

0.4.1 2016-09-09 20:22 UTC

This package is auto-updated.

Last update: 2024-09-22 08:52:55 UTC


README

安装Laravel框架

您需要Composer PHP包管理器来安装Laravel和FMLaravel。您可以从getcomposer.org安装Composer。

如果您尚未安装Laravel框架,您需要在终端运行以下命令来安装Laravel

composer create-project laravel/laravel YourProjectName

Composer安装完成后,您需要在终端运行以下命令,以便Laravel能够写入存储目录

chmod -R 777 storage

安装FMLaravel

在您的文本编辑器中打开composer.json文件,并在文件的"require"部分添加以下行。这将告诉Composer您的项目需要FMLaravel。

"andrewmile/fm-laravel": "0.4.*"

在终端运行以下命令以安装FMLaravel

composer update

配置

回到您的文本编辑器,打开config/app.php文件,并将以下行添加到providers数组中

'FMLaravel\Database\FileMakerServiceProvider',

在config/database.php中更改默认连接类型为filemaker

'default' => 'filemaker',

仍然在config/database.php中添加以下内容到connections数组中

'filemaker' => [
	'driver'   => 'filemaker',
	'host'     => env('DB_HOST'),
	'database' => env('DB_DATABASE'),
	'username' => env('DB_USERNAME'),
	'password' => env('DB_PASSWORD'),
],

在您的根目录下创建一个名为.env的新文件,并添加以下内容,同时包括您的数据库连接信息

DB_HOST=YourHost
DB_DATABASE=YourDatabase
DB_USERNAME=YourUsername
DB_PASSWORD=YourPassword

请注意,如果您正在使用版本控制,您不希望.env文件成为您仓库的一部分,因此它默认包含在.gitignore中。

使用方法

创建模型

Laravel包含一个名为artisan的命令行工具,您可以使用它执行许多有用的任务,包括生成文件以避免重复输入样板代码。

您将为在项目中使用的每个表创建一个模型类。Laravel使用约定,使用单数模型名称。要为tasks表创建一个模型,请在终端运行以下命令

php artisan make:model Task

为您生成的文件位于app/Task.php。这个类扩展了Laravel的Eloquent Model类,但我们需要它扩展FMLaravel Model类。从新创建的Task.php文件中删除以下行

use Illuminate\Database\Eloquent\Model;

然后替换为以下行

use FMLaravel\Database\Model;

在您的模型类中,您需要指定在查询FileMaker数据库中的tasks表时应使用的布局。为此,在Task类内部添加以下行

protected $layoutName = 'YourTaskLayoutName';

默认情况下,Laravel会假设您表的键是"id"。如果您有不同的主键,您需要在类内部添加以下内容

protected $primaryKey = 'YourTaskPrimaryKey';

查询表

在一个用于查询FileMaker任务数据的文件中,在文件顶部添加以下内容

use App\Task;

现在您已经导入了Task模型,您可以对tasks表执行以下类型的查询

查找所有记录

$tasks = Task::all();

通过其主键查找记录

$task = Task::find(3); //will find the task record with a primary key of 3

查找符合您查找标准的任务。您可以通过传递两个参数(第一个是字段名,第二个是要匹配的值)或传递一个包含字段名和值的数组来实现。

//will find tasks where the task_name is 'Go to the store'
$tasks = Task::where('task_name', 'Go to the store')->get();

//will find tasks where task_name is 'Go to the store' and priority is 1
$tasks = Task::where([
	'task_name' => 'Go to the store',
	'priority'  => 1
])->get();

如果您想将查询限制为符合您标准的第一个记录,可以使用first()而不是get()。

$tasks = Task::where('task_name', 'Go to the store')->first();

如果您想指定要限制查询的记录数,可以使用limit()方法。

//will find the first 10 records that match the find criteria
$tasks = Task::where('task_name', 'Go to the store')->limit(10)->get();

您还可以使用skip()方法指定要跳过的记录数。

//will find records that match the find criteria after skipping the first 10
$tasks = Task::where('task_name', 'Go to the store')->skip(10)->get();

这些查询方法可以串联使用,因此您可以进行如下操作

//will find 10 records that match the find criteria after skipping the first 100
$tasks = Task::where('task_name', 'Go to the store')->skip(100)->limit(10)->get();

如果您在同一个查询中使用skip()和limit(),并且想将它们合并成一个方法,您也可以使用以下方法

//will find 10 records that match the find criteria after skipping the first 100
$tasks = Task::where('task_name', 'Go to the store')->setRange(100, 10)->get();

默认情况下,您在模型属性 $layoutName 上设置的布局将用于查询数据。但是,如果您需要为特定查询指定不同的布局,可以使用 setLayout() 方法。

//will use the PastDue layout to perform the query
$tasks = Task::where('task_name', 'Go to the store')->setLayout('PastDue')->get();

待办事项

  • 编写身份验证的文档