laravel store ÒªÁì
laravelÊÇÒ»¿î¿ªÔ´µÄphp¿ò¼Ü£¬ËüµÄ·ºÆð´ó´ó¼ò»¯ÁËwebÓ¦ÓóÌÐòµÄ¿ª·¢Àú³Ì¡£ÔÚlaravelÖУ¬storeÒªÁìÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄÒªÁìÖ®Ò»¡£±¾ÎĽ«ÏêϸÏÈÈÝlaravelÖеÄstoreÒªÁ죬×ÊÖú¿ª·¢Õ߸üºÃµØÃ÷È·Ó¦ÓóÌÐò¿ª·¢µÄ»ù±¾²Ù×÷¡£
Ò»¡¢Ê²Ã´ÊÇStoreÒªÁ죿
ÔÚLaravelÖУ¬StoreÒªÁìÊÇÒ»¸öÓÃÓÚ½«Êý¾Ý´æ´¢µ½Êý¾Ý¿âµÄÒªÁì¡£ËüÊÇÒ»¸öͨÓõÄÒªÁ죬¿ÉÒÔÓ¦ÓÃÓÚ½¨Éè¡¢¸üкÍɾ³ý²Ù×÷¡£ÏêϸÀ´Ëµ£¬StoreÒªÁìʵÏÖÁ˽«Êý¾Ý´æ´¢µ½Êý¾Ý¿âµÄÂß¼£¬°üÀ¨Êý¾ÝÑéÖ¤¡¢Êý¾Ý´¦ÀíºÍÊý¾ÝÉúÑĵȲÙ×÷¡£
¶þ¡¢StoreÒªÁìµÄÓ÷¨
ÔÚLaravelÖУ¬StoreÒªÁìͨ³£ÓÃÓÚ´¦ÀíÓû§Í¨¹ý±íµ¥Ìá½»µÄÊý¾Ý¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃLaravelµÄ±íµ¥ÌìÉúÆ÷ÌìÉúÒ»¸ö¾ßÓÐһЩ±ØÌî×Ö¶ÎµÄ±íµ¥£¬²¢ÔÚ±íµ¥ÌύʱŲÓÃStoreÒªÁì¾ÙÐÐÊý¾Ý´¦Àí¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý´úÂ룺
// ½ç˵һ¸ö¿ØÖÆÆ÷ class UserController extends Controller { public function store(Request $request) { // ÑéÖ¤±íµ¥Ìá½»µÄÊý¾ÝÊÇ·ñÇкϹæÔò $this->validate($request, [ 'name' => 'required|max:255', 'email' => 'required|unique:users|max:255', 'password' => 'required|max:255', ]); // ´¦Àí±íµ¥Ìá½»µÄÊý¾Ý $user = new User; $user->name = $request->name; $user->email = $request->email; $user->password = bcrypt($request->password); $user->save(); // ·µ»ØÐÂÓû§µÄÐÅÏ¢ return response()->json([ 'user' => $user, ]); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬StoreÒªÁìÎüÊÕÒ»¸öRequest¹¤¾ß×÷Ϊ²ÎÊý£¬²¢Æ¾Ö¤±íµ¥Ìá½»µÄÊý¾Ý¾ÙÐÐÑéÖ¤¡¢´¦ÀíºÍÉúÑÄ¡£ÏêϸÀ´Ëµ£¬ÎÒÃÇÏÈͨ¹ývalidateÒªÁì¶Ô±íµ¥Ìá½»µÄÊý¾Ý¾ÙÐÐÑéÖ¤£¬È·±£±íµ¥ÖеıØÌî×Ö¶ÎÒѾÌîд¡£È»ºó£¬ÎÒÃǽ¨ÉèÒ»¸öUser¹¤¾ß£¬²¢´Ó$request¹¤¾ßÖлñÈ¡¶ÔÓ¦µÄÊý¾Ý£¬×îºó½«Êý¾ÝÉúÑĵ½Êý¾Ý¿âÖС£×îºó£¬ÎÒÃǽ«Ð½¨ÉèµÄÓû§ÐÅÏ¢×÷ΪJSONÃûÌ÷µ»Ø¸ø¿Í»§¶Ë¡£
Èý¡¢ÆäËûStoreÒªÁìµÄʹÓ÷½·¨
³ýÁËÓû§±íµ¥µÄ´æ´¢²Ù×÷£¬StoreÒªÁìÔÚLaravelÖÐÉÐÓÐÐí¶àÆäËûµÄÏÖʵӦÓá£ÏÂÃæÎÒÃÇ»®·ÖÏÈÈÝÒ»ÏÂÕâЩӦÓó¡¾°£º
ÎļþÉÏ´«
ÔÚÎļþÉÏ´«µÄÀú³ÌÖУ¬ÎÒÃÇͨ³£ÐèÒª½«ÉÏ´«ÎļþÉúÑĵ½Ð§ÀÍÆ÷µÄÓ²ÅÌÉÏ£¬È»ºó½«ÎļþµÄ·¾¶ÉúÑĵ½Êý¾Ý¿âÖС£LaravelÌṩÁËÒ»¸östoreÒªÁ죬ÓÃÓÚÉúÑÄÉÏ´«Îļþµ½Ö¸¶¨µÄ·¾¶ÖС£
// ½ç˵һ¸öÉÏ´«ÎļþµÄ¿ØÖÆÆ÷ class FileController extends Controller { public function store(Request $request) { // ´ÓÇëÇóÖлñÈ¡Îļþ¹¤¾ß $file = $request->file('file'); // ÑéÖ¤ÎļþÃûÌú;Þϸ $this->validate($request, [ 'file' => 'required|image|max:2048', ]); // ´¦ÀíÉÏ´«Îļþ $path = $file->store('uploads'); // ½«Îļþ·¾¶ÉúÑĵ½Êý¾Ý¿âÖÐ $file = new File; $file->path = $path; $file->save(); // ·µ»ØÎļþÐÅÏ¢ return response()->json([ 'file' => $file, ]); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇͨ¹ý$file->store(‘uploads’)ÒªÁ콫ÉÏ´«µÄÎļþÉúÑĵ½Ö¸¶¨µÄuploadsĿ¼ÖУ¬²¢½«Îļþ·¾¶ÉúÑĵ½Êý¾Ý¿âÖС£
±íµ¥¸üÐÂ
ÔÚ¸üÐÂ±íµ¥µÄʱ¼ä£¬ÎÒÃÇÐèҪƾ֤±íµ¥Ìá½»µÄÊý¾ÝÕÒµ½ÏìÓ¦µÄ¼Í¼£¬²¢½«¸üеÄÊý¾Ý¸üе½Êý¾Ý¿âÖС£¹ØÓÚÕâÖÖÇéÐΣ¬LaravelÌṩÁËÒ»¸öupdateÒªÁ죬ÓÃÓÚ¸üÐÂÖ¸¶¨¼Í¼µÄÊý¾Ý¡£
// ½ç˵һ¸ö¸üÐÂÓû§ÐÅÏ¢µÄ¿ØÖÆÆ÷ class UserController extends Controller { public function update(Request $request, $id) { // ´ÓÇëÇóÖлñÈ¡Óû§¹¤¾ß $user = User::find($id); // ÑéÖ¤±íµ¥Ìá½»µÄÊý¾ÝÊÇ·ñÇкϹæÔò $this->validate($request, [ 'name' => 'required|max:255', 'email' => 'required|unique:users,email,' . $user->id . '|max:255', 'password' => 'required|max:255', ]); // ´¦Àí±íµ¥Ìá½»µÄÊý¾Ý $user->name = $request->name; $user->email = $request->email; $user->password = bcrypt($request->password); $user->save(); // ·µ»ØÐÂÓû§µÄÐÅÏ¢ return response()->json([ 'user' => $user, ]); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇƾ֤´«ÈëµÄ$id²ÎÊýÕÒµ½ÏìÓ¦µÄUser¼Í¼£¬²¢Æ¾Ö¤±íµ¥Ìá½»µÄÊý¾Ý¶ÔÆä¾ÙÐиüС£
¼Í¼ɾ³ý
ÔÚɾ³ýÒ»¸ö¼Í¼µÄʱ¼ä£¬ÎÒÃÇͨ³£ÐèÒª´ÓÊý¾Ý¿âÖÐÕÒµ½ÏìÓ¦µÄ¼Í¼£¬²¢Ö´ÐÐdelete²Ù×÷¡£¹ØÓÚÕâÖÖÇéÐΣ¬LaravelÌṩÁËdestroyÒªÁ죬ÓÃÓÚͨ¹ýidɾ³ýÖ¸¶¨µÄ¼Í¼¡£
// ½ç˵һ¸öɾ³ýÓû§¼Í¼µÄ¿ØÖÆÆ÷ class UserController extends Controller { public function destroy($id) { // ´ÓÊý¾Ý¿âÖвéÕÒ¶ÔÓ¦µÄÓû§¼Í¼ $user = User::find($id); // ɾ³ýÓû§¼Í¼ $user->delete(); // ·µ»Øɾ³ýÀÖ³ÉÐÅÏ¢ return response()->json([ 'message' => 'user deleted successfully', ]); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÓÃUser::find($id)ÒªÁì»ñÈ¡µ½¶ÔÓ¦µÄ¼Í¼£¬²¢Í¨¹ýDeleteÒªÁìÖ±½Ó´ÓÊý¾Ý¿âÖÐɾ³ý¡£
ËÄ¡¢×ܽá
LaravelÖеÄStoreÒªÁìÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄÒªÁ죬Äܹ»Àû±ãµØ×ÊÖú¿ª·¢ÕßÍê³ÉÊý¾ÝÑéÖ¤¡¢´¦ÀíºÍÉúÑĵȲÙ×÷¡£Í¨¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇ¿ÉÒÔ¸üºÃµØÏàʶStoreÒªÁìÔÚLaravelÖеÄʹÓ÷½·¨ºÍÏÖʵӦÓó¡¾°¡£¿ª·¢ÕßÃÇÔÚÓ¦ÓÃLaravel¿ò¼Üʱ¿ÉÒÔƾ֤ÏÖʵÐèÒªÎÞаӦÓÃStoreÒªÁ죬²¢ÍŽávalidate¡¢updateºÍdestroyµÈÒªÁì¾ÙÐÐÊý¾Ý´¦ÀíºÍÖÎÀí¡£
ÒÔÉϾÍÊÇlaravel store ÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡