尊龙凯时人生就是搏

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ʵÏÖ¶à̬¹ØÁª£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ