Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-通过关系中的ID获取有关用户的信息_Php_Mysql_Sql_Laravel_Eloquent - Fatal编程技术网

Php Laravel-通过关系中的ID获取有关用户的信息

Php Laravel-通过关系中的ID获取有关用户的信息,php,mysql,sql,laravel,eloquent,Php,Mysql,Sql,Laravel,Eloquent,我有三个表格:票证、票证回复和用户回复。我想得到它的答复和有关答辩人的数据机票。我的桌子看起来像这样: 用户: 门票: ID | title | user_id | active | ... 门票及回覆: ID | tickets_id | message | user_id | ... <?php class Tickets_Replies extends Eloquent { public function replies() { retur

我有三个表格:票证、票证回复和用户回复。我想得到它的答复和有关答辩人的数据机票。我的桌子看起来像这样:

用户:

门票:

ID | title | user_id | active | ...
门票及回覆:

ID | tickets_id | message | user_id | ...
<?php
class Tickets_Replies extends Eloquent {    
    public function replies()
    {
        return $this->belongsTo('Tickets');
    }
}
?>
它在雄辩的ORM中应该是什么样子?现在我已经

用户:

<?php
class User extends Eloquent {    
public function replies() {
    return $this->belongsTo("Tickets_Replies");
}
}
?>

您应该将表名更改为用户、票证和票证三倍。这是laravel的最佳实践,也是我在下面建立关系的方式:

用户:

<?php
    class User extends Eloquent {

        public function tickets() {
            return $this->hasMany('Ticket');
        }

        public function replies() {
            return $this->hasMany('TicketReply');
        }

    }
?>
<?php
    class TicketReply extends Eloquent {

        protected $table = 'ticketreplies';

        public function ticket()
        {
            return $this->belongsTo('Tickets');
        }

        public function user()
        {
            return $this->belongsTo('User');
        }

    }
?>
<?php

    class Ticket extends Eloquent {  

        public function replies() {
            return $this->hasMany('TicketReply');
        }

        public function user()
        {
            return $this->belongsTo('User');
        }

    }

?>
<?php
    Ticket::with('replies', 'replies.user')->find('id of ticket here');
?>

TicketReply:

<?php
    class User extends Eloquent {

        public function tickets() {
            return $this->hasMany('Ticket');
        }

        public function replies() {
            return $this->hasMany('TicketReply');
        }

    }
?>
<?php
    class TicketReply extends Eloquent {

        protected $table = 'ticketreplies';

        public function ticket()
        {
            return $this->belongsTo('Tickets');
        }

        public function user()
        {
            return $this->belongsTo('User');
        }

    }
?>
<?php

    class Ticket extends Eloquent {  

        public function replies() {
            return $this->hasMany('TicketReply');
        }

        public function user()
        {
            return $this->belongsTo('User');
        }

    }

?>
<?php
    Ticket::with('replies', 'replies.user')->find('id of ticket here');
?>

门票:

<?php
    class User extends Eloquent {

        public function tickets() {
            return $this->hasMany('Ticket');
        }

        public function replies() {
            return $this->hasMany('TicketReply');
        }

    }
?>
<?php
    class TicketReply extends Eloquent {

        protected $table = 'ticketreplies';

        public function ticket()
        {
            return $this->belongsTo('Tickets');
        }

        public function user()
        {
            return $this->belongsTo('User');
        }

    }
?>
<?php

    class Ticket extends Eloquent {  

        public function replies() {
            return $this->hasMany('TicketReply');
        }

        public function user()
        {
            return $this->belongsTo('User');
        }

    }

?>
<?php
    Ticket::with('replies', 'replies.user')->find('id of ticket here');
?>

如何获取数据:

<?php
    class User extends Eloquent {

        public function tickets() {
            return $this->hasMany('Ticket');
        }

        public function replies() {
            return $this->hasMany('TicketReply');
        }

    }
?>
<?php
    class TicketReply extends Eloquent {

        protected $table = 'ticketreplies';

        public function ticket()
        {
            return $this->belongsTo('Tickets');
        }

        public function user()
        {
            return $this->belongsTo('User');
        }

    }
?>
<?php

    class Ticket extends Eloquent {  

        public function replies() {
            return $this->hasMany('TicketReply');
        }

        public function user()
        {
            return $this->belongsTo('User');
        }

    }

?>
<?php
    Ticket::with('replies', 'replies.user')->find('id of ticket here');
?>


这将为您提供一个具有您要求的嵌套关系的票证模型。(:

你想要什么样的查询?@SheikhHeera我在我的问题中添加了SQL查询。我如何在视图中显示用户名?我已经为每个
$ticket->回复
$reply
,我正在
$reply->user
中查找一些内容,但我现在真的不知道是什么…{{$reply->user->username}就这么简单;)如果您有疑问,您可以随时进行var_转储。laravel dd()中有一个很好的小助手函数(die and dump的缩写)。dd($reply->user)!我知道。我只是犯了一个小错误,对于一个回复,我添加了用户id为0。无论如何,谢谢你的帮助。它起作用了!:)