onpage-dev / laravel-plugin
演示包
Requires (Dev)
- laravel/laravel: ^8.6
README
此包实现了在任何 Laravel 应用程序中的 OnPage 数据和数据结构。所有 CLI 命令必须在您的 Laravel 项目主目录中执行。
安装
将仓库添加到您的 composer 文件中并安装包
composer require onpage-dev/laravel-plugin:^v1
发布配置文件
php artisan vendor:publish --provider 'OnPage\OnPageServiceProvider'
运行插件迁移(我们使用 op_* 前缀作为我们的表)
php artisan migrate
升级
运行插件迁移(我们使用 op_* 前缀作为我们的表)
composer upgrade onpage-dev/laravel-plugin
配置
- 转到您的 On Page 并从“快照生成器”部分生成新的快照
- 复制快照生成器 API 令牌
- 将以下内容添加到您的
.env文件中ONPAGE_COMPANY=acme-inc ONPAGE_TOKEN=SNAPSHOT-GENERATOR-API-TOKEN
默认情况下,插件将在您的基目录中创建一个 onpage-models 目录。该文件夹将填充 On Page 生成的模型,您不应修改它们,因为它们是自动生成的。默认情况下,模型将位于 Data 命名空间中,因此您可以使用 \Data\ModelName::... 访问它们。为了使此操作生效,您需要指示 composer 预加载此文件夹。
{
...
"autoload": {
"psr-4": {
...
"Data\\": "onpage-models"
},
}
}
最后确保更新 composer。
composer dumpautoload
导入数据
要导入您的数据,请执行以下命令
php artisan onpage:import
错误预防 如果某些资源或字段已被删除或更改,导入将提示您是否要继续。您可以使用 --force 标志来忽略此警告。
php artisan onpage:import --force # Not recommended
无用导入预防 如果您尝试导入已经存在的相同数据,导入将停止。您可以使用 --anyway 标志来忽略此警告。
php artisan onpage:import --anyway # Not recommended
恢复以前的快照
每次您导入数据时,快照都会保存在您的 Laravel 项目的本地。如果您想恢复以前的快照,请执行回滚命令并输入与所选快照关联的编号。
php artisan onpage:rollback
查询数据
由于插件实际上并没有为您的数据生成相应的表和列,您将必须使用 whereField 函数而不是 where 子句,它们的工作方式相同。如果您在执行某些操作时遇到问题,请打开一个问题,解释您的用例。
// Search by native fields (id, created_at, updated_at, order) \Data\Products::where('id', 123123)->first(); // For other fields, use the `whereField` function \Data\Products::whereField('code', 'AT-1273')->first(); // By default, the filter will be applied on the current locale language \Data\Products::whereField('description', 'like', '%icecream%')->get(); // You force the filter to search for values in a specific language \Data\Products::whereField('description.it', 'like', '%gelato%')->paginate(); // To query relations, you can use the standard whereHas laravel function \Data\Products::whereHas('categories', function($q) { $q->whereField('is_visible', true); })->get(); // If you have a file field, you can query by token and by name \Data\Products::whereField('image:name', 'gelato.jpg')->get(); \Data\Products::whereField('image:token', '79YT34R8798FG7394N')->get(); // For dimension fields (dim2 and dim3) you can use both the x,y,z selectors, or the 0,1,2 selectors \Data\Products::whereField('dimension:x', '>', 100)->get(); // ... is the same as \Data\Products::whereField('dimension:0', '>', 100)->get();
高级 whereField 子句
如果您需要使用高级 where* 子句查询数据,您必须使用高级 whereField* 函数。
// Prefix 'or' for expand the results to another condition \Data\Products::whereField('name','icecream')->orWhereField('kcal','>',200)->get() // Suffix 'Not' for negative query \Data\Products::whereFieldNot('calories','like','low calorie')->get() // Suffix 'In' for search in an array of values \Data\Products::whereFieldIn('code',['4','8','15','16','23','42'])->get();
您还可以将它们组合起来以获得更高级的子句,它们的工作方式相同。
$q->orWhereFieldNot(...) $q->orWhereFieldIn(...) $q->whereFieldNotIn(...) $q->orWhereFieldNotIn(...)
获取值
一旦您获取了记录,您需要显示相关的数据。要访问记录的字段数据,您需要使用 ->val($field_name, $lang) 函数。默认情况下,$lang 将设置为当前默认语言。示例
// Native field values: $product->id // 123123 $product->created_at // 2022-01-01 00:00:00 $product->updated_at // 2023-02-03 01:30:00 // Field values: $product->val('name') // Icecream $product->val('name', 'it') // Gelato
多值字段
对于包含多个值的字段,val 函数将始终返回值的集合
echo $product->val('descriptions')->first(); // ... or foreach ($product->val('descriptions') as $descr) { echo "- $descr\n"; }
文件和图像字段
对于这些文件,返回的值将是一个 \OnPage\File::class 实例。要获取文件或图像 URL,请使用 ->link() 函数。链接将指向原始文件。
// Original size $product->val('specsheet')->name // Icecream-spec.pdf $product->val('specsheet')->token // R417C0YAM90RF $product->val('specsheet')->link() // https://acme-inc.onpage.it/api/storage/R417C0YAM90RF?name=icecream-spec.pdf
要将图像转换为缩略图,请添加如下所示的选项数组
// Maintain proportions width 200px $product->val('cover_image')->link(['x' => 200]) // Maintain proportions height 100px $product->val('cover_image')->link(['y' => 100]) // Crop image to width 200px and height 100px $product->val('cover_image')->link(['x' => 200, 'y' => 100]) // Maintain proportions and contain in a rectangle of width 200px and height 100px $product->val('cover_image')->link(['x' => 200, 'y' => 100, 'contain' => true]) // Convert the image to png (default is jpg) $product->val('cover_image')->link(['x' => 200, 'ext' => 'png'])
获取资源和字段
// Get a resource definition by name: $prod_res = \Onpage\resource('prodotti') // Returns \OnPage\Resource::class $prod_res->label; // "Prodotti" $prod_res->name; // "products" $prod_res->labels; // [ 'it' => 'Prodotti', 'en' => 'Products' ] // Get all fields available for this resource: $weight_field = $prod_res->field('weight'); // Returns \OnPage\Field::class or null $weight_field->label; // "Peso" $weight_field->name; // "weight" $weight_field->type; // "real" $weight_field->getUnit(); // "kg" $weight_field->labels; // [ 'it' => 'Peso', 'en' => 'Weight' ] $weight_field->descriptions; // 'Rappresenta il peso espresso in kg' $weight_field->description; // [ 'it' => 'Rappresenta il peso espresso in kg', 'en' => 'Represents the weight expressed in kg' ] // Get all resource fields: $prod_res->fields // Collection of \OnPage\Field::class // Print all fields of the products resource foreach($products->fields as $field) { echo "- $field->label (type: $field->type)\n" }
使用字段文件夹
$prod_res = \Onpage\resource('prodotti') // Returns \OnPage\Resource::class $prod_res->field_folders; // Collection of \OnPage\FieldFolders::class $field_folder = $prod_res->field_folders->first() // get the first field folder $field_folder->label; // "Folder1" $field_folder->fields; // Collection of \OnPage\Field::class $prod_res->things->first()->default_folder; // Get the default folder for the thing or null