Skip to content

Commit 9f00866

Browse files
committed
Eloquent Relationships
1 parent 288eaba commit 9f00866

File tree

7 files changed

+80
-29
lines changed

7 files changed

+80
-29
lines changed

app/Entities/Entity.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace TeachMe\Entities;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Entity extends Model {
8+
9+
public static function getClass()
10+
{
11+
return get_class(new static);
12+
}
13+
14+
}

app/Entities/Ticket.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22

33
namespace TeachMe\Entities;
44

5-
use Illuminate\Database\Eloquent\Model;
6-
7-
class Ticket extends Model
5+
class Ticket extends Entity
86
{
97

8+
public function author()
9+
{
10+
return $this->belongsTo(User::getClass());
11+
}
12+
13+
public function comments()
14+
{
15+
return $this->hasMany(TicketComment::getClass());
16+
}
17+
18+
public function voters()
19+
{
20+
return $this->belongsToMany(User::getClass(), 'ticket_votes');
21+
}
22+
1023
public function getOpenAttribute()
1124
{
1225
return $this->status == 'open';

app/Entities/TicketComment.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
namespace TeachMe\Entities;
44

5-
use Illuminate\Database\Eloquent\Model;
6-
7-
class TicketComment extends Model
5+
class TicketComment extends Entity
86
{
9-
//
7+
8+
public function ticket()
9+
{
10+
return $this->belongsTo(Ticket::getClass());
11+
}
12+
13+
public function user()
14+
{
15+
return $this->belongsTo(User::getClass());
16+
}
17+
1018
}

app/Entities/TicketVote.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
namespace TeachMe\Entities;
44

5-
use Illuminate\Database\Eloquent\Model;
6-
7-
class TicketVote extends Model
5+
class TicketVote extends Entity
86
{
9-
//
7+
8+
public function ticket()
9+
{
10+
return $this->belongsTo(Ticket::getClass());
11+
}
12+
13+
public function user()
14+
{
15+
return $this->belongsTo(User::getClass());
16+
}
17+
1018
}

app/Entities/User.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
namespace TeachMe\Entities;
44

55
use Illuminate\Auth\Authenticatable;
6-
use Illuminate\Database\Eloquent\Model;
76
use Illuminate\Auth\Passwords\CanResetPassword;
87
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
98
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
109

11-
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
10+
class User extends Entity implements AuthenticatableContract, CanResetPasswordContract
1211
{
1312
use Authenticatable, CanResetPassword;
1413

@@ -32,4 +31,15 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
3231
* @var array
3332
*/
3433
protected $hidden = ['password', 'remember_token'];
34+
35+
public function tickets()
36+
{
37+
return $this->hasMany(Ticket::getClass());
38+
}
39+
40+
public function voted()
41+
{
42+
return $this->belongsToMany(Ticket::getClass(), 'ticket_votes');
43+
}
44+
3545
}

resources/views/tickets/details.blade.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@
1313
<span class="glyphicon glyphicon-time"></span> {{ $ticket->created_at->format('d/m/y h:ia') }}
1414
</p>
1515
<h4 class="label label-info news">
16-
9 votos </h4>
16+
{{ count($ticket->voters) }} votos
17+
</h4>
1718

1819
<p class="vote-users">
19-
<span class="label label-info">Eddie Reilly I</span>
20-
<span class="label label-info">Letha Marks</span>
21-
<span class="label label-info">Orpha Quitzon</span>
22-
<span class="label label-info">Orpha Quitzon</span>
23-
<span class="label label-info">Orpha Quitzon</span>
24-
<span class="label label-info">Prof. Robbie Russel V</span>
25-
<span class="label label-info">Juanita Senger</span>
26-
<span class="label label-info">Geo Armstrong PhD</span>
27-
<span class="label label-info">Prof. Ruthe Keebler I</span>
20+
@foreach($ticket->voters as $user)
21+
<span class="label label-info">{{ $user->name }}</span>
22+
@endforeach
2823
</p>
2924

3025
<form method="POST" action="http://teachme.dev/votar/5" accept-charset="UTF-8"><input name="_token" type="hidden" value="VBIv3EWDAIQuLRW0cGwNQ4OsDKoRhnK2fAEF6UbQ">
@@ -49,13 +44,16 @@
4944
<button type="submit" class="btn btn-primary">Enviar comentario</button>
5045
</form>
5146

52-
<h3>Comentarios (6)</h3>
47+
<h3>Comentarios ({{ count($ticket->comments) }})</h3>
5348

54-
@foreach ($comments as $comment)
49+
@foreach ($ticket->comments as $comment)
5550
<div class="well well-sm">
56-
<p><strong>{{ $comment->name }}</strong></p>
51+
<p><strong>{{ $comment->user->name }}</strong></p>
5752
<p>{{ $comment->comment }}</p>
58-
<p class="date-t"><span class="glyphicon glyphicon-time"></span> 01/04/2015 12:21am</p>
53+
<p class="date-t">
54+
<span class="glyphicon glyphicon-time"></span>
55+
{{ $comment->created_at->format('d/m/Y h:ia') }}
56+
</p>
5957
</div>
6058
@endforeach
6159
</div>

resources/views/tickets/partials/item.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
</a>
1414
--}}
1515
<a href="{{ route('tickets.details', $ticket) }}">
16-
<span class="votes-count">12 votos</span>
17-
- <span class="comments-count">0 comentarios</span>.
16+
<span class="votes-count">{{ $ticket->voters()->count() }} votos</span>
17+
- <span class="comments-count">{{ $ticket->comments()->count() }} comentarios</span>.
1818
</a>
1919
</p>
2020
<p class="date-t">

0 commit comments

Comments
 (0)