Actors::getAvgActors()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 43
rs 8.8571
cc 1
eloc 5
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
 * Classe qui va stocker mes requetes autoirs
10
 * de ma table movies
11
 * Hérite de ma super classe Model.
12
 */
13
class Actors extends Model
14
{
15
    /**
16
     * Décrit le nom de la table
17
     * que classe fait référence.
18
     */
19
    protected $table = 'actors';
20
21
    public function movies()
22
    {
23
        return $this->belongsToMany('\App\Http\Model\Movies');
24
    }
25
26
    /**
27
     * Get nb actors by city.
28
     *
29
     * @return mixed
30
     */
31
    public function getNbActorsByCity()
32
    {
33
        $result = DB::table('actors')
34
                    ->select(DB::raw('COUNT(id) as nb'), 'city')
35
                    ->groupBy('city')
36
                    ->get();
37
38
        return $result;
39
    }
40
41
    /**
42
     * Retourne la moyenne d'age des acteurs
43
     * 1er mode de Laravel pour construire mes requetes
44
     * Cela me permet de conserver une syntaxe pure en Mysql.
45
     */
46
    public function getAvgActors()
47
    {
48
49
        //1ere methode: Utilisation de MYSQL
50
        // marche mais n'est peu souple
51
         /*
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...
52
          * $results = DB::select('
53
            SELECT ROUND(AVG(TIMESTAMPDIFF(YEAR,dob, NOW()))) as age
54
            FROM actors
55
        ');
56
         */
57
58
        // 2nd méthode: PHP & MYSQL
59
        // Query Builder: Le constructeur de Requête en Laravel
60
        // DB::table => correspond FROM actors en MYSQL
61
        // select() => corresponds a mon SELECT en MYSQL
62
        // DB::raw() => permet d'utiliser les fonctions MYSQL
63
        // 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...
64
65
        // first() => corresponds LIMIT 1 en MYSQL
66
67
        // first() => l'equivalent de fetch()
68
        // get() => l'équivalent de fetchAll()
69
70
        // MAITRISE
71
72
          $results = DB::table('actors')
73
            ->select(DB::raw('ROUND(AVG(TIMESTAMPDIFF(YEAR,dob, NOW()))) as age'))
74
            ->first();
75
76
        /*
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...
77
          $results = Actors::select(DB::raw('ROUND(AVG(TIMESTAMPDIFF(YEAR,dob, NOW()))) as age'))
78
            ->first();
79
        */
80
81
        //3eme methode: Eloquant ORM
82
        // Model Actors
83
        // <=> SELECT AVG(dob) FROM actors
84
        //$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...
85
86
        // je retourne le resultat de ma requete executé
87
        return $results;
88
    }
89
}
90