检索输入的值
获得在 Illuminate\Http\Request 实例上的用户的所有的输入
$name = Request::input('userId');
在缺少输入值的时候设定默认值 $name = Request::input('userId','1');
确定一个输入值是否出现 if (Request::has('userId')) { // }
获取所有的输入值 Request::all();
获取数组中的值 Request::input('products.0.name');
获取请求里的部分输入值
Request::only('userId', 'userName');
Request::except('bankName');
把输入值闪存到会话里
把输入值闪存到会话里,把当前的输入的值闪存(flash)到会话( session) 里
Request::flash();
把一部分输入值闪存到会话里
Request::flashOnly('username', 'email');
Request::flashExcept('password');
闪存输入值并且用一个重定向(redirect)到前一个页面上,可以把输入闪存链到一个重定向上
return redirect('form')->withInput();
return redirect('form')->withInput(Request::except('password'));
得到前一个请求里的闪存的输入值 $username = Request::old('username');
如果你打算用 Blade 模板显示以前的输入值,可以使用 old 这个帮手: {{ old('username') }}
Cookie
检索 Cookie 值
$value = Request::cookie('name');
把一个新 Cookie 附加给一个响应
cookie 这个帮手的作用就是去生成新的 Symfony\Component\HttpFoundation\Cookie 实例。可以使用 withCookie 方法把Cookies 附加到 Response 实例上:
$response = new Illuminate\Http\Response('Hello World');
$response->withCookie(cookie('name', 'value', $minutes));
创建永恒的 Cookie(5年)
$response->withCookie(cookie()->forever('name', 'value'));
文件
检索一个上传了的文件
$file = Request::file('photo');
确定一个文件是不是被上传了
if (Request::hasFile('photo')){ // }
file 方法返回来的对象是 Symfony\Component\HttpFoundation\File\UploadedFile 类的一个实例,这个类继承了 PHP 的 SplFileInfo 类,提供了很多方法可以跟文件交互
确定上传的文件是否有效
if (Request::file('photo')->isValid())
{
//
}
移动一个上传的文件
Request::file('photo')->move($destinationPath);
Request::file('photo')->move($destinationPath, $fileName);
请求信息
Request 类为你的应用提供了很多方法可以检查 HTTP 请求,它继承了 Symfony\Component\HttpFoundation\Request 类。下面是几个亮点。
检索请求 URI $url = Request::path();
检索请求方法
$method = Request::method();
if (Request::isMethod('post')){ }
确定请求路径是否匹配一个模式 if (Request::is('admin/*')){ }
得到当前请求的地址 $url = Request::url();
Laravel 表单验证规则
required、present 、filled 和 nullable
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
public function register(Request $request)
{
$this->validator($request->all())->validate();
...
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
验证规则
required 验证字段必须出现在输入数据中、不能为空。
present 验证字段必须出现在输入数据中、能为空。
filled 验证字段可不在输入数据中。当验证字段出现在输入数据中时,不能为空。
nullable 验证字段的值可以是 null。
Laravel 中 offset,limit 或 skip , take 的使用
laravel 本身有一个自带的快速分页方法 paginate,只需要传入每页显示多少条数据就可以 了,但是如果想使用自定义从哪里开始呢。
offset,limit (offset 设置从哪里开始,limit 设置想要查询多少条数据)
Model::offset(0)->limit(10)->get();
skip,take (sikip 跳过几条,take 取多少条数据)
Model::skip(3)->take(3)->get();