Home > Net >  How often is the value in the database in Laravel Equolent
How often is the value in the database in Laravel Equolent

Time:07-10

I am beginner. I have small problem with Laravel Equolent.

I have migrations:

Schema::create('datas', function (Blueprint $table) {
            $table->id();
            $table->integer('number');
            $table->timestamps();
        });

and model:

class Data extends Model
{
    protected $guarded = ['id'];

    protected $fillable = [
        'number'
    ];

    protected $dates = [
        'created_at',
        'updated_at',
        'date'
    ];

    protected $casts = [
        'number'=>'integer',
    ];
}

Normalny I make this code:

Data::get();

and this is return me all record from DB.

I need information how often does the number appear in the database.

For example:

Data->where('number', 1)->get();
Data->where('number', 15)->get();
Data->where('number', 55)->get();

etc

how wany can I count this result? ->count()?

CodePudding user response:

If you need to count all the records in your Data model, you can use this code:

$count = Data::count();

But if you want to count specific number, try this:

$count = Data::where('number', $number)->count();

CodePudding user response:

in the controller file Data::count();

  • Related