Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

In the place of question mark what comes for adding year in all table as session value laravel

guys i have a small doubt.

i have a form to add academic year. and a table to store it.

this is my table academic_year with columns

id      acc_year            note                status

1       2019-2020       current year            active

so after inserting this year, i will work with other forms.

say for example i have a form to add class and table class in the below table academic year was added. but inmy form i should not have academic control. when i try to add the class details into class table automatically the active of year must be stored as acc_year in the class table

id      acc_year_id(fk)          class               note           status
1       1                               Vth std          fifth std       active

this is my coding

 $class=new Class;

       $class->acc_year_id =????????????;
       $class->class =trim($request->class);
       $class->note = trim($request->note);
       $class->status = trim($request->status);

In the place of question mark what comes? the year which is active from the year table must be inserted in the acc_year_id.

whats the solution kindly some one suggest please.

0 likes
22 replies
Tray2's avatar

You probably have a form where you select the acc_year

<select name="acc_year">
    @foreach( $academicYears as $acYear)
        <option value="{{ $acYear->id }}">{{ $acYear->academic_year }}</option>
    @endforeach
</select>

So when you submit it to your store method it would become

$class->acc_year_id = $request->acc_year;
1 like
aurawindsurfing's avatar

Hi @abdulbazith

At first you are asking for trouble here using PHP reserved name class here is list of PHP reserved words: https://www.php.net/manual/en/reserved.keywords.php

Second you are using string to record a year. The way you could do this is have maybe one year field since academic year always have starting date. One you have that it will be easy to display end year, just add one year to it right?

Third, you are correctly creating new instance of your Class (see how confusing it already gets with that name) but you should leverage laravel Eloquent models, relations to create and persist your data. Have a look here: https://laravel.com/docs/5.8/eloquent

Hope it helps!

AbdulBazith's avatar

@tray2 thank you for your response

ya you are right. i can keep a select box in the form. but what my client expecting is the year must be in session.

so that after login the system prompts to choose the year. after choosing the year, what are actions i perform must be done to that years record. say for example after choosing an year, i will add class, add student, add fee payment, delete a student whatever i do that must be process withing the chosen year.

thats my doubt.

iam expecting like this

Auth::user()->school_id; // this generally picks the login user 

like thie above command i need a query AcademicYear::year()->year_id; like this i need.

i should not choose each and everytime the year. the year must be static by choosing first itself.

thats my big problem.

AbdulBazith's avatar

@aurawindsurfing thank you for your response.

iam using 2019-2020 like this as string. is this right only?? whats the problem i will face here??

you are saying that to add year and month for start and end

am i right??

but iam using like this 2019-2020.

during my search i will use same like that only 2019-2020

so what will be the problem.

JorickL's avatar

You should change the data structure of you academic_year table: add a starts_at and ends_at column with the type of date.

Then you are able to reference by id (which will return an academic year) or find the correct ID with a query like:

$today = now();
...
$class->acc_year_id = AcademicYear::where('starts_at', '<', $today)->where('ends_at', '>', $today)->first();
...

You can even specify a scope on the AcademicYear model (at least, I assume you do have a model related to your academic_year table) which will accept the date and returns the correct AcademicYear.

If that's not what you want, fetch all the records from the academic_year table and give that to the view. Create a select element which foreaches trough all the academic_years and give it a value of the ID and show the acc_year as visual representation, something like this:

<select name="academic_year">
@foreach($academic_years as $year)
<option value="{{ $year->id }}">{{$year->acc_year}}</option>
@endforeach()
</select>

Good luck!

1 like
aurawindsurfing's avatar

@abdulbazith in theory there is no problem with it at all until you actually try to do something with it like add, subtract etc. Also in theory sorting will work but sorting strings might produce different outputs then sorting integers. Many things can go wrong.

AbdulBazith's avatar

@jorickl @aurawindsurfing thank you for your responses.

so i understood that keeping it in date format can give me some worthy outputs.

ok let me follow it.

actually my problem is,

when an accountant of the school login the application, he/she is asked to choose the year from the drop down. after choosing it he moves to the dashboard. the data which is showed in the screen are records of that chosen year.

the accountant can perform, insert delete, update of any class, fees, students, salary, etc etc, that should be done for that year records only.

if he needs to move back to the previous year, he must have a option in the nav bar to change year, if he changes that, that those years records are displayed for student, class, fee etc. if he performs any actions then that year records should be modified or deleted are something.

this is my major doubt.

for each record insertion, deletion, updating, accountant should not choose the year, once the year is chosen and moved inside, everything must be done within that year records only. the year must be in session,so that only this can be achieved??

Tray2's avatar

Let's say you have the school years 2018 - 2019 and 2019 - 2020 in a school_years table that have have an id of 1 and 2.

And you have a droppdown where you select the school year and upon that selection you update your page with the records corresponding to that year. You pass that value (id 1 or id 2) depending on which year you are working on to your crud methods.

In your school_years you have something like this

  • id
  • first_day_of_school
  • last_day_of_school

The data would look something like this

1   2018-09-10 8:00:00  2019-06-05 10:00:00
2   2019-09-12 8:00:00      2020-06-08  10:00:00

So if you have an activities table you can do something like this

  • id
  • activity
  • description
  • acivity_type_id
  • school_year_id
  • date
1   First day of school     The start of the school year        1   1   null
2   National exams Math     The first national exam in math 2   1   2018-11-05 8:00

Your activity table would look something like this

  • id
  • activity
1   school_start
2   Exam
aurawindsurfing's avatar

@abdulbazith answer is correct. If you want to store the information in what year you are now in session here is how you would do it:

session(['academic_year' => '2018-2019']);

or better following the convention, only the id of the actual year:

session(['academic_year' => 2]);

then to retrieve it from user session you do:

$academic_year = session('academic_year');

More about it here: https://laravel.com/docs/master/session#using-the-session

Snapey's avatar

I would get away from a string in the format you show. Thats just decoration.

Either store all years in a table that you can have as a relation on all other tables, or just store the year as a number on each table

What I mean is '2019' is all you need. In your dropdown just create the string on the fly since years are always $year.'-'.$year+1

You might want to create a table for the year if you need to store other things such as start and end date, or other attributes that are specific to the year

AbdulBazith's avatar

@snapey thank you for your response.

according to you, you say that let it be in string or number??

i have to form to insert the year. i will insert it like this only 2019-2020 as string from text box to table.

is this right??

Snapey's avatar

What I'm saying is that year can be integer, eg '2019'

Use it on all records that are created (add a year column to every table that is year specific)

When you have your dropdown to choose the year,

<select name='year'>
    <option value='2019'>2019 - 2020</option>
    <option value='2020'>2020 - 2021</option>
    <option value='2021'>2021 - 2022</option>
    <option value='2022'>2022 - 2023</option>
</select>

after choosing the year, you will have one value, 2019, 2020 etc

Save this value in session

Write the value to any records that are year specific

Create a scope on all your models that only get the 'active' year records

eg

    public function scopeActiveYear($query)
    {
        return $query->where('year',session('year'));
    }

then when getting invoices for example;

    $invoices = Invoice::activeYear()->get();

The above is using local scope, but you could also use a global scope and then you don't need to specify which year you are working with in your queries. You would then use withoutGlobalScope() when you need to override and get records from any year

You could also use a model event to automatically add the active year to all new records

AbdulBazith's avatar

@tray2 @aurawindsurfing @jorickl @snapey

Now which is better, to have a start date and end date columns or simply academic_year column??

but one thing just i need only the years. not the month or date.

just need the year 2017-2018, or 2018-2019, or 2019-2020.... like this so on..

for this inly i have a form

academic year: 2019-2020 (as text box to enter)

so what i should do now??

confusing.....

Snapey's avatar

I think I already answered your questions

Tray2's avatar

I would go with proper dates since in the future there might be a need to have different timespans like 6 months course, 3 months and so on.

Snapey's avatar

@tray then the concept of 'academic year' is broken and lots of other things need to be changed.

Like 'financial year' all invoices, subscriptions, expenses etc belong to a financial year, but each can have different dates and timespans. They all belong to the same year

Tray2's avatar

True, but you would get the start date and end date for free.

AbdulBazith's avatar

@snapey

then how i can save it as 2019-2020 in table. u having

 <option value='2019'>2019 - 2020</option>

like above means then how i can save it as 2018-2019 like this??

this is my adding year form

refer: https://imgur.com/hmAfnBf

and this is my table columns,

id,
academic_year
note
status

now how i should change this table columns and form??

after doing this my next is adding class details like below

refer: https://imgur.com/MbzlLEQ

see in the image, its template. the academic year is displayed above. if user need they can change

Kindly reply please

aurawindsurfing's avatar

@abdulbazith

The way you save in in database is simply integer with value of 2019 exactly what you see in the value tag here:

 <option value='2019'>2019 - 2020</option>

then the representation like @snapey told you before will be a decoration of 2019-2020 but this is only for user to know what it is, it does not matter at all for your database.

Another example of it would be when you have selector for colours:

 <option value='1'>red</option>
 <option value='2'>green</option>
 <option value='3'>blue</option>

See what I mean? The same way you shoudl treat your years, keep value on database, show human readable version to your user.

Hope it helps!

Snapey's avatar

Yes. Just save 2019 in the database. Its meaning is 2019 - 2020

AbdulBazith's avatar

@snapey @aurawindsurfing thank you.

i understood clearly @snapey .. thank you thank you.

but the important thing is session??

the year to be chosen by the accountant. so that, once it chosen and moved inside the records displayed, added , deleted etc everything will be done for that particalr year records only.

each and every time accountant should not chose the year??

how??

Please or to participate in this conversation.