1. 程式人生 > >Laravel框架之ORM操作--新增資料

Laravel框架之ORM操作--新增資料

在之前的查詢篇中已經建立好了模型和控制器,在此就不多說了,如果有想了解的小夥伴可以檢視

laravel之ORM查詢

好了,廢話也不多說了.直接上程式碼:

public function apply(Request $request)
	{
		if($request->isMethod('get')){
			
			return view('Player/apply');

		}elseif($request->isMethod('post')){

			$Player = new Player;

			$data = $request->except('_token','image');//接收除_token的資料

			$img = $request->file('image');//接收圖片

			$image = self::upload($img);//圖片路徑

			$data['image'] = $image; //將圖片路徑放入要入庫的陣列中

			$Player->title = $data['title'];	$Player->telphone = $data['telphone'];

			$Player->manifesto = $data['manifesto'];	$Player->introduction = $data['introduce'];

			$Player->image = $data['image'];	$Player->created_at = time();

			$result = $Player->save();

			if($result){

				return redirect('player/info');
			}
		}
	}

以下程式碼為上傳圖片方法的程式碼:

private function upload($img)
	{
		$ext = $img->getClientOriginalExtension();

		$fileName = date('YmdHis').rand(111111,999999).'.'.$ext;

		$path = $img->move("./uploads",$fileName);//執行--圖片會進入到upload資料夾

		$paths = '/uploads/'.$fileName;

		return $paths;
	}