Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel EloquentʵÏÖ¶à̬¹ØÁª£¿
laravel¿ª·¢£ºÔõÑùʹÓÃlaravel eloquentʵÏÖ¶à̬¹ØÁª£¿
¶à̬¹ØÁªÊÇ Laravel Eloquent µÄÒ»ÏîÖ÷Òª¹¦Ð§£¬Ëü¿ÉÒÔʹһ¸öÄ£×ӺͶà¸ö²î±ðµÄÄ£×Ó½¨Éè¹ØÁª¹Øϵ¡£ÔÚÏÖʵӦÓÃÖУ¬´¦Àí²î±ðÀàÐ͵ÄÊý¾ÝÏà¶Ô¼òÆÓÇÒ¸ßЧ£¬ÓÈÆäÔÚÊý¾Ý¿âÉè¼ÆÉϺÜÊÇÀû±ã¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùʹÓà Laravel Eloquent ʵÏÖ¶à̬¹ØÁª¡£
Ò»¡¢Ê²Ã´ÊǶà̬¹ØÁª£¿
¶à̬¹ØÁªÊÇÖ¸Ò»¸öÄ£×ӺͶà¸ö²î±ðµÄÄ£×Ó½¨Éè¹ØÁª¹Øϵ£¬¿ÉÒÔ½«ÆäÊÓΪ¶ÔͨÓÃÀà±ðµÄÒýÓá£ËüÄÜΪÐí¶àÓ¦ÓôøÀ´±ãµ±£¬È磺
ͼƬ¡¢ÒôƵºÍÊÓƵģ×Ó¶¼¿ÉÒÔÓë̸ÂÛÄ£×Ó½¨Éè¶à̬¹ØÁª£¬Ê¹Ì¸ÂÛ¿ÉÒÔÓ¦ÓÃÓÚ¶àÖÖÊý¾ÝÀàÐÍ¡£
Óû§¿ÉÒÔÓë̸ÂÛÄ£×Ó½¨Éè¶à̬¹ØÁª£¬²¢±»Ó¦ÓÃÓÚ¶àÖÖÊý¾ÝÀàÐÍ£¬ÈçÎÄÕ¡¢Í¼Æ¬¡¢ÊÓƵµÈ¡£
¶©µ¥Ä£×Ó¿ÉÒÔÓëÊÕ»õµØµãÄ£×Ó½¨Éè¶à̬¹ØÁª£¬Ê¹¶©µ¥¿ÉÒÔÅäË͵½¶àÖֵصãÀàÐÍ£¬Èç¼ÒÍ¥¡¢¹«Ë¾¡¢ÍøµãµÈ¡£
¶þ¡¢ÊµÏÖ¶à̬¹ØÁªµÄÒªÁì
ÏÂÃæÈÃÎÒÃÇ¿´¿´ÔõÑùʹÓà Laravel Eloquent ʵÏÖ¶à̬¹ØÁª¡£
Ê×ÏÈ£¬ÎÒÃÇÐèҪ˼Á¿µÄÊÇÊý¾Ý±íµÄÉè¼Æ¡£ÎÒÃÇÐèÒª½¨ÉèÒ»ÆäÖÐÐÄ±í£¬ÓÃÓڴ洢ģ×ÓÖ®¼äµÄ¶à̬¹ØÁª¹Øϵ¡£´Ë±íÓ¦°üÀ¨ÒÔÏÂÁУº
id: ±íÖ÷¼ü ID£»
target_type: Ä¿µÄÄ£×ÓµÄÀàÐÍÃû³Æ£»
target_id: Ä¿µÄÄ£× ID£»
source_type: Ô´Ä£×ÓµÄÀàÐÍÃû³Æ£»
source_id: Ô´Ä£× ID¡£
ÏÂÃæÊÇÊý¾Ý¿âǨáãÎļþʾÀý£º
<?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateCommentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('comments', function (Blueprint $table) { $table->id(); $table->morphs('commentable'); $table->text('content'); $table->timestamps(); }); Schema::create('votes', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('user_id'); $table->unsignedBigInteger('voteable_id'); $table->string('voteable_type'); $table->enum('type', ['up', 'down']); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('comments'); Schema::dropIfExists('votes'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏǨáãÎļþÖУ¬ÎÒÃǽ¨ÉèÁËÁ½¸öÐÂµÄ±í£ºcommentsºÍvotes¡£
comments ±í°üÀ¨Ì¸ÂÛÄ£×ӵĻù±¾ÐÅÏ¢£¬ÁíÍâʹÓÃmorphs()ÒªÁìÀ´ÊµÏÖÁ˶à̬¹ØÁªµÄÖ¸Ïò¡£votes ±íÒ²ÀàËÆ£¬Ê¹ÓÃvoteable_idºÍvoteable_type×Ö¶ÎÀ´ÊµÏÖ¶à̬¹ØÁª¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÔÚ Eloquent Ä£×ÓÖнç˵¹ØÁª¹Øϵ¡£
Comment Ä£×Ó£º
<?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel; class Comment extends Model { use HasFactory; public function commentable() { return $this->morphTo(); } public function votes() { return $this->morphMany(Vote::class, 'voteable'); } }
µÇ¼ºó¸´ÖÆ
Vote Ä£×Ó£º
<?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel; class Vote extends Model { use HasFactory; public function voteable() { return $this->morphTo(); } public function user() { return $this->belongsTo(User::class); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫Ϊ Comment Ä£×ÓºÍ Vote Ä£×Ó»®·Ö½ç˵¶à̬¹ØÁª¹Øϵ¡£ÔÚ Comment Ä£×ÓÖУ¬ÎÒÃÇʹÓÃmorphTo()ÒªÁì½ç˵ÁËÖ¸ÏòµÄ¶à̬¹ØÁª¹Øϵ£¬¶øÔÚ Vote Ä£×ÓÖУ¬ÎÒÃÇʹÓÃmorphMany()ÒªÁìÀ´½ç˵Á˶Ô̸Â۵Ķà̬¹ØÁª¹Øϵ¡£
Èý¡¢Ê¹Óöà̬¹ØÁª
ÈÃÎÒÃÇ¿´¿´ÔõÑùʹÓöà̬¹ØÁª¡£
½¨Éè̸ÂÛ£º
$article = Article::find(1); $comment = $article->comments()->create([ 'content' => 'This is a comment', ]);
µÇ¼ºó¸´ÖÆ
»ñȡ̸ÂÛµÄͶƱ£º
$votes = $comment->votes;
µÇ¼ºó¸´ÖÆ
»ñÈ¡ÎÄÕµÄ̸ÂÛ£º
$comments = $article->comments;
µÇ¼ºó¸´ÖÆ
ͶƱ£º
$comment->votes()->create([ 'user_id' => 1, 'type' => 'up', ]);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëʾÀýÑÝʾÁ˶à̬¹ØÁª¹ØϵµÄ»ù±¾Ó÷¨£¬Äã¿ÉÒÔÔÚ Laravel Eloquent ÎĵµÖÐÕÒµ½¸ü¶à¹ØÓÚ´ËÌØÕ÷µÄÏêϸÐÅÏ¢¡£
×ܽá
¶à̬¹ØÁªÊÇ Laravel Eloquent µÄÖ÷ÒªÌØÕ÷Ö®Ò»£¬Ëü¿ÉÒÔʹһ¸öÄ£×ӺͶà¸ö²î±ðµÄÄ£×Ó½¨Éè¹ØÁª¹Øϵ¡£ÔÚÊý¾Ý¿âÉè¼ÆºÍÓ¦Óÿª·¢ÖкÜÊÇÓÐÓá£ÔÚʹÓà Laravel Eloquent ʵÏÖ¶à̬¹ØÁªÊ±£¬ÐèÒªÉè¼Æ¹ØÁª¹ØϵµÄÖÐÐÄ±í£¬²¢ÔÚ Eloquent Ä£×ÓÖнç˵¹ØÁª¹Øϵ¡£ÎÒÃÇ¿ÉÒÔʹÓÃmorphTo() ºÍ morphMany() ÒªÁìÀ´ÊµÏÖ¶à̬¹ØÁª¹Øϵ¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel EloquentʵÏÖ¶à̬¹ØÁª£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡