我想自动添加created_by和modified_by字段到Laravel 4中数据库表的每次插入/更新,无论我使用的是Eloquent还是Query Builder.但是,并非所有表都包含这些字段,因此任何解决方案都必须在添加之前检查这些列是否存在.
我已经扩展了Illuminate\Database\Eloquent\Model类并编写了一个覆盖方法save(),以便为每个保存的记录添加一些额外的元数据字段.
这很好,除非我使用查询生成器执行插入,然后绕过它.看一下这个Model类,看起来数据库操作实际上是使用查询构建器完成的.
我看过Illuminate\Database\Query\Builder模型,看起来我可能会为insert()和编写覆盖方法update().
这是为每次插入/更新执行某项任务的合理方式,还是在以后遇到麻烦?
对于我的项目,我使用学生表进行身份验证和存储信息.对于学生注册,与登录认证不同,所有数据都插入到学生故事中.学生表的属性是[id(primary key and auto incremented), username, password, stdname, stdage, created_at, updated_at].
注册
public function post_register()
{
$input = Input::all();
$rules = array(
'email' => 'required|email|Between:3, 64|unique:student,username',
'studentPass' => 'required',
'studentName' => 'required',
'studentAge' => 'integer|Min:18|Max:45');
$messages = array(
'email.required' => 'You forgot to enter your email id!',
'email.between' => 'Email must be between 3 to 64 character!',
'studentPass.required' => 'You forgot to enter your password!',
'studentName.required' => 'You forgot to enter student name!',
'studentAge.integer' => 'Student …Run Code Online (Sandbox Code Playgroud) 我想在Pagination :: slider视图中做一些更改,例如添加类和更改箭头样式.我怎样才能做到这一点?
好吧,我正在尝试使用labrvel 4的Hybridauth.但是当我尝试使用facebook登录时,我似乎变得非常普遍:
验证失败!Facebook返回了无效的用户ID.
我已阅读所有其他帖子,并且没有运气,所以只是希望有人可以帮助我.
我遵循了这个教程:http://www.mrcasual.com/on/coding/laravel4-package-management-with-composer/
并尝试了其他几种配置,但没有成功.
这是我的config/hybridauth.php
<?php
return array(
"base_url" => "http://myapp.dev/social/auth/",
"providers" => array (
"Facebook" => array (
"enabled" => true,
"keys" => array ( "id" => "****", "secret" => "****" ),
),
),
);
Run Code Online (Sandbox Code Playgroud)
这是我的路线:
Route::get('social/{action?}', array("as" => "hybridauth", function($action = "")
{
// check URL segment
if ($action == "auth") {
// process authentication
try {
Hybrid_Endpoint::process();
}
catch (Exception $e) {
// redirect back to http://URL/social/
return Redirect::route('hybridauth');
}
return;
} …Run Code Online (Sandbox Code Playgroud) 我的laravel4应用程序中有一个小型图像生成器。生成图像大约需要700毫秒,因此我开始在服务器上缓存生成的结果,并将其返回给浏览器,这样可以节省一些时间。
由于图像一旦生成就永远不会改变,所以我想告诉浏览器在本地缓存图像,并使用以下代码完成了此操作:
$path = $cacheFolderPath . $cacheFileName;
if (File::exists( $path )){
$response = Response::make(File::get($path));
$response->header('Content-Type', 'image/png');
$response->header('Content-Disposition', 'inline; filename="'.$cacheFileName.'"');
$response->header('Content-Transfer-Encoding', 'binary');
$response->header('Cache-Control', 'public, max-age=10800, pre-check=10800');
$response->header('Pragma', 'public');
$response->header('Expires', date(DATE_RFC822,strtotime(" 2 day")) );
$response->header('Last-Modified', date(DATE_RFC822, File::lastModified($path)) );
$response->header('Content-Length', filesize($path));
return $response;
}
Run Code Online (Sandbox Code Playgroud)
这会将带有状态代码的图像发送200 OK到带有以下标头的浏览器:
Cache-Control:max-age=10800, pre-check=10800, public
Connection:Keep-Alive
Content-Disposition:inline; filename="pie_0_normal.png"
Content-Length:2129
Content-Transfer-Encoding:binary
Content-Type:image/png
Date:Wed, 07 Aug 2013 10:29:20 GMT
Expires:Fri, 09 Aug 13 10:29:20 +0000
Keep-Alive:timeout=5, max=93
Last-Modified:Wed, 07 Aug 13 10:14:42 +0000
Pragma:public
Server:Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
Set-Cookie:laravel_session=767487mhf6j2btv3k01vu56174; …Run Code Online (Sandbox Code Playgroud) 假设我有三个表用户,users_groups和groups.如何通过users_groups表获取用户的组?
class User extends Eloquent {
protected $table = 'users';
}
class Groups extends Eloquent {
protected $table = 'groups';
}
class Usergroups extends Eloquent {
protected $table = 'users_groups';
}
Run Code Online (Sandbox Code Playgroud) 我刚开始在laravel 4中实现restful控制器.我不明白在使用这种路由方式时如何将参数传递给控制器中的函数.
控制器:
class McController extends BaseController
{
private $userColumns = array("stuff here");
public function getIndex()
{
$apps = Apps::getAllApps()->get();
$apps=$apps->toArray();
return View::make('mc')->nest('table', 'mc_child.table',array('apps'=>$apps, 'columns'=>$this->userColumns));
}
public function getTable($table)
{
$data = $table::getAll()->get();
$data=$data->toArray();
return View::make('mc')->nest('table', 'mc_child.table',array('apps'=>$apps, 'columns'=>$this->userColumns));
}
}
Run Code Online (Sandbox Code Playgroud)
路线:
Route::controller('mc', 'McController');
Run Code Online (Sandbox Code Playgroud)
我能够访问这两个URL,因此我的路由工作正常.使用此路由和控制器方法时,如何将参数传递给此控制器?
我无法在PhpStorm中设置自动完成功能来处理Laravel 4 blade.php文件.我到处搜索,但我找不到其他用户的问题.我已经在https://github.com/barryvdh/laravel-ide-helper上正确安装了此存储库,但我仍然得到刀片页面的空白格式.
谢谢
我是laravel 4的新手,我正在尝试按照Apigee定义的最佳实践创建一个rest API.
apigee定义的最佳实践之一是对json属性键使用camel case,这种方式在Javascript中使用API时,相应的对象将遵循属性代码约定(camel case).
我希望能够在蛇案例之后定义数据表列,但是当通过我的api检索有说服力的对象时,相应的JSON必须遵循骆驼案例.
我读到了一个静态变量$ snakeAttributes,它可以在模型类中设置,它的文档说"指示属性是否是蛇形数组".我试图覆盖这个变量并将其设置为false(MyResource类)但是在执行下面的代码时,json仍然以蛇形式出现:
码:
$resource = MyResource::find($id);
return Response::json($resource);
Run Code Online (Sandbox Code Playgroud)
JSON:
{
first_name: 'Michael',
created_at: "2013-10-24 15:30:01",
updated_at: "2013-10-24 15:30:01"
}
Run Code Online (Sandbox Code Playgroud)
有人知道如何解决这个问题吗?
我正在尝试显示错误消息,以防所选字段在数据库中重复。为此,我使用 laravel 验证需要唯一。我在重定向时遇到问题 这是商店控制器
public function store() {
$rules = array(
'car' => array('required', 'unique:insur_docs,car_id'),
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails()) {
// Validation has failed.
return Redirect::to('insur_docs/create')->with_input()->with_errors($validation);
} else {
$data = new InsurDoc();
$data->ownership_cert = Input::get('ownership_cert');
$data->authoriz = Input::get('authoriz');
$data->drive_permis = Input::get('drive_permis');
$data->sgs = Input::get('sgs');
$data->tpl = Input::get('tpl');
$data->kasko = Input::get('kasko');
$data->inter_permis = Input::get('inter_permis');
$data->car_id = Input::get('car');
$data->save();
// redirect
return Redirect::to('/');
}
}
Run Code Online (Sandbox Code Playgroud)
这是路线
Route::get('insur_docs/create', array('as' => 'insur_docs.create','uses' => 'Insur_DocController@create'));
Run Code Online (Sandbox Code Playgroud)
创建控制器
public function create() {
$cars …Run Code Online (Sandbox Code Playgroud) laravel ×10
laravel-4 ×9
php ×3
eloquent ×2
blade ×1
facebook ×1
http-caching ×1
http-headers ×1
hybridauth ×1
mysql ×1
pagination ×1
php-5.3 ×1
phpstorm ×1