Justin's Words


  • Home

  • About

  • Archives
Justin's Words

Laravel 常见需求解决

Posted on 2014-12-09 | In PHP |

搜索

SQL:

1
SELECT * FROM  users WHERE (username LIKE '%search%') OR (profession LIKE '%search%') OR (skilled_subject LIKE '%search%') ORDER BY username ASC;

对应的 Eloquent:

1
2
3
4
5
6
7
8
9
10
public function search() {
$search = htmlspecialchars(trim(Input::get('search')));
$users = User::where(function ($query) use ($search) {
$query->where('username', 'like', '%'.$search.'%');
$query->orWhere('profession', 'like', '%'.$search.'%');
$query->orWhere('skilled_subject', 'like', '%'.$search.'%');
})->orderBy('username', 'asc')->paginate(20);

return View::make('pages.tutors')->with(array('users' => $users, 'query' => $search));
}

scotch.io 对 Eloquent 有详细讲解。

Justin's Words

Laravel Custom Validator

Posted on 2014-12-09 | In PHP |

在 laravel Validation rules 不够用时你就需要进行自定义 validation rule。

Validator::extend()

最直接的是通过 Validation::extend() 来创建自定义验证规则,你可以在 app/routes.php 或控制器来写:

1
2
3
4
Validator::extend('foo', function($attribute, $value, $parameters)
{
return $value == 'foo';
});

$attribute 为要验证的属性名称,$value 为属性的值,$parameters 为传递到规则的参数数组。

Read more »
Justin's Words

Laravel CRUD

Posted on 2014-12-07 | In PHP |

本文讲 Laravel CRUD(create, read, update, delete),学习 Laravel CRUD 期间受 scotch.io 帮助很大,跳转过去看效果可能更好。

News Table

创建迁移文件

直接通过 artisan migrate:make 创建迁移文件即可:

1
php artisan migrate:make create_news_table
Read more »
Justin's Words

Laravel 404 处理

Posted on 2014-12-07 | In PHP |

在 app/routes.php 添加函数 App::missing(callback):

1
2
3
4
5
6
7
8
9
/*
|------------------------------------------
| Route 404
|------------------------------------------
|
*/
App::missing(function($exception) {
return Response::view('error', array(), 404);
});

此时你应该在 app/views 下创建一个 error.blade.php,这个 error.blade.php 就是 404 时展示的页面。

Justin's Words

Laravel Multi-Auth 管理

Posted on 2014-12-03 | In PHP |

使用 Laravel 建站过程中我遇到了不同身份验证 (Multi-Auth) 的需求,我有一个用户身份验证管理,同时还有一个管理员身份验证管理,可惜 Laravel 本身不支持多身份验证,不过我还是找到了解决方案,那就是 ollieread/multiauth

Read more »
Justin's Words

Laravel 笔记(二)

Posted on 2014-12-03 | In PHP |

本篇讲路由 (Routes)

查看官方文档

控制路由的文件为 app/routes.php。

Read more »
Justin's Words

Laravel 笔记 (一)

Posted on 2014-12-03 | In PHP |

9 月份一个同学叫我帮他做一个家教网站,我答应下来了。前端页面不复杂,很快就完成了,因为后端语言中我对 PHP 最熟悉,于是就决定用 PHP 来开发,听说 Laravel 名声不错,于是就直接上手 Laravel。

Laravel 有中文官网,里面文档很详细,不懂可以去查文档。

Laravel 链接 css 和 _js_ 这些都以 public 文件夹为当前路径,务必注意。

本篇讲 HTML 函数 和 Blade

Read more »
Justin's Words

利用 Composer 为 Laravel 添加外部库

Posted on 2014-11-27 | In PHP |

举个例子,这次我们添加的是 Captcha for Laravel 4,一个生成验证码的库。

captcha

Read more »
Justin's Words

MySQL 新用户的创建和权限分配

Posted on 2014-11-27 | In SQL |

参考:How To Create a New User and Grant Permission in Mysql

注意,这里所有的 newuser 和 password 等都应该替换为你在本地相应的值。

Read more »
Justin's Words

在 Nginx 安装 Laravel

Posted on 2014-11-25 | In Linux |

环境: Ubuntu 14.04

安装后端必要组件

1
2
sudo apt-get update
sudo apt-get install nginx php5-fpm php5-cli php5-mcrypt git

组件简单解释:

  • nginx: Nginx HTTP 服务器,具体配置看这里:Nginx restore in Ubuntu
  • php5-fpm: PHP5 处理器主件,必要配置看这里:运维笔记(二)
  • php5-cli: PHP5 命令行支持
  • php5-mcrypt: PHP5 算法支持
  • git: Git 版本控制系统
Read more »
1…678…11
Justin Young

Justin Young

You Deserve A Better Life

107 posts
15 categories
54 tags
RSS
Github Twitter
© 2014 - 2019 Justin Young
Powered by Hexo
Theme - NexT.Mist