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筆記