㈠ laravel的唯一验证怎么过滤软删除的数据
删除链接
编辑在app/views/articles/index.blade.php文件,在列表视图每个文章后面,添加删除菜单:
MENU:
{{ Form::open(array('method' => 'DELETE', 'route' => array('articles.destroy', $article->id))) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
执行版删除
修改 ArticlesController 控制器 destroy 动作权的代码:
public function destroy($id)
{
Article::destroy($id);
return Redirect::route('articles.index');
}
注意,删除后,直接跳转到列表页面。
㈡ php laravel5.0框架从静态化取出的数据中取出一个值
数据填充(也就是批量导入数据)不应该使用EloquentORM,而是用Laravel的Artisan命令行,使用起来也很简单专。帮助文档里写属的比较清楚了参考:/docs/4.2/migrations#database-seeding最下面的DatabaseSeeding
㈢ 请教各位 Laravel 中的 model 里面能对某个属性进行过滤操作吗
use App\User;
-
㈣ Laravel 中大量数据查询有什么技巧吗
->get(array('posts.id', 'posts.support', 'posts.against', 'users.username', 'posts.post_author', 'posts.post_title', 'posts.post_body'));
foreach($posts as $p){
$data[] = array(
'id' => $p -> id,
'support' => $p -> support,
'against' => $p -> against,
'username'=> $p -> username,
'post_author' => $p -> post_author,
'post_title' => $p -> post_title,
'post_body' => $p -> post_body
);
}
$res = View::make('home.index')
-> with('posts', $data);
Cache::forever('staticPageCache_home', $res);
}
// 返回缓存的数据
return Cache::get('staticPageCache_home');
}
}
这里我用到了三个api
1). Cache::has ,这个判断是说如果当前不存在 staticPageCache_home 这个名字的缓存, 就立即去取数据
2). Cache::forever, 这个从用例文档里面可知是"永久缓存"的意思, 因为我一般都是很勤劳的,如果发表了博文,自己再去后台立即刷新一下缓存就好了, 所以不需要设置过期啊失效时间之类的, 当然这个是要按各自的具体需求来的
3). Cache::get , 这句是从缓存里面取出 staticPageCache_home 这个名字的缓存, 然后作为响应内容返回
嗯, 就这么简单, 呵呵, 一个基本的缓存功能就完成了, laravel的确是不错地!
3. 为后台添加刷新缓存功能
还是贴代码吧, 不过也很简单:
// 刷新首页缓存(暂时只支持首页)
public function get_refreshcache() {
/*
@var $GID admin组id
*/
$GID = 1;
if ( Auth::user() -> gid === 1 ) {
$data = array();
$posts = Post::with('user')
->join('users', 'users.id', '=', 'posts.post_author')
-> order_by('posts.created_at', 'desc')
->get(array('posts.id', 'posts.support', 'posts.against', 'users.username', 'posts.post_author', 'posts.post_title', 'posts.post_body'));
foreach($posts as $p){
$data[] = array(
'id' => $p -> id,
'support' => $p -> support,
'against' => $p -> against,
'username'=> $p -> username,
'post_author' => $p -> post_author,
'post_title' => $p -> post_title,
'post_body' => $p -> post_body
);
}
$res = View::make('home.index')
-> with('posts', $data);
Cache::forever('staticPageCache_home', $res);
return '刷新首页缓存成功!';
}
return '对不起,只有管理员组才可进行此操作!';
}
我给后台添加了一个项目, 对应这个方法, 方法内容和首页的大同小异, 取数据, 然后Cache::forever 刷新一下缓存,就这么简单,当然了,上面的Auth::user() 判断是个简单的判断,只有管理员组才能进行刷新操作,呵呵
㈤ laravel怎么实现多条件模糊查询,而且前端选择查询的选项可以为空
获取当前的grid 循环判断获取需要的值 存进newData中 加载数据
var newData =[];
var gridData = $("#id").datagrid('getData');//获取当内前grid的所有数容据
for(var i =0;i<gridData.total;i++){
if (gridData[i].fieldId == '10') {
newData.push(gridData[i]);
}
}
$("#id").datagrid('loadData',newData);
㈥ Laravel中表单验证里unique在update时怎么排除当前记录
首先确认,后台的用户表,我设计表叫做badmin,每个管理员有用户名(),有昵称(nickname),有邮箱(email),有密码(password)这里玩个花,使用laravel的migration来建立表(实际上可以用不着使用这个工具建立表)1安装好最基本的laravel框架2创建migration文件:./artisanmigrate:makecreate-badmin-table3发现app/database/migration/下面多了一个php文件:2014_10_19_090336_create-badmin-table.php4往up和down里面增加内容;increments('id’);$table->string(‘nickname',100)->unique();$table->string('username',100)->unique();$table->string('email',100)->unique();$table->string('password',64);$table->timestamps();});}/***Reversethemigrations.**@returnvoid*/publicfunctiondown(){Schema::drop('badmin’);}}5配置好local的database,app/config/local/database.phpPDO::FETCH_CLASS,'default'=>'mysql','connections'=>array('mysql'=>array('driver'=>'mysql','host'=>'localhost','database'=>'test','username'=>'yejianfeng','password'=>'123456','charset'=>'utf8','collation'=>'utf8_unicode_ci','prefix'=>'',),),'migrations'=>'migrations',);6创建数据表:./artisanmigrate--env=local这个时候去数据库看,就发现多了一张badmin表,数据结构如下:CREATETABLE——badmin——(——id——int(10)unsignedNOTNULLAUTO_INCREMENT,——nickname——varchar(100)COLLATEutf8_unicode_ciNOTNULL,——username——varchar(100)COLLATEutf8_unicode_ciNOTNULL,——email——varchar(100)COLLATEutf8_unicode_ciNOTNULL,——password——varchar(64)COLLATEutf8_unicode_ciNOTNULL,——created_at——timestampNOTNULLDEFAULT'0000-00-0000:00:00',——updated_at——timestampNOTNULLDEFAULT'0000-00-0000:00:00',PRIMARYKEY(——id——),UNIQUEKEY——badmin_nickname_unique——(——nickname——),UNIQUEKEY——badmin_username_unique——(——username——),UNIQUEKEY——badmin_email_unique——(——email——))ENGINE=InnoDBAUTO_INCREMENT=2DEFAULTCHARSET=utf8COLLATE=utf8_unicode_ci;要问这里为什么多出了create_at和update_at,这是laravel默认为每个表创建的字段,而且在使用Eloquent进行增删改查的时候能自动更新这两个字段7创建个Model:'require|alpha_num|min:2','username'=>'require','email'=>'required|email|unique:users','password'=>'required|alpha_num|between:6,12|confirmed',];}这里必须要implementsUserInterface和RemindableInterface8把model和Auth关联上,修改app/config/auth.php'eloquent',//只有驱动为eloquent的时候才有用'model'=>'Badmin',);这里的driver可以是eloquent或者database,使用eloquent就告诉Auth组件说,用户认证类是Badmin这个类管的。这里的model是有命名空间的,就是说如果你的admin类是\Yejianfeng\Badmin,这里就应该改成‘\Yejianfeng\Badmin'9好了,这个时间其实逻辑部分已经搭建完毕了,你已经可以在controller种使用Auth::attempt(XXX)做权限认证Auth::user()获取登录用户(一个Badmin类)等。10下面要建立一个用户登录页面:11设置路由:'user.login','uses'=>'UserController@getLogin']);Route::get('user/login',['as'=>'login','uses'=>'UserController@getLogin']);Route::post('user/login',['as'=>'login','uses'=>'UserController@postLogin']);//需要登录验证才能操作的接口Route::group(array('before'=>'auth’),function(){Route::get(‘user/logout',['as'=>'logout','uses'=>'UserController@getLogout']);Route::get('user/dashboard',['as'=>'dashboard','uses'=>'UserController@getDashboard']);});12设置controller:Input::get('email’),'password'=>Input::get(‘password’)))){returnRedirect::to(‘user/dashboard’)->with(‘message',’成功登录‘);}else{returnRedirect::to('user/login’)->with(‘message',’用户名密码不正确‘)->withInput();}}//登出publicfunctiongetLogout(){Auth::logout();returnRedirect::to('user/login’);}publicfunctiongetDashboard(){returnView::make(‘user.dashboard’);}//添加新用户操作publicfunctiongetCreate(){returnView::make(‘user.create’);}//添加新用户操作publicfunctionpostCreate(){$validator=Validator::make(Input::all(),User::$rules);if($validator->passes()){$bAdmin=newBadmin();$bAdmin->nickname=Input::get(‘nickname’);$bAdmin->username=Input::get(‘username’);$bAdmin->email=Input::get(‘email’);$user->password=Hash::make(Input::get(‘password’));$user->save();Response::json(null);}else{Response::json(['message'=>‘注册失败'],410);}}}13设置下filter,app/filter.phpRoute::filter('auth',function(){if(Auth::guest()){if(Request::ajax()){returnResponse::make('Unauthorized',401);}else{returnRedirect::guest('/’);}}});将这里认证失败后的地址转到/路径14设置views/user/login.blade.php
㈦ 怎样在laravel框架路由群组添加过滤器
框架下载好了,但是想要很好的使用,可能我们还有一些东西需要知道,这就是配置。和项目有关的配置是在 app/config 文件夹里,但是除了这里还有一些配置可能是我们需要的。作为一个基础教程,我就不一一介绍了,只是选择一些大家配置比较多的地方讲解一下。
app/config 中的配置说明
在 app/config 文件夹中经常配置的一般有两个文件:app.php 和 database.php 两个文件,他们一个是配置项目杂项的、一个是配置数据库的。下面我就里面的常用配置做一下解释: 先是 app.php 文件
其实 app.php 后面还有一些内容,但那些基本上不需要你修改。(只有添加第三方包的时候才有需要,我们会到时候再讲)
接下来介绍 database.php 文件
<?php
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'meinv',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'//database/proction.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'meinv' => array( //这里就是上面例子里的默认连接数据库名,实际上是 mysql 数据库
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'prefix' => '',
),
),
);
?>
㈧ Laravel查询构建器如何将匹配数据过滤出去
你指的不起作用,我猜你应该不是用的 Eloquent::save() 去创建的。我建议你在 Request 层就做用户提交数据的验证,和处理,这样Controller 里会更加清晰!
㈨ laravel电商网站怎么防止库存脏数据
如果是要自己开发的话很麻烦的,首先要看很多专业性的书籍,不过如果只是想要建设一个网站的话,可以找专业的开发公司,也可以选择php语言的电子商务系统进行开发,如麦多php商城系统,ecshop等
㈩ 如何在 Laravel5 上优雅的统计从数据库中取出来的结果数
优雅的是用 scope, 在 User model 里面回加下面答 method
public function scopeVerified($query)
{
return $query->where('verify', 1);
}
然后 User::verified()->count();