首页 > 代码库 > laravel5.4笔记
laravel5.4笔记
laravel获取输入数据
获取已知key的数据
public function store(Request $request)
{
$name = $request->input(‘name‘);
$request->path(); //获取请求路径
if ($request->is(‘admin/*‘)); //判断是否符合目标路由
$url = $request->url(); //获取请求url
$url = $request->fullUrl();//获取请求url,包含查询字符串
$method = $request->method(); //请求方法
if ($request->isMethod(‘post‘));//判断请求方法
$input = $request->all();//以数组方式获取输入全部数据
}
处理session
$request->session()->put(‘aa‘,‘bb‘); //添加
var_dump($request->session()->get(‘aa‘)); //获取
$request->session()->forget(‘aa‘); //删除
$request->session()->flush(); //删除全部
参考地址:http://laravelacademy.org/post/6763.html
Laravel模型操作
引用模型 use Illuminate\Database\Eloquent\Model;
设置表 $table = ‘tbname’;
设置主键 $primaryKey = ‘key’;
设置非数字主键 $incrementing = true;
取消维护数据记录(时间戳) $timestamps = false;
获取全部数据
$flights = $this->all();
$this::where([])->get();
分页
$this::where([])->orderBy(‘id‘,‘asc‘)->paginate(15);
增加一定的限制
$flights = App\Flight::where(‘active‘, 1)
->orderBy(‘name‘, ‘desc‘)
->take(10)
->get();
获取单个数据
$flight = App\Flight::find(1); //使用主键查找
$flight = App\Flight::where(‘active‘, 1)->first();
插入一条数据
$this->fill($data);
$this->save();
$flight = new Flight;
$flight->name = $request->name;
$flight->save();
更新数据
$flight = App\Flight::find(1);
$flight->name = ‘New Flight Name‘;
$flight->save();
$sk = $this::find($id);
$sk->update($data);
删除数据
$sk = $this::find($id);
$sk->delete();
laravel5.4笔记