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

tzak9022's avatar

How to access a column data from a function into a different function

Hi guys,

It seems a strange question, I have 2 functions, the first function pull the data from the DB which is fine, the second function does a calculation based on current date and departure date, now the departuredate exist within the first function.

My question is how can I access departuredate from first function into the second one?

first function:

 private function get_view($from, $to)
    {
        $Reports = DB::table('create_vouchers')->whereNull('create_vouchers.deleted_at')
            ->select('create_vouchers.id', 'client_name', 'night', 'hotels.hotel_name', 'arrivaldate', 'departuredate', 'total_amount', 'number_of_room', 'payment_mode')
            ->join('hotels', 'hotels.id', 'create_vouchers.hotel_name_id')
            ->whereBetween('arrivaldate', [$from, $to])->get();


        return view('admin.vouchersAccountings.index', compact('Reports', 'from', 'to'));
    }

Second function:

public function getStatus()
    {
        $CurrentDay = Carbon::today()->format(config('panel.date_fomat'));
        $DepartureDate = DB::table('create_vouchers.departuredate');
        
        if ($CurrentDay >= $DepartureDate) {
            return "S";
        } else {
            return "D";
        }
    }
0 likes
8 replies
RayC's avatar

You could set it at the top of your Model:

public $CurrentDay = null;
public $DepartureDate = null;

public function getStatus()
    {
        $this->CurrentDay = Carbon::today()->format(config('panel.date_fomat'));
        $this->DepartureDate = DB::table('create_vouchers.departuredate');
        
        if ($this->CurrentDay >= $this->DepartureDate) {
            return "S";
        } else {
            return "D";
        }
    }

// Access it from within another method
$this->CurrentDay;
$this->DepartureDate;
tzak9022's avatar

@RayC Thank you very much, i got a small issue

Error
Using $this when not in object context

my current code:

 private function get_view($from, $to)
    {
        $Reports = DB::table('create_vouchers')->whereNull('create_vouchers.deleted_at')
            ->select('create_vouchers.id', 'client_name', 'night', 'hotels.hotel_name', 'arrivaldate', 'departuredate', 'total_amount', 'number_of_room', 'payment_mode')
            ->join('hotels', 'hotels.id', 'create_vouchers.hotel_name_id')
            ->whereBetween('arrivaldate', [$from, $to])->get();


        return view('admin.vouchersAccountings.index', compact('Reports', 'from', 'to'));
    }

    public $DepartureDate = null;

    public function getStatus()
    {
        $CurrentDay = Carbon::today()->format(config('panel.date_fomat'));
        $this->DepartureDate = DB::table('create_vouchers.departuredate');

        if ($CurrentDay >= $this->DepartureDate) {
            return "S";
        } else {
            return "D";
        }
    }
Tray2's avatar

@tzak9022 Not sure what it is you are trying to do here, but any properties in a class is usually defined first in the class, and not between methods. Why is the get_view a private method? My guess is that is the method you tell Laravel to execute from your route, if so, it should be public.

This line makes no sense at all, you try to assign it a table called departuredate in a database called create_vouchers....

$this->DepartureDate = DB::table('create_vouchers.departuredate');

I do suggest that you take a look here before you do anything else.

https://laracasts.com/series/laravel-8-from-scratch

tzak9022's avatar

@Tray2 actually the table is : create_vouchers and the column is departuredate

I'm trying to get the departuredate column from get_view function without creating a new query from scratch, is that possible?

Tray2's avatar

@tzak9022 Sure, you can.

$Reports->the_column_you_want

This returns an instance of the query builder, and does not contain the date.

DB::table('create_vouchers.departuredate');
tzak9022's avatar

@Tray2 How about this code

 public function getStatus()
    {
        $CurrentDay = Carbon::today()->format(config('panel.date_fomat'));
        $DepartureDate = DB::table('create_vouchers')
            ->select('departuredate')
            ->get();
        return $DepartureDate;
      

        if ($DepartureDate > $CurrentDay) {
            return "Staying";
        } else {
            return "Departed";
        }
    }

however i'm getting all the departure dates of other rows

1 Michael Cox hotel 2022-07-31 2022-08-01 1 1 by_cash 1000.00 [{"departuredate":"2022-08-01"},{"departuredate":"2022-08-21"},{"departuredate":"2022-08-28"},{"departuredate":"2022-08-20"},{"departuredate":"2022-08-25"},{"departuredate":"2022-08-28"},{"departuredate":"2022-09-09"}]

Tray2's avatar

@tzak9022 Of course you do, you are asking for all of them, you need to add a where in your query. However, you need to learn some essentials about programming first.

public function getStatus()
    {
        $CurrentDay = Carbon::today()->format(config('panel.date_fomat'));
		
//This part fetches all the create_vouchers (Which is a bad table name btw)
        $DepartureDate = DB::table('create_vouchers')
            ->select('departuredate')
            ->get();

// This returns the departure dates
        return $DepartureDate;
      
// Nothing after the return above is ever executed, so everything below is dead code.
        if ($DepartureDate > $CurrentDay) {
            return "Staying";
        } else {
            return "Departed";
        }
    }

Oh, and please don't use Capitals in the beginning of a varible name.

$DepartuteDate 

// should be

$departureDate

And when it comes to method names, pick a standard and use it everywhere. I prefere using the same naming convention as my variables getView and not get_view .

Colum names in your database that contains more than one word are seperated by an underscore.

departuredate should be departure_date

RayC's avatar

@tzak9022 As @tray2 stated above, you need to use where to get one record or CreateVoucher::find(1); where 1 is the id of the record you're looking for:

public function getStatus($id)
    {
        $CurrentDay = Carbon::today()->format(config('panel.date_fomat'));
        $DepartureDate = CreateVoucher::find($id);  // 1 being the ID of the record in the table.

        if ($DepartureDate->departuredate > $CurrentDay) {
            return "Staying";
        } else {
            return "Departed";
        }
    }

// You can call this method in your other method.
// This will return either Staying or Departed for the record you've indicated by the ID .
$this->getStatus(1);

Make sure to include the CreateVoucher model. If this doesn't make any sense, please tale the advice above and watch the Laravel 8 From scratch series.

Please or to participate in this conversation.