Laravel模型和关系票与反馈和用户

时间:2015-02-27 13:23:18

标签: php laravel-4 eloquent

我正试图通过创建一个票务系统来掌握Eloquent ORM的概念。我想要实现的是:

  • 发布故障单的用户的门票
  • 属于故障单的反馈和输入的用户 反馈

这就是我现在所拥有的:

// TicketController.php
    public function index()
    {
        $tickets = Ticket::with('feedback')->with('user')->orderBy("created_at", "desc")->get();

        //dd($tickets);

        return View::make('modules.helpdesk.index')->withTickets($tickets);
    }

以下型号

// Ticket.php
class Ticket extends Eloquent {

    protected $table = 'helpdesk_tickets';

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

// Feedback.php
class Feedback extends Eloquent {

    protected $table = 'helpdesk_tickets_feedback';

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

// User.php
class User extends Eloquent {

    protected $table = 'users';

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

我现在拥有的是门票,相关反馈以及创建门票的用户。我现在要做的是获得创建反馈的用户。

3 个答案:

答案 0 :(得分:2)

您需要修复关系:

// User model
public function tickets()
{
  return $this->hasMany('Ticket'); // adjust namespace if needed
}

接下来添加关系:

// Feedback model
public function user()
{
  return $this->belongsTo('User'); // namespace like above
}

然后使用急切加载:

// it will execute 4 queries:
// 1st for tickets
// 2nd for feedback
// 3rd for feedbacks' user
// 4th for tickets' user
$tickets = Ticket::with('feedback.user', 'user')->latest()->get();
然后,您可以在循环中访问关系,如下所示:

@foreach ($tickets as $ticket)
  {{ $ticket->title }} by {{ $ticket->user->name }}
  @foreach ($ticket->feedback as $feedback)
    {{ $feedback->content }}
  @endforeach
@endforeach

答案 1 :(得分:0)

你想要做的是创建嵌套关系,就像Ticket在反馈时添加一个belgonsTo关系一样

当您想要使用它时,您可以使用点符号feedback.user

来链接关系

代码

// Feedback.php
class Feedback extends Eloquent {

    protected $table = 'helpdesk_tickets_feedback';

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

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


 // TicketController.php
public function index()
{
    $tickets = Ticket::with('feedback')->with('user')->with('feedback.user')->orderBy("created_at", "desc")->get();

    //dd($tickets);

    return View::make('modules.helpdesk.index')->withTickets($tickets);
}

答案 2 :(得分:0)

编辑:

即使这样可行,它也会执行比需要更多的查询。见Jareks的答案。

原始答案:

首先,你需要让你的关系变得紧张,在User.php中你应该调用与HasMany的用户关系。

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

modules.helpdesk.index中,您现在应该拥有一个故障单集合,因为您将$ticket变量附加到视图中。

如果你使用foreach循环遍历这个集合,那么你应该得到的是每个循环的模型:

foreach($tickets as $ticket) {
  // Prints the name property of the Ticket model
  print $ticket->name;

  // Since a ticket only belongs to ONE user then that means that you are trying to fetch a model
  // What we're doing here is getting the User model via the relationship you made in the model Ticket.php and then getting the name.
  print $ticket->user()->first()->username;

  // Since a ticket can have MANY feedbacks that means were fetching a collection 
  // which needs to be broken down to models so we do that looping the collection.
  // Here we are doing the same thing as with the User model except with a collection.
  foreach($ticket->feedback()->get() as $feedback) {
    $feedback->text;
  }
}

您一定要查看Laravel API并查看其中的Collection和Model。 http://laravel.com/api/当你遇到困难时,你会得到很多帮助,请相信我:)

我希望这能回答你的问题。