laravel 常用命令

阅读:454 2019-03-19 14:43:19 来源:新网

artisan常用命令

//在版本5.1.11新添加,见http://laravel-china.org/docs/5.1/5.1/authorization#creating-policiesphpartisanmake:policypostpolicy//针对命令显示帮助信息phpartisan--helpor-h//抑制输出信息phpartisan--quietor-q//打印laravel的版本信息phpartisan--versionor-v//不询问在任何交互性的问题phpartisan--on-interactionor-n//强制输出ansi格式phpartisan--ansi//禁止输出ansi格式phpartisan--no-ansi//显示当前命令行运行的环境phpartisan--env//-v|vv|vvv通过添加v的个数来控制命令行输出内容的详尽情况:1个代表正常输出,2个代表输出更多消息,3个代表调试phpartisan--verbose//移除编译优化过的文件(storage/frameworks/compiled.php)phpartisanclear-compiled//显示当前框架运行的环境phpartisanenv//显示某个命令的帮助信息phpartisanhelp//显示所有可用的命令phpartisanlist//进入应用交互模式phpartisantinker//进入维护模式phpartisandown//退出维护模式phpartisanup//优化框架性能//--force强制编译已写入文件(storage、frameworks、compiled.php)//--psr不对composer的dump-autoload进行优化phpartisanoptimize[--force][--psr]//启动内置服务器phpartisanserve//更改默认端口phpartisanserver--port8080//使其在本地服务器外也可正常工作phpartisanserver--host0.0.0.0//更改命名空间phpartisanapp:namenamespace//清除过期的密码重置令牌phpartisanauth:clear-resets//清空应用缓存phpartisancache:cache//创建缓存数据库表phpartisancache:table//合并所有的配置信息为一个,提高加载速度phpartisanconfig:cache//移除配置缓存文件phpartisanconfig:clear//程序内部调用artisan命令$exitcode=artisan::call('config:cache');//运行所有的seed假数据生成类//--class可以指定运行的类,默认是:"databaseseeder"//--database可以指定数据库//--force当处于生产环境时强制执行操作phpartisandb:seed[--class[="..."]][--database[="..."]][--force]//基于注册的信息,生成遗漏的events和handlersphpartisanevent:generate//生成新的处理类//--command需要处理器处理的命令类名字phpartisanhandler:command[--command="..."]name//创建一个新的时间处理类//--event需要处理器处理的事件类名字//--queued需要处理器使用队列处理的事件类名字phpartisanhandler:event[--event="..."][--queued]name//生成应用的key(会覆盖)phpartisankey:generate//在默认情况下,这将创建未加入队列的自处理命令//通过--handler标识来生成一个处理器,用--queued来使其入队列.phpartisanmake:command[--handler][--queued]name//创建一个新的artisan命令//--command命令被调用的名称。(默认为:"command:name")phpartisanmake:console[--command[="..."]]name//创建一个新的资源控制器//--plain生成一个空白的控制器类phpartisanmake:controller[--plain]namephpartisanmake:controllerapp\admin\http\controllers\dashboardcontroller//创建一个新的事件类phpartisanmake:eventname//创建一个新的中间件类phpartisanmake:middlewarename//创建一个新的迁移文件//--create将被创建的数据表.//--table将被迁移的数据表.phpartisanmake:migration[--create[="..."]][--table[="..."]]name//创建一个新的eloquent模型类phpartisanmake:modelname//创建一个新的服务提供者类phpartisanmake:providername//创建一个新的表单请求类phpartisanmake:requestname//数据库迁移//--database指定数据库连接(下同)//--force当处于生产环境时强制执行,不询问(下同)//--path指定单独迁移文件地址//--pretend把将要运行的sql语句打印出来(下同)//--seedseed任务是否需要被重新运行(下同)phpartisanmigrate[--database[="..."]][--force][--path[="..."]][--pretend][--seed]//创建迁移数据库表phpartisanmigrate:install[--database[="..."]]//重置并重新运行所有的migrations//--seeder指定主seeder的类名phpartisanmigrate:refresh[--database[="..."]][--force][--seed][--seeder[="..."]]//回滚所有的数据库迁移phpartisanmigrate:reset[--database[="..."]][--force][--pretend]//回滚最最近一次运行的迁移任务phpartisanmigrate:rollback[--database[="..."]][--force][--pretend]//migrations数据库表信息phpartisanmigrate:status//为队列数据库表创建一个新的迁移phpartisanqueue:table//监听指定的队列//--queue被监听的队列//--delay给执行失败的任务设置延时时间(默认为零:0)//--memory内存限制大小,单位为mb(默认为:128)//--timeout指定任务运行超时秒数(默认为:60)//--sleep等待检查队列任务的秒数(默认为:3)//--tries任务记录失败重试次数(默认为:0)phpartisanqueue:listen[--queue[="..."]][--delay[="..."]][--memory[="..."]][--timeout[="..."]][--sleep[="..."]][--tries[="..."]][connection]//查看所有执行失败的队列任务phpartisanqueue:failed//为执行失败的数据表任务创建一个迁移phpartisanqueue:failed-table//清除所有执行失败的队列任务phpartisanqueue:flush//删除一个执行失败的队列任务phpartisanqueue:forget//在当前的队列任务执行完毕后,重启队列的守护进程phpartisanqueue:restart//对指定id的执行失败的队列任务进行重试(id:失败队列任务的id)phpartisanqueue:retryid//指定订阅iron.io队列的链接//queue:iron.io的队列名称.//url:将被订阅的url.//--type指定队列的推送类型.phpartisanqueue:subscribe[--type[="..."]]queueurl//处理下一个队列任务//--queue被监听的队列//--daemon在后台模式运行//--delay给执行失败的任务设置延时时间(默认为零:0)//--force强制在「维护模式下」运行//--memory内存限制大小,单位为mb(默认为:128)//--sleep当没有任务处于有效状态时,设置其进入休眠的秒数(默认为:3)//--tries任务记录失败重试次数(默认为:0)phpartisanqueue:work[--queue[="..."]][--daemon][--delay[="..."]][--force][--memory[="..."]][--sleep[="..."]][--tries[="..."]][connection]//生成路由缓存文件来提升路由效率phpartisanroute:cache//移除路由缓存文件phpartisanroute:clear//显示已注册过的路由phpartisanroute:list//运行计划命令phpartisanschedule:run//为session数据表生成迁移文件phpartisansession:table//从vendor的扩展包中发布任何可发布的资源//--force重写所有已存在的文件//--provider指定你想要发布资源文件的服务提供者//--tag指定你想要发布标记资源.phpartisanvendor:publish[--force][--provider[="..."]][--tag[="..."]]phpartisantail[--path[="..."]][--lines[="..."]][connection]

composer命令

composercreate-projectlaravel/laravelfolder_namecomposerinstallcomposerupdatecomposerdump-autoload[--optimize]composerself-updatecomposerrequire[options][--][vendor/packages]...

db类的基础使用

db::connection('connection_name');//运行数据库查询语句$results=db::select('select*fromuserswhereid=?',[1]);$results=db::select('select*fromuserswhereid=:id',['id'=>1]);//运行普通语句db::statement('droptableusers');//监听查询事件db::listen(function($sql,$bindings,$time){code_here;});//数据库事务处理db::transaction(function(){db::table('users')->update(['votes'=>1]);db::table('posts')->delete();});db::begintransaction();db::rollback();db::commit();查询语句构造器//取得数据表的所有行db::table('name')->get();//取数据表的部分数据db::table('users')->chunk(100,function($users){foreach($usersas$user){//}});//取回数据表的第一条数据$user=db::table('users')->where('name','john')->first();db::table('name')->first();//从单行中取出单列数据$name=db::table('users')->where('name','john')->pluck('name');db::table('name')->pluck('column');//取多行数据的「列数据」数组$roles=db::table('roles')->lists('title');$roles=db::table('roles')->lists('title','name');//指定一个选择字句$users=db::table('users')->select('name','email')->get();$users=db::table('users')->distinct()->get();$users=db::table('users')->select('nameasuser_name')->get();//添加一个选择字句到一个已存在的查询语句中$query=db::table('users')->select('name');$users=$query->addselect('age')->get();//使用where运算符$users=db::table('users')->where('votes','>',100)->get();$users=db::table('users')->where('votes','>',100)->orwhere('name','john')->get();$users=db::table('users')->wherebetween('votes',[1,100])->get();$users=db::table('users')->wherenotbetween('votes',[1,100])->get();$users=db::table('users')->wherein('id',[1,2,3])->get();$users=db::table('users')->wherenotin('id',[1,2,3])->get();$users=db::table('users')->wherenull('updated_at')->get();db::table('name')->wherenotnull('column')->get();//动态的where字句$admin=db::table('users')->whereid(1)->first();$john=db::table('users')->whereidandemail(2,'john@doe.com')->first();$jane=db::table('users')->wherenameorage('jane',22)->first();//orderby,groupby,和having$users=db::table('users')->orderby('name','desc')->groupby('count')->having('count','>',100)->get();db::table('name')->orderby('column')->get();db::table('name')->orderby('column','desc')->get();db::table('name')->having('count','>',100)->get();//偏移&限制$users=db::table('users')->skip(10)->take(5)->get();joins//基本的join声明语句db::table('users')->join('contacts','users.id','=','contacts.user_id')->join('orders','users.id','=','orders.user_id')->select('users.id','contacts.phone','orders.price')->get();//leftjoin声明语句db::table('users')->leftjoin('posts','users.id','=','posts.user_id')->get();//select*fromuserswherename='john'or(votes>100andtitle<>'admin')db::table('users')->where('name','=','john')->orwhere(function($query){$query->where('votes','>',100)->where('title','<>','admin');})->get();聚合$users=db::table('users')->count();$price=db::table('orders')->max('price');$price=db::table('orders')->min('price');$price=db::table('orders')->avg('price');$total=db::table('users')->sum('votes');原始表达句$users=db::table('users')->select(db::raw('count(*)asuser_count,status'))->where('status','<>',1)->groupby('status')->get();//返回行db::select('select*fromuserswhereid=?',array('value'));db::insert('insertintofoosetbar=2');db::update('updatefoosetbar=2');db::delete('deletefrombar');//返回voiddb::statement('updatefoosetbar=2');//在声明语句中加入原始的表达式db::table('name')->select(db::raw('count(*)ascount,column2'))->get();inserts/updates/deletes/unions/pessimisticlocking//插入db::table('users')->insert(['email'=>'john@example.com','votes'=>0]);$id=db::table('users')->insertgetid(['email'=>'john@example.com','votes'=>0]);db::table('users')->insert([['email'=>'taylor@example.com','votes'=>0],['email'=>'dayle@example.com','votes'=>0]]);//更新db::table('users')->where('id',1)->update(['votes'=>1]);db::table('users')->increment('votes');db::table('users')->increment('votes',5);db::table('users')->decrement('votes');db::table('users')->decrement('votes',5);db::table('users')->increment('votes',1,['name'=>'john']);//删除db::table('users')->where('votes','<',100)->delete();db::table('users')->delete();db::table('users')->truncate();//集合//unionall()方法也是可供使用的,调用方式与union相似$first=db::table('users')->wherenull('first_name');$users=db::table('users')->wherenull('last_name')->union($first)->get();//消极锁db::table('users')->where('votes','>',100)->sharedlock()->get();db::table('users')->where('votes','>',100)->lockforupdate()->get();

route

路由的基础用法

route::get('foo',function(){});route::get('foo','controllername@function');route::controller('foo','foocontroller');资源路由route::resource('posts','postscontroller');//资源路由器只允许指定动作通过route::resource('photo','photocontroller',['only'=>['index','show']]);route::resource('photo','photocontroller',['except'=>['update','destroy']]);触发错误app::abort(404);$handler->missing(...)inerrorserviceprovider::boot();thrownewnotfoundhttpexception;路由参数route::get('foo/{bar}',function($bar){});route::get('foo/{bar?}',function($bar='bar'){});http请求方式route::any('foo',function(){});route::post('foo',function(){});route::put('foo',function(){});route::patch('foo',function(){});route::delete('foo',function(){});//restful资源控制器route::resource('foo','foocontroller');//为一个路由注册多种请求方式route::match(['get','post'],'/',function(){});安全路由(tbd)route::get('foo',array('https',function(){}));路由约束route::get('foo/{bar}',function($bar){})->where('bar','[0-9]+');route::get('foo/{bar}/{baz}',function($bar,$baz){})->where(array('bar'=>'[0-9]+','baz'=>'[a-za-z]'))//设置一个可跨路由使用的模式route::pattern('bar','[0-9]+')http中间件//为路由指定middlewareroute::get('admin/profile',['middleware'=>'auth',function(){}]);route::get('admin/profile',function(){})->middleware('auth');命名路由route::currentroutename();route::get('foo/bar',array('as'=>'foobar',function(){}));route::get('user/profile',['as'=>'profile','uses'=>'usercontroller@showprofile']);route::get('user/profile','usercontroller@showprofile')->name('profile');$url=route('profile');$redirect=redirect()->route('profile');路由前缀route::group(['prefix'=>'admin'],function(){route::get('users',function(){return'matchesthe"/admin/users"url';});});路由命名空间//此路由组将会传送'foobar'命名空间route::group(array('namespace'=>'foobar'),function(){})子域名路由//{sub}将在闭包中被忽略route::group(array('domain'=>'{sub}.example.com'),function(){});

model基础使用

//定义一个eloquent模型classuserextendsmodel{}//生成一个eloquent模型phpartisanmake:modeluser//指定一个自定义的数据表名称classuserextendsmodel{protected$table='my_users';}moremodel::create(array('key'=>'value'));//通过属性找到第一条相匹配的数据或创造一条新数据model::firstorcreate(array('key'=>'value'));//通过属性找到第一条相匹配的数据或实例化一条新数据model::firstornew(array('key'=>'value'));//通过属性找到相匹配的数据并更新,如果不存在即创建model::updateorcreate(array('search_key'=>'search_value'),array('key'=>'value'));//使用属性的数组来填充一个模型,用的时候要小心「massassignment」安全问题!model::fill($attributes);model::destroy(1);model::all();model::find(1);//使用双主键进行查找model::find(array('first','last'));//查找失败时抛出异常model::findorfail(1);//使用双主键进行查找,失败时抛出异常model::findorfail(array('first','last'));model::where('foo','=','bar')->get();model::where('foo','=','bar')->first();model::where('foo','=','bar')->exists();//动态属性查找model::wherefoo('bar')->first();//查找失败时抛出异常model::where('foo','=','bar')->firstorfail();model::where('foo','=','bar')->count();model::where('foo','=','bar')->delete();//输出原始的查询语句model::where('foo','=','bar')->tosql();model::whereraw('foo=barandcars=2',array(20))->get();model::on('connection-name')->find(1);model::with('relation')->get();model::all()->take(10);model::all()->skip(10);//默认的eloquent排序是上升排序model::all()->orderby('column');model::all()->orderby('column','desc');软删除model::withtrashed()->where('cars',2)->get();//在查询结果中包括带被软删除的模型model::withtrashed()->where('cars',2)->restore();model::where('cars',2)->forcedelete();//查找只带有软删除的模型model::onlytrashed()->where('cars',2)->get();模型关联//一对一-user::phone()return$this->hasone('appphone','foreign_key','local_key');//一对一-phone::user(),定义相对的关联return$this->belongsto('appuser','foreign_key','other_key');//一对多-post::comments()return$this->hasmany('appcomment','foreign_key','local_key');//一对多-comment::post()return$this->belongsto('apppost','foreign_key','other_key');//多对多-user::roles();return$this->belongstomany('approle','user_roles','user_id','role_id');//多对多-role::users();return$this->belongstomany('appuser');//多对多-retrievingintermediatetablecolumns$role->pivot->created_at;//多对多-中介表字段return$this->belongstomany('approle')->withpivot('column1','column2');//多对多-自动维护created_at和updated_at时间戳return$this->belongstomany('approle')->withtimestamps();//远层一对多-country::posts(),一个country模型可能通过中介的users//模型关联到多个posts模型(user::country_id)return$this->hasmanythrough('apppost','appuser','country_id','user_id');//多态关联-photo::imageable()return$this->morphto();//多态关联-staff::photos()return$this->morphmany('appphoto','imageable');//多态关联-product::photos()return$this->morphmany('appphoto','imageable');//多态关联-在appserviceprovider中注册你的「多态对照表」relation::morphmap(['post'=>apppost::class,'comment'=>appcomment::class,]);//多态多对多关联-涉及数据库表:posts,videos,tags,taggables//post::tags()return$this->morphtomany('apptag','taggable');//video::tags()return$this->morphtomany('apptag','taggable');//tag::posts()return$this->morphedbymany('apppost','taggable');//tag::videos()return$this->morphedbymany('appvideo','taggable');//查找关联$user->posts()->where('active',1)->get();//获取所有至少有一篇评论的文章...$posts=apppost::has('comments')->get();//获取所有至少有三篇评论的文章...$posts=post::has('comments','>=',3)->get();//获取所有至少有一篇评论被评分的文章...$posts=post::has('comments.votes')->get();//获取所有至少有一篇评论相似于foo%的文章$posts=post::wherehas('comments',function($query){$query->where('content','like','foo%');})->get();//预加载$books=appbook::with('author')->get();$books=appbook::with('author','publisher')->get();$books=appbook::with('author.contacts')->get();//延迟预加载$books->load('author','publisher');//写入关联模型$comment=newappcomment(['message'=>'anewcomment.']);$post->comments()->save($comment);//save与多对多关联$post->comments()->savemany([newappcomment(['message'=>'anewcomment.']),newappcomment(['message'=>'anothercomment.']),]);$post->comments()->create(['message'=>'anewcomment.']);//更新「从属」关联$user->account()->associate($account);$user->save();$user->account()->dissociate();$user->save();//附加多对多关系$user->roles()->attach($roleid);$user->roles()->attach($roleid,['expires'=>$expires]);//从用户上移除单一身份...$user->roles()->detach($roleid);//从用户上移除所有身份...$user->roles()->detach();$user->roles()->detach([1,2,3]);$user->roles()->attach([1=>['expires'=>$expires],2,3]);//任何不在给定数组中的ids将会从中介表中被删除。$user->roles()->sync([1,2,3]);//你也可以传递中介表上该ids额外的值:$user->roles()->sync([1=>['expires'=>true],2,3]);事件model::creating(function($model){});model::created(function($model){});model::updating(function($model){});model::updated(function($model){});model::saving(function($model){});model::saved(function($model){});model::deleting(function($model){});model::deleted(function($model){});model::observe(newfooobserver);eloquent配置信息//关闭模型插入或更新操作引发的「massassignment」异常eloquent::unguard();//重新开启「massassignment」异常抛出功能eloquent::reguard();auth

用户认证

用户认证//判断当前用户是否已认证(是否已登录)auth::check();//获取当前的认证用户auth::user();//获取当前的认证用户的id(未登录情况下会报错)auth::id();//通过给定的信息来尝试对用户进行认证(成功后会自动启动会话)auth::attempt(['email'=>$email,'password'=>$password]);//通过auth::attempt()传入true值来开启'记住我'功能auth::attempt($credentials,true);//只针对一次的请求来认证用户auth::once($credentials);//登录一个指定用户到应用上auth::login(user::find(1));//登录指定用户id的用户到应用上auth::loginusingid(1);//使用户退出登录(清除会话)auth::logout();//验证用户凭证auth::validate($credentials);//attempttoauthenticateusinghttpbasicauth//使用http的基本认证方式来认证auth::basic('username');//performastatelesshttpbasicloginattempt//执行「httpbasic」登录尝试auth::oncebasic();//发送密码重置提示给用户password::remind($credentials,function($message,$user){});用户授权//定义权限gate::define('update-post','class@method');gate::define('update-post',function($user,$post){...});//传递多个参数gate::define('delete-comment',function($user,$post,$comment){});//检查权限gate::denies('update-post',$post);gate::allows('update-post',$post);gate::check('update-post',$post);//指定用户进行检查gate::foruser($user)->allows('update-post',$post);//在user模型下,使用authorizabletraituser::find(1)->can('update-post',$post);user::find(1)->cannot('update-post',$post);//拦截所有检查gate::before(function($user,$ability){});gate::after(function($user,$ability){});//blade模板语法@can('update-post',$post)@endcan//支持else表达式@can('update-post',$post)@else@endcan//生成一个新的策略phpartisanmake:policypostpolicy//`policy`帮助函数policy($post)->update($user,$post)//控制器授权$this->authorize('update',$post);//指定用户$user授权$this->authorizeforuser($user,'update',$post);

上一篇: 冷门JS技巧
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服