Completed
Push — master ( 002bc5...c06b9c )
by Julien
08:45
created

Actors::getNbActorsByCity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4286
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\DB;
7
8
9
/**
10
 * Classe qui va stocker mes requetes autoirs
11
 * de ma table movies
12
 * Hérite de ma super classe Model
13
 */
14
class Actors extends Model{
15
16
    /**
17
     * Décrit le nom de la table
18
     * que classe fait référence
19
     */
20
    protected $table = 'actors';
21
22
23
24
    public function movies()
25
    {
26
        return $this->belongsToMany('\App\Http\Model\Movies');
27
    }
28
29
    /**
30
     * Get nb actors by city
31
     * @return mixed
32
     */
33
    public function getNbActorsByCity(){
34
35
        $result = DB::table('actors')
36
                    ->select(DB::raw("COUNT(id) as nb"), "city")
37
                    ->groupBy('city')
38
                    ->get();
39
40
        return $result;
41
42
43
    }
44
45
46
    /**
47
     * Retourne la moyenne d'age des acteurs
48
     * 1er mode de Laravel pour construire mes requetes
49
     * Cela me permet de conserver une syntaxe pure en Mysql
50
     */
51
    public function getAvgActors(){
52
53
        //1ere methode: Utilisation de MYSQL
54
        // marche mais n'est peu souple
55
         /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
          * $results = DB::select('
57
            SELECT ROUND(AVG(TIMESTAMPDIFF(YEAR,dob, NOW()))) as age
58
            FROM actors
59
        ');
60
         */
61
62
63
64
        // 2nd méthode: PHP & MYSQL
65
        // Query Builder: Le constructeur de Requête en Laravel
66
        // DB::table => correspond FROM actors en MYSQL
67
        // select() => corresponds a mon SELECT en MYSQL
68
        // DB::raw() => permet d'utiliser les fonctions MYSQL
69
        // comme ROUND() AVG() NOW()...
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
71
        // first() => corresponds LIMIT 1 en MYSQL
72
73
        // first() => l'equivalent de fetch()
74
        // get() => l'équivalent de fetchAll()
75
76
        // MAITRISE
77
78
          $results = DB::table('actors')
79
            ->select(DB::raw('ROUND(AVG(TIMESTAMPDIFF(YEAR,dob, NOW()))) as age'))
80
            ->first();
81
82
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
          $results = Actors::select(DB::raw('ROUND(AVG(TIMESTAMPDIFF(YEAR,dob, NOW()))) as age'))
84
            ->first();
85
        */
86
87
        //3eme methode: Eloquant ORM
88
        // Model Actors
89
        // <=> SELECT AVG(dob) FROM actors
90
        //$results = Actors::avg('dob');
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
92
        // je retourne le resultat de ma requete executé
93
        return $results;
94
95
    }
96
97
98
99
100
101
102
103
104
105
}
106
107
108
109