㈠ 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();