Laravel eloquent calculate work experience
up vote
2
down vote
favorite
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?
laravel eloquent laravel-5.7
add a comment |
up vote
2
down vote
favorite
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?
laravel eloquent laravel-5.7
What do you want to have in output? can you explain that please?
– Hamid Naghipour
Nov 10 at 10:26
@HamidNaghipour I need calculate user work experiences. In my example data user has 7 year work experience. How I can calculate it with eloquent?
– Andreas Hunter
Nov 10 at 10:56
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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?
laravel eloquent laravel-5.7
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?
laravel eloquent laravel-5.7
laravel eloquent laravel-5.7
edited Nov 10 at 10:54
asked Nov 10 at 9:09
Andreas Hunter
741318
741318
What do you want to have in output? can you explain that please?
– Hamid Naghipour
Nov 10 at 10:26
@HamidNaghipour I need calculate user work experiences. In my example data user has 7 year work experience. How I can calculate it with eloquent?
– Andreas Hunter
Nov 10 at 10:56
add a comment |
What do you want to have in output? can you explain that please?
– Hamid Naghipour
Nov 10 at 10:26
@HamidNaghipour I need calculate user work experiences. In my example data user has 7 year work experience. How I can calculate it with eloquent?
– Andreas Hunter
Nov 10 at 10:56
What do you want to have in output? can you explain that please?
– Hamid Naghipour
Nov 10 at 10:26
What do you want to have in output? can you explain that please?
– Hamid Naghipour
Nov 10 at 10:26
@HamidNaghipour I need calculate user work experiences. In my example data user has 7 year work experience. How I can calculate it with eloquent?
– Andreas Hunter
Nov 10 at 10:56
@HamidNaghipour I need calculate user work experiences. In my example data user has 7 year work experience. How I can calculate it with eloquent?
– Andreas Hunter
Nov 10 at 10:56
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
1) Create a model in app
folder where filename is Workplace.php
with following content:
<?php namespace App;
use IlluminateDatabaseEloquentModel;
class Workplace extends Model
protected $table = 'workplaces';
protected $fillable = ['user_id', 'company', 'position', 'description', 'from', 'to'];
public $timestamps = true;
public function user()
return $this->belongsTo('AppUser');
public function experienceYears()
$from = property_exists($this, 'from') ? $this->from : null;
$to = property_exists($this, 'to') ? $this->to : null;
if (is_null($from)) return 0;
if (is_null($to)) $to = $from; // or = date('Y'); depending business logic
return (int)$to - (int)$from;
public static function calcExperienceYearsForUser(User $user)
$workplaces =
self::with('experienceYears')
->whereUserId($user->id)
->get(['from', 'to']);
$years = 0;
foreach ($workplaces AS $workplace)
$years+= $workplace->experienceYears;
return $years;
2) Use it in controller's action:
$userId = 1;
$User = User::findOrFail($userId);
$yearsOfExperience = Workplace::calcExperienceYearsForUser($User);
How do you think how can I get users with more or less n - years experience?
– Andreas Hunter
Nov 10 at 14:30
@AndreasHunter for such case You've to addexperience_years
field tousers
table and make console command that will run periodically and do calculation and update that field.
– num8er
Nov 10 at 14:49
add a comment |
up vote
1
down vote
I believe you should do such calculations on the DB side. It will be a much faster and more flexible solution. What if you got 1 million users with multiple experience entries and want to select top 10 most experienced users? Doing such calculation on the PHP side will be extremely inefficient.
Here's a raw query (Postgres) that might do the main part of the job:
SELECT SUM(COALESCE(to_year, extract(year from current_date)) - from_year) from experiences;
If you want to play around with it and test other cases: http://sqlfiddle.com/#!9/c9840b/3
Others might say that this is a premature optimization, but it's a one liner. Raw queries are very capable and should be employed more often.
that's perfect solution, but question is: How I can calculate custom work experiences using laravel eloquent? I hope there is no such requirement to show top 10 users (: which will lead to addexperience_years
tousers
table and background worker or database trigger that will calculate numbers and update that field
– num8er
Nov 10 at 12:44
1
Well, I just wanted to provide an idea. One can always "hide" raw query bits within Model class to feel it more eloquent-like. As your answer is technically correct, I think there's no need to elaborate on an alternative. And I agree on the summary table / triggers part.
– Andrius Rimkus
Nov 10 at 13:51
what about doing extra addition to Your answer? Cause Andreas wants that feature now (:
– num8er
Nov 10 at 14:51
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
1) Create a model in app
folder where filename is Workplace.php
with following content:
<?php namespace App;
use IlluminateDatabaseEloquentModel;
class Workplace extends Model
protected $table = 'workplaces';
protected $fillable = ['user_id', 'company', 'position', 'description', 'from', 'to'];
public $timestamps = true;
public function user()
return $this->belongsTo('AppUser');
public function experienceYears()
$from = property_exists($this, 'from') ? $this->from : null;
$to = property_exists($this, 'to') ? $this->to : null;
if (is_null($from)) return 0;
if (is_null($to)) $to = $from; // or = date('Y'); depending business logic
return (int)$to - (int)$from;
public static function calcExperienceYearsForUser(User $user)
$workplaces =
self::with('experienceYears')
->whereUserId($user->id)
->get(['from', 'to']);
$years = 0;
foreach ($workplaces AS $workplace)
$years+= $workplace->experienceYears;
return $years;
2) Use it in controller's action:
$userId = 1;
$User = User::findOrFail($userId);
$yearsOfExperience = Workplace::calcExperienceYearsForUser($User);
How do you think how can I get users with more or less n - years experience?
– Andreas Hunter
Nov 10 at 14:30
@AndreasHunter for such case You've to addexperience_years
field tousers
table and make console command that will run periodically and do calculation and update that field.
– num8er
Nov 10 at 14:49
add a comment |
up vote
3
down vote
accepted
1) Create a model in app
folder where filename is Workplace.php
with following content:
<?php namespace App;
use IlluminateDatabaseEloquentModel;
class Workplace extends Model
protected $table = 'workplaces';
protected $fillable = ['user_id', 'company', 'position', 'description', 'from', 'to'];
public $timestamps = true;
public function user()
return $this->belongsTo('AppUser');
public function experienceYears()
$from = property_exists($this, 'from') ? $this->from : null;
$to = property_exists($this, 'to') ? $this->to : null;
if (is_null($from)) return 0;
if (is_null($to)) $to = $from; // or = date('Y'); depending business logic
return (int)$to - (int)$from;
public static function calcExperienceYearsForUser(User $user)
$workplaces =
self::with('experienceYears')
->whereUserId($user->id)
->get(['from', 'to']);
$years = 0;
foreach ($workplaces AS $workplace)
$years+= $workplace->experienceYears;
return $years;
2) Use it in controller's action:
$userId = 1;
$User = User::findOrFail($userId);
$yearsOfExperience = Workplace::calcExperienceYearsForUser($User);
How do you think how can I get users with more or less n - years experience?
– Andreas Hunter
Nov 10 at 14:30
@AndreasHunter for such case You've to addexperience_years
field tousers
table and make console command that will run periodically and do calculation and update that field.
– num8er
Nov 10 at 14:49
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
1) Create a model in app
folder where filename is Workplace.php
with following content:
<?php namespace App;
use IlluminateDatabaseEloquentModel;
class Workplace extends Model
protected $table = 'workplaces';
protected $fillable = ['user_id', 'company', 'position', 'description', 'from', 'to'];
public $timestamps = true;
public function user()
return $this->belongsTo('AppUser');
public function experienceYears()
$from = property_exists($this, 'from') ? $this->from : null;
$to = property_exists($this, 'to') ? $this->to : null;
if (is_null($from)) return 0;
if (is_null($to)) $to = $from; // or = date('Y'); depending business logic
return (int)$to - (int)$from;
public static function calcExperienceYearsForUser(User $user)
$workplaces =
self::with('experienceYears')
->whereUserId($user->id)
->get(['from', 'to']);
$years = 0;
foreach ($workplaces AS $workplace)
$years+= $workplace->experienceYears;
return $years;
2) Use it in controller's action:
$userId = 1;
$User = User::findOrFail($userId);
$yearsOfExperience = Workplace::calcExperienceYearsForUser($User);
1) Create a model in app
folder where filename is Workplace.php
with following content:
<?php namespace App;
use IlluminateDatabaseEloquentModel;
class Workplace extends Model
protected $table = 'workplaces';
protected $fillable = ['user_id', 'company', 'position', 'description', 'from', 'to'];
public $timestamps = true;
public function user()
return $this->belongsTo('AppUser');
public function experienceYears()
$from = property_exists($this, 'from') ? $this->from : null;
$to = property_exists($this, 'to') ? $this->to : null;
if (is_null($from)) return 0;
if (is_null($to)) $to = $from; // or = date('Y'); depending business logic
return (int)$to - (int)$from;
public static function calcExperienceYearsForUser(User $user)
$workplaces =
self::with('experienceYears')
->whereUserId($user->id)
->get(['from', 'to']);
$years = 0;
foreach ($workplaces AS $workplace)
$years+= $workplace->experienceYears;
return $years;
2) Use it in controller's action:
$userId = 1;
$User = User::findOrFail($userId);
$yearsOfExperience = Workplace::calcExperienceYearsForUser($User);
edited Nov 10 at 11:56
answered Nov 10 at 11:06
num8er
11k21839
11k21839
How do you think how can I get users with more or less n - years experience?
– Andreas Hunter
Nov 10 at 14:30
@AndreasHunter for such case You've to addexperience_years
field tousers
table and make console command that will run periodically and do calculation and update that field.
– num8er
Nov 10 at 14:49
add a comment |
How do you think how can I get users with more or less n - years experience?
– Andreas Hunter
Nov 10 at 14:30
@AndreasHunter for such case You've to addexperience_years
field tousers
table and make console command that will run periodically and do calculation and update that field.
– num8er
Nov 10 at 14:49
How do you think how can I get users with more or less n - years experience?
– Andreas Hunter
Nov 10 at 14:30
How do you think how can I get users with more or less n - years experience?
– Andreas Hunter
Nov 10 at 14:30
@AndreasHunter for such case You've to add
experience_years
field to users
table and make console command that will run periodically and do calculation and update that field.– num8er
Nov 10 at 14:49
@AndreasHunter for such case You've to add
experience_years
field to users
table and make console command that will run periodically and do calculation and update that field.– num8er
Nov 10 at 14:49
add a comment |
up vote
1
down vote
I believe you should do such calculations on the DB side. It will be a much faster and more flexible solution. What if you got 1 million users with multiple experience entries and want to select top 10 most experienced users? Doing such calculation on the PHP side will be extremely inefficient.
Here's a raw query (Postgres) that might do the main part of the job:
SELECT SUM(COALESCE(to_year, extract(year from current_date)) - from_year) from experiences;
If you want to play around with it and test other cases: http://sqlfiddle.com/#!9/c9840b/3
Others might say that this is a premature optimization, but it's a one liner. Raw queries are very capable and should be employed more often.
that's perfect solution, but question is: How I can calculate custom work experiences using laravel eloquent? I hope there is no such requirement to show top 10 users (: which will lead to addexperience_years
tousers
table and background worker or database trigger that will calculate numbers and update that field
– num8er
Nov 10 at 12:44
1
Well, I just wanted to provide an idea. One can always "hide" raw query bits within Model class to feel it more eloquent-like. As your answer is technically correct, I think there's no need to elaborate on an alternative. And I agree on the summary table / triggers part.
– Andrius Rimkus
Nov 10 at 13:51
what about doing extra addition to Your answer? Cause Andreas wants that feature now (:
– num8er
Nov 10 at 14:51
add a comment |
up vote
1
down vote
I believe you should do such calculations on the DB side. It will be a much faster and more flexible solution. What if you got 1 million users with multiple experience entries and want to select top 10 most experienced users? Doing such calculation on the PHP side will be extremely inefficient.
Here's a raw query (Postgres) that might do the main part of the job:
SELECT SUM(COALESCE(to_year, extract(year from current_date)) - from_year) from experiences;
If you want to play around with it and test other cases: http://sqlfiddle.com/#!9/c9840b/3
Others might say that this is a premature optimization, but it's a one liner. Raw queries are very capable and should be employed more often.
that's perfect solution, but question is: How I can calculate custom work experiences using laravel eloquent? I hope there is no such requirement to show top 10 users (: which will lead to addexperience_years
tousers
table and background worker or database trigger that will calculate numbers and update that field
– num8er
Nov 10 at 12:44
1
Well, I just wanted to provide an idea. One can always "hide" raw query bits within Model class to feel it more eloquent-like. As your answer is technically correct, I think there's no need to elaborate on an alternative. And I agree on the summary table / triggers part.
– Andrius Rimkus
Nov 10 at 13:51
what about doing extra addition to Your answer? Cause Andreas wants that feature now (:
– num8er
Nov 10 at 14:51
add a comment |
up vote
1
down vote
up vote
1
down vote
I believe you should do such calculations on the DB side. It will be a much faster and more flexible solution. What if you got 1 million users with multiple experience entries and want to select top 10 most experienced users? Doing such calculation on the PHP side will be extremely inefficient.
Here's a raw query (Postgres) that might do the main part of the job:
SELECT SUM(COALESCE(to_year, extract(year from current_date)) - from_year) from experiences;
If you want to play around with it and test other cases: http://sqlfiddle.com/#!9/c9840b/3
Others might say that this is a premature optimization, but it's a one liner. Raw queries are very capable and should be employed more often.
I believe you should do such calculations on the DB side. It will be a much faster and more flexible solution. What if you got 1 million users with multiple experience entries and want to select top 10 most experienced users? Doing such calculation on the PHP side will be extremely inefficient.
Here's a raw query (Postgres) that might do the main part of the job:
SELECT SUM(COALESCE(to_year, extract(year from current_date)) - from_year) from experiences;
If you want to play around with it and test other cases: http://sqlfiddle.com/#!9/c9840b/3
Others might say that this is a premature optimization, but it's a one liner. Raw queries are very capable and should be employed more often.
answered Nov 10 at 12:04
Andrius Rimkus
27528
27528
that's perfect solution, but question is: How I can calculate custom work experiences using laravel eloquent? I hope there is no such requirement to show top 10 users (: which will lead to addexperience_years
tousers
table and background worker or database trigger that will calculate numbers and update that field
– num8er
Nov 10 at 12:44
1
Well, I just wanted to provide an idea. One can always "hide" raw query bits within Model class to feel it more eloquent-like. As your answer is technically correct, I think there's no need to elaborate on an alternative. And I agree on the summary table / triggers part.
– Andrius Rimkus
Nov 10 at 13:51
what about doing extra addition to Your answer? Cause Andreas wants that feature now (:
– num8er
Nov 10 at 14:51
add a comment |
that's perfect solution, but question is: How I can calculate custom work experiences using laravel eloquent? I hope there is no such requirement to show top 10 users (: which will lead to addexperience_years
tousers
table and background worker or database trigger that will calculate numbers and update that field
– num8er
Nov 10 at 12:44
1
Well, I just wanted to provide an idea. One can always "hide" raw query bits within Model class to feel it more eloquent-like. As your answer is technically correct, I think there's no need to elaborate on an alternative. And I agree on the summary table / triggers part.
– Andrius Rimkus
Nov 10 at 13:51
what about doing extra addition to Your answer? Cause Andreas wants that feature now (:
– num8er
Nov 10 at 14:51
that's perfect solution, but question is: How I can calculate custom work experiences using laravel eloquent? I hope there is no such requirement to show top 10 users (: which will lead to add
experience_years
to users
table and background worker or database trigger that will calculate numbers and update that field– num8er
Nov 10 at 12:44
that's perfect solution, but question is: How I can calculate custom work experiences using laravel eloquent? I hope there is no such requirement to show top 10 users (: which will lead to add
experience_years
to users
table and background worker or database trigger that will calculate numbers and update that field– num8er
Nov 10 at 12:44
1
1
Well, I just wanted to provide an idea. One can always "hide" raw query bits within Model class to feel it more eloquent-like. As your answer is technically correct, I think there's no need to elaborate on an alternative. And I agree on the summary table / triggers part.
– Andrius Rimkus
Nov 10 at 13:51
Well, I just wanted to provide an idea. One can always "hide" raw query bits within Model class to feel it more eloquent-like. As your answer is technically correct, I think there's no need to elaborate on an alternative. And I agree on the summary table / triggers part.
– Andrius Rimkus
Nov 10 at 13:51
what about doing extra addition to Your answer? Cause Andreas wants that feature now (:
– num8er
Nov 10 at 14:51
what about doing extra addition to Your answer? Cause Andreas wants that feature now (:
– num8er
Nov 10 at 14:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237497%2flaravel-eloquent-calculate-work-experience%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you want to have in output? can you explain that please?
– Hamid Naghipour
Nov 10 at 10:26
@HamidNaghipour I need calculate user work experiences. In my example data user has 7 year work experience. How I can calculate it with eloquent?
– Andreas Hunter
Nov 10 at 10:56