×ðÁú¿­Ê±¹ÙÍøµÇ¼

laravel ÔõôÁ¬±íÅÌÎÊ

laravel ÊÇÒ»¸öÊ¢ÐÐµÄ php ¿ò¼Ü£¬ËüÌṩÁËÒ»ÖÖÓÅÑŵķ½·¨À´¹¹½¨ web Ó¦ÓóÌÐòºÍ api¡£ÔÚ¹¹½¨Ó¦ÓóÌÐòµÄÀú³ÌÖУ¬¾­³£»áÐèÒª¾ÙÐбíÖ®¼äµÄ¹ØÁªÅÌÎÊ£¬ÒÔ±ãÓÚ»ñÈ¡¸ü¶àµÄÊý¾ÝÐÅÏ¢¡£±¾ÎĽ«ÖصãÏÈÈÝÔõÑùʹÓà laravel ¾ÙÐÐÁ¬±íÅÌÎÊ¡£

»ù´¡Ä£×ÓÀà

ÔÚ Laravel ÖУ¬Ã¿¸ö¹Øϵ¶¼ÊÇͨ¹ýÏà¹ØÄ£×ÓÖ®¼äµÄÒªÁ콨ÉèµÄ¡£ÎÒÃÇÐèÒªÔÚÄ£×ÓÀàÖнç˵¹ØϵҪÁì¡£ÏÂÃæµÄÀý×ÓչʾÁËÔõÑùÔÚÄ£×ÓÀàÖнç˵ belongsTo ºÍ hasMany ¹ØϵҪÁì¡£

class User extends Model
{
    /**
     * Get the post that belongs to the user.
     */
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚ User Ä£×ÓÖУ¬belongsTo ÒªÁìÌåÏÖ User Ä£×ÓÓµÓÐÒ»¸ö Post Ä£×Ó£¬¶øÔÚ Post Ä£×ÓÖУ¬hasMany ÒªÁìÌåÏÖ Post Ä£×ÓÓжà¸ö Comment Ä£×Ó¡£

¹ØϵÅÌÎÊ

ÔÚ Laravel ÖУ¬ÅÌÎʹ¹½¨Æ÷ÌṩÁËһЩҪÁìÀ´¾ÙÐйØÁªÅÌÎÊ¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓà with ÒªÁì»ñÈ¡¹ØÁªÄ£×ÓµÄÊý¾Ý¡£

$users = User::with('post')->get();

µÇ¼ºó¸´ÖÆ

Õâ¸ö´úÂ뽫»ñÈ¡ËùÓÐ User Ä£×Ó£¬²¢Ê¹Óà with ÒªÁìÔ¤¼ÓÔØÏà¹ØµÄ Post Ä£×Ó¡£ÕâÑù£¬ÎÒÃǾͿÉÒÔÔÚÓû§ºÍÌû×ÓÖ®¼ä½¨Éè¹ØϵÁË¡£

ͬÑùµØ£¬ÎÒÃÇÒ²¿ÉÒÔÔÚ post ºÍ comment Ö®¼ä¾ÙÐйØϵÅÌÎÊ¡£

$posts = Post::with('comments')->get();

µÇ¼ºó¸´ÖÆ

Õâ¸ö´úÂ뽫»ñÈ¡ËùÓÐ Post Ä£×Ó£¬²¢Ê¹Óà with ÒªÁìÔ¤¼ÓÔØÏà¹ØµÄ Comment Ä£×Ó¡£

ÈôÊÇÐèÒª½øÒ»²½¹ýÂËÅÌÎÊЧ¹û£¬ÎÒÃÇ¿ÉÒÔÔÚÒªÁìÖд«Èë±Õ°üº¯Êý¡£ÈçÏÂÃæµÄÀý×ÓչʾÁËÔõÑù»ñÈ¡ËùÓÐÒÑÐû²¼µÄ̸ÂÛ¡£

$comments = Comment::with(['post' => function ($query) {
    $query->where('published', true);
}])->get();

µÇ¼ºó¸´ÖÆ

Õâ¸ö´úÂ뽫»ñÈ¡ËùÓÐ Comment Ä£×Ó£¬²¢Ê¹Óà with ÒªÁìÔ¤¼ÓÔØÏà¹ØµÄ Post Ä£×Ó¡£ÔÚ with ÒªÁìÖУ¬ÎÒÃÇÒ²¿ÉÒÔת´ïÒ»¸ö¹ØÁªÊý×é¡£´Ëʱ£¬Êý×éµÄ¼üÌåÏÖ¹ØϵÃû³Æ£¬¶øÊý×éµÄÖµÌåÏÖÄ¿½ñ¹ØϵµÄÅÌÎʱհüº¯Êý¡£

×Ô½ç˵¹ØϵÅÌÎÊ

ÔÚһЩÇéÐÎÏ£¬ÎÒÃÇ¿ÉÄÜÐèÒª¾ÙÐÐһЩ×Ô½ç˵ÅÌÎÊ¡£ÀýÈ磬ÎÒÃÇÐèҪƾ֤Óû§µÄ½ÇÉ«¾ÙÐÐÅÌÎÊ¡£´Ëʱ£¬ÎÒÃÇ¿ÉÒÔÔÚÄ£×ÓÀàÖнç˵һ¸ö¹ØϵҪÁì¡£

class User extends Model
{
    /**
     * Get the posts for the user by role.
     */
    public function postsByRole($role)
    {
        return $this->hasManyThrough(
            'App\Post', 
            'App\Category', 
            'user_id', 
            'category_id'
        )->where('role', '=', $role);
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÔÚ User Ä£×ÓÖнç˵ÁËÒ»¸ö postsByRole ÒªÁì¡£¸ÃÒªÁìʹÓà hasManyThrough ÒªÁ콨Éè Posts Ä£×ÓºÍ Categories Ä£×ÓÖ®¼äµÄ¹Øϵ¡£ÆäÖУ¬µÚÒ»¸ö²ÎÊýÌåÏÖ Posts Ä£×Ó£¬µÚ¶þ¸ö²ÎÊýÌåÏÖ Categories Ä£×Ó£¬µÚÈý¸ö²ÎÊýÌåÏÖ¿ÉÒÔ´ÓÖлñÈ¡ Posts Ä£× User Ä£×ÓµÄÍâ¼üÃû£¬µÚËĸö²ÎÊýÌåÏÖ¿ÉÒÔ´ÓÖлñÈ¡ Categories Ä£× Posts Ä£×ÓµÄÍâ¼üÃû¡£

¶à¶Ô¶à¹Øϵ

ÔÚ Laravel ÖУ¬¶à¶Ô¶à¹ØϵÊÇͨ¹ýÖÐÑë±í½¨ÉèµÄ¡£ÔÚÄ£×ÓÀàÖУ¬ÎÒÃÇÐèÒª½ç˵ belongsToMany ¹ØϵҪÁìÀ´½¨Éè¶à¶Ô¶à¹Øϵ¡£ÏÂÃæµÄÀý×ÓչʾÁËÔõÑùÔÚ User Ä£×ÓºÍ Role Ä£×ÓÖ®¼ä½¨Éè¶à¶Ô¶à¹Øϵ¡£

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}

class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚ User Ä£×ÓÖУ¬belongsToMany ÒªÁìÌåÏÖ User Ä£×ÓºÍ Role Ä£×ÓÖ®¼ä½¨ÉèÁ˶à¶Ô¶à¹Øϵ¡£Í¬ÑùµØ£¬ÔÚ Role Ä£×ÓÖУ¬belongsToMany ÒªÁìÌåÏÖ Role Ä£×ÓºÍ User Ä£×ÓÖ®¼ä½¨ÉèÁ˶à¶Ô¶à¹Øϵ¡£

¹ØÓÚ¶à¶Ô¶à¹ØϵµÄÅÌÎÊ£¬Laravel ÌṩÁËһЩҪÁìÀ´ÊµÏÖ£¬ÀýÈ磺withCount¡¢has¡¢whereHas µÈ¡£

½áÂÛ

±¾ÎÄÖصãÏÈÈÝÁËÔÚ Laravel ÖÐÔõÑù¾ÙÐбíÖ®¼äµÄ¹ØÁªÅÌÎÊ£¬°üÀ¨»ù´¡Ä£×ÓÀà¡¢¹ØϵÅÌÎÊ¡¢×Ô½ç˵¹ØϵÅÌÎÊÒÔ¼°¶à¶Ô¶à¹ØϵÅÌÎÊ¡£Ï£Íûͨ¹ý±¾ÎĵÄѧϰ£¬¶ÁÕß¿ÉÒÔÕÆÎÕ Laravel Á¬±íÅÌÎʵĻù´¡ÖªÊ¶£¬²¢Äܹ»ÔÚÏÖʵÏîÄ¿ÖÐÎÞаӦÓá£

ÒÔÉϾÍÊÇlaravel ÔõôÁ¬±íÅÌÎʵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

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

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

QR code
ÍøÕ¾µØͼ