简体   繁体   中英

How often is the value in the database in Laravel Equolent

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()?

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();

在控制器文件中 Data::count();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM