sdavis1902/eloquent-vars 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sdavis1902/eloquent-vars

Composer 安装命令:

composer require sdavis1902/eloquent-vars

包简介

Add ability to save additional fields or Vars on a Laravel model without a specific db column for it

README 文档

README

Software License Total Downloads

Add ability to save additional fields or Vars on a Laravel model without a specific db column for it

Install

Via Composer

$ composer require sdavis1902/eloquent-vars

Add the service provider ( not required for 5.5 )

sdavis1902\EloquentVars\EloquentVarsServiceProvider::class,

Publish and run migration

$ php artisan vendor:publish --provider="sdavis1902\EloquentVars\EloquentVarsServiceProvider"
$ php artisan migrate

Usage

Add the trait to your model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use sdavis1902\EloquentVars\EloquentVarsTrait;

class Task extends Model {

    use EloquentVarsTrait;

Now you can do the following...

$task->setVar('temporary_field', 'The value');
$task->setVar('temporary_id', 1);

echo $task->getVar('temporary_field');

$task->deleteVar('temporary_field');

Note, the above require the model object to have an id, so will need to have been written to the database already.

If you want use a more eloquent method, you can use the following. Additionally, the following method will work on new model instances that have not been inserted yet.

// saving new model and it's vars all at once
$task = new \App\Task;
$task->name = 'Some Task';

// now our vars
$task->vars->first_var = 'test';

$task->save();

// retreiving a model and accessing it's vars directly
$task = \App\Task::find(1);
echo $task->vars->first_var;

// save another var
$task->vars->second_var = 'another test';
$task->save();

// if you try to get a var that doesn't exist, it simply returns null
var_dump($task->vars->no_var);

Additionally, the trait includes a relationship for the vars which allows you to eager load. The above method of accessing vars uses this relationship, so you can eager load and then use the vars like above.

$tasks = \App\Task::with('ModelVars')->get();

foreach($tasks as $task){
    if($task->vars->first_var){
        echo $task->vars->first_var;
    }
}

Note, values are stored as a string, so if you save an int, when you get it, it will be a string.

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 14.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固