I have custom table with users work experiences:
Schema::create('workplaces', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->string('company')->nullable();
$table->string('position')->nullable();
$table->string('description')->nullable();
$table->smallInteger('from')->nullable();
$table->smallInteger('to')->nullable();
$table->timestamps();
});
Here example user experiences data:
----------------------------------------------------------
| user_id | company | position | description | from | to |
----------------------------------------------------------
----------------------------------------------------------
| 1 | Google | Designer | Lorem ipsum | 2018 |null|
----------------------------------------------------------
----------------------------------------------------------
| 1 | Yahoo | Designer | Lorem ipsum | 2014 |2017|
----------------------------------------------------------
----------------------------------------------------------
| 1 |Microsoft| Designer | Lorem ipsum | 2004 |2008|
----------------------------------------------------------
In this example user with id == 1 has 7 years work experience.
2018 - (2017 - 2014) - (2008 - 2004) = 2011
User last year work in 2018 and now I need to subtraction result from last working year:
2018 - 2011 = 7
Now, current user has 7 year work experience.
How I can calculate custom work experiences using laravel eloquent?