承接 way/database 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

way/database

Composer 安装命令:

composer require way/database

包简介

README 文档

README

This package takes care of the model validation process for you. Rather than manually going through the tedious Validator::make(...) process, just save the model, and this package will handle the rest.

Usage

Install the package through Composer.

{
    "require": {
        "laravel/framework": "4.0.*",
        "way/database": "dev-master"
    }
}

Then, rather than extending Eloquent from your model, extend Way\Database\Model, like so:

<?php

class Dog extends Way\Database\Model {

}

Alternatively, edit app/config/app.php, and add a new item to the aliases array:

'Model' => 'Way\Database\Model'

Now, your models can simply extend Model.

<?php

class Dog extends Model {

}

Validation

This package hooks into Eloquent's save event, and automatically validates the model's current attributes against the rules that you have set for your model.

Here's an example of setting validation rules for the model:

<?php

class Dog extends Model {
    protected static $rules = [
        'name' => 'required'
    ];

    //Use this for custom messages
    protected static $messages = [
        'name.required' => 'My custom message for :attribute required'
    ];
}

Now, simply save the model as you normally would, and let the package worry about the validation. If it fails, then the model's save method will return false.

Here's an example of storing a new dog.

public function store()
{
    $dog = new Dog(Input::all());

    if ($dog->save())
    {
        return Redirect::route('dogs.index');
    }

    return Redirect::back()->withInput()->withErrors($dog->getErrors());
}

If using Eloquent's static create method, you can use the hasErrors() methods to determine if validation errors exist.

$dog = Dog::create(Input::all());

if ($dog->hasErrors()) ...

That's it! Have fun.

统计信息

  • 总下载量: 101.22k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 278
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 275
  • Watchers: 14
  • Forks: 54
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2013-04-12