laravel±í¸ñÔöɾ¸Ä²é
laravel ÊÇÒ»¸öÊ¢ÐÐµÄ php ¿ò¼Ü£¬ËüÌṩÁËÀû±ãµÄ¹¤¾ßºÍ¹¦Ð§À´×ÊÖú¿ª·¢Ö°Ô±¿ìËÙ¹¹½¨ web Ó¦ÓóÌÐò¡£ÆäÖÐÒ»¸ö»ù±¾¹¦Ð§ÊÇʹÓñí¸ñÔöɾ¸Ä²éÊý¾Ý£¬±¾ÎĽ«ÏÈÈÝÔõÑùÔÚ laravel ÖÐʵÏÖÕâЩ¹¦Ð§¡£
½¨ÉèÊý¾Ý¿âºÍ±í¸ñ
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÊý¾Ý¿âºÍÒ»¸öÊý¾Ý±íÀ´´æ´¢Êý¾Ý¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«½¨ÉèÒ»¸öÃûΪ¡°users¡±µÄ±í¸ñ£¬Ëü°üÀ¨ÒÔÏÂ×ֶΣº id¡¢name¡¢email ºÍ password¡£
ÎÒÃÇ¿ÉÒÔʹÓà Laravel ÖеÄǨáãÀ´½¨Éè±í¸ñ¡£ÔÚÏÂÁîÐÐÖÐÔËÐÐÒÔÏÂÏÂÁ
php artisan make:migration create_users_table --create=users
µÇ¼ºó¸´ÖÆ
ÔËÐиÃÏÂÁîºó£¬Laravel ½«ÔÚ¡°database/migrations¡±Ä¿Â¼Öн¨ÉèÒ»¸öеÄǨáãÎļþ¡£ÎÒÃÇ¿ÉÒÔÔÚǨáãÎļþÖÐʹÓá°Schema¡±ÀàÀ´½ç˵±í¸ñ½á¹¹¡£Æä´úÂëÈçÏ£º
use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽨ÉèÁËÒ»¸öÃûΪ¡°users¡±µÄ±í¸ñ£¬ÆäÖаüÀ¨ id¡¢name¡¢email¡¢password ºÍ timestamps ×ֶΡ£
µ±ÎÒÃÇÔËÐÐǨáãʱ£¬Laravel ½«ÔÚÊý¾Ý¿âÖн¨Éè±í¸ñ¡£ÔËÐÐÒÔÏÂÏÂÁî¾ÙÐÐǨá㣺
php artisan migrate
µÇ¼ºó¸´ÖÆ
ÔöÌí¼Í¼
ÒªÏò±í¸ñÖÐÌí¼Ó¼Í¼£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸ö¿ØÖÆÆ÷²¢Ê¹ÓÃÄ£×ÓÀ´½«Êý¾ÝÉúÑĵ½Êý¾Ý¿âÖС£ÔÚÏÂÁîÐÐÖÐÔËÐÐÒÔÏÂÏÂÁ
php artisan make:controller UserController
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁÔÚ¡°app/Http/Controllers¡±Ä¿Â¼Öн¨ÉèÒ»¸öеġ°UserController¡±¿ØÖÆÆ÷¡£
ÔÚ¿ØÖÆÆ÷ÎļþÖУ¬ÎÒÃÇ¿ÉÒÔʹÓá°create¡±ÒªÁìÀ´ÉúÑÄмͼ¡£ÒÔÏÂÊÇʾÀý´úÂ룺
use AppUser; use IlluminateHttpRequest; class UserController extends Controller { public function store(Request $request) { $user = User::create([ 'name' => $request->input('name'), 'email' => $request->input('email'), 'password' => bcrypt($request->input('password')), ]); return response()->json([ 'message' => 'User created successfully', 'user' => $user, ]); } }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÊ×Ïȵ¼Èë¡°User¡±Ä£×Ó£¬È»ºóÔÚ¡°store¡±ÒªÁìÖÐʹÓá°create¡±ÒªÁìÀ´½¨Éèмͼ¡£ÎÒÃǽ«ÇëÇóÖеġ°name¡±¡¢¡°email¡±ºÍ¡°password¡±×Ö¶ÎÓÃ×÷²ÎÊý£¬²¢Ê¹Óá°bcrypt¡±º¯Êý½«ÃÜÂë¼ÓÃÜ¡£×îºó£¬ÎÒÃÇ·µ»ØÒ»¸ö JSON ÏìÓ¦£¬ÆäÖаüÀ¨½¨ÉèµÄÓû§¼Í¼¡£
Áгö¼Í¼
ÎÒÃÇ¿ÉÒÔʹÓÿØÖÆÆ÷À´»ñÈ¡±í¸ñÖеÄËùÓмͼ²¢½«Æä·µ»Ø¸øÓû§¡£ÔÚ¿ØÖÆÆ÷ÖÐÌí¼ÓÒÔÏ´úÂ룺
public function index() { $users = User::all(); return view('users.index', ['users' => $users]); }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëͨ¹ýʹÓá°all¡±ÒªÁì»ñÈ¡Óû§±í¸ñÖеÄËùÓмͼ£¬²¢½«Ð§¹ûת´ï¸øÊÓͼ¡£ÎÒÃÇ¿ÉÒÔÔÚÊÓͼÖÐʹÓÃÕâЩÊý¾ÝÀ´äÖȾ HTML ±í¸ñ¡£
¸üмͼ
Ó뽨Éè¼Í¼ʱÀàËÆ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÄ£×ӺͿØÖÆÆ÷À´¸üмͼ¡£ÒÔÏÂÊÇÒ»¸öʾÀý¡°update¡±ÒªÁ죺
public function update(Request $request, $id) { $user = User::find($id); $user->name = $request->input('name'); $user->email = $request->input('email'); if ($request->input('password')) { $user->password = bcrypt($request->input('password')); } $user->save(); return response()->json([ 'message' => 'User updated successfully', 'user' => $user, ]); }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÊ×ÏÈʹÓá°find¡±ÒªÁì»ñÈ¡Ö¸¶¨ ID µÄÓû§¼Í¼¡£È»ºó£¬ÎÒÃǽ«ÇëÇóÖеġ°name¡±ºÍ¡°email¡±×Ö¶ÎÓÃ×÷ÊôÐÔÖµÀ´¸üмͼ¡£ÈôÊÇÇëÇó°üÀ¨¡°password¡±×ֶΣ¬ÔòʹÓá°bcrypt¡±º¯Êý½«Æä¼ÓÃܲ¢¸üÐÂÓû§¼Í¼¡£×îºó£¬ÎÒÃÇʹÓá°save¡±ÒªÁìÉúÑļͼ£¬²¢·µ»Ø¸üкóµÄÓû§¼Í¼×÷Ϊ JSON ÏìÓ¦¡£
ɾ³ý¼Í¼
ÎÒÃÇ¿ÉÒÔʹÓÿØÖÆÆ÷ºÍÄ£×ÓÀ´É¾³ýÓû§¼Í¼¡£ÒÔÏÂÊÇÒ»¸öʾÀýµÄ¡°destroy¡±ÒªÁ죺
public function destroy($id) { $user = User::find($id); $user->delete(); return response()->json([ 'message' => 'User deleted successfully', ]); }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÊ×ÏÈʹÓá°find¡±ÒªÁì»ñÈ¡Ö¸¶¨ ID µÄÓû§¼Í¼¡£È»ºó£¬ÔÚʹÓá°delete¡±ÒªÁìɾ³ý¼Í¼֮ǰ£¬ÎÒÃÇ¿ÉÒԶԸüͼִÐÐÆäËû²Ù×÷¡£×îºó£¬ÎÒÃÇ·µ»ØÒ»¸ö JSON ÏìÓ¦£¬ÆäÖаüÀ¨ÓйØÒÑɾ³ýÓû§µÄÐÅÏ¢¡£
±í¸ñÊÓͼ
¹ØÓÚÁгöËùÓÐÓû§¼Í¼µÄÇéÐΣ¬ÎÒÃÇ¿ÉÒÔʹÓÃÊÓͼÀ´äÖȾ HTML ±í¸ñ¡£ÒÔÏÂÊÇÒ»¸öʾÀýÊÓͼ´úÂ룺
<table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Action</th> </tr> </thead> <tbody> @foreach ($users as $user) <tr> <td>{{ $user->id }}</td> <td>{{ $user->name }}</td> <td>{{ $user->email }}</td> <td> <a href="{{ route('users.edit', $user->id) }}">Edit</a> <form action="{{ route('users.destroy', $user->id) }}" method="POST"> {{ csrf_field() }} {{ method_field('DELETE') }} <button type="submit">Delete</button> </form> </td> </tr> @endforeach </tbody> </table>
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇʹÓá°@foreach¡±Ö¸ÁîÀ´Ñ»·±éÀúËùÓÐÓû§¼Í¼£¬²¢ÔÚ HTML ±í¸ñÖÐÏÔʾËüÃÇµÄ ID¡¢Ãû³ÆºÍµç×ÓÓʼþ¡£±ðµÄ£¬ÎÒÃÇÌí¼ÓÁËÁ½¸ö²Ù×÷ÁУº±à¼ºÍɾ³ý¡£¹ØÓÚɾ³ý£¬ÎÒÃÇʹÓÃÒ»¸ö±íµ¥À´Ìá½» DELETE ÇëÇ󣬲¢Ê¹Óá°csrf_field¡±Ö¸ÁîÌí¼Ó CSRF ÁîÅÆ¡£¹ØÓڱ༣¬ÎÒÃÇÔÚ·ÓÉÖнç˵µ¥¶ÀµÄÊÓͼºÍ¿ØÖÆÆ÷¡£
½áÂÛ
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓà Laravel ÖеÄÄ£×ӺͿØÖÆÆ÷À´Ö´Ðлù±¾µÄ±í¸ñÔöɾ¸Ä²é²Ù×÷¡£ËäÈ»ÔÚÏÖʵÏîÄ¿ÖпÉÄÜÉæ¼°¸ü¶àµÄ¹¦Ð§ºÍϸ½Ú£¬µ«±¾ÎÄÌṩÁËÒ»¸öÆðµãÀ´×ÊÖúÄú×îÏÈʹÓà Laravel À´´¦Àí Web Ó¦ÓóÌÐòµÄ±í¸ñ²Ù×÷¡£
ÒÔÉϾÍÊÇlaravel±í¸ñÔöɾ¸Ä²éµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡