0

I was trying to declare a static var, but always is null, I try several combinations and ways without luck. I'm trying to store a var with some data, but I really don't want to reprocess, If already was processed one time.

            class PackingController extends Controller
            {
                protected static $i;

                function Process(Request $request)
                {
                    $r = new packing();
                    $v = $r->customer('data','data');
                    $items = $this->startProcess($v,false);
                    return \View::make('list/print',compact('items'));

                }

                private function startProcess($dbitems, $ajust=false)
                {

                //Some logic

                self::$i = $items;
                return $items;

                }

                function printPdf()
                {

                    $items = self::$i;
                    //shows null:
                    dd($items);
                }
            }

What's is wrong?

Update: I try with singleton without luck.

        namespace App\Helper;

        class Items
        {

            private static $_items;
            private static $inst;

            public function setItems($value)
            {
                self::$_items = $value;
            }

            public function getItems()
            {
                return self::$_items;
            }


            public static function Instance()
            {
                if (self::$inst == null) {
                    self::$inst = new Items();
                }

                return self::$inst;
            }

            private function __clone()
            {
            }

            private function __construct()
            {
            }

        }

With singleton, still null, Also try with a single value, instead of variable, without luck

    class PackingController extends Controller
    {
        protected static $i;


        function GetPackings()
        {
            $s = sales::with('Customer')->where('salestype','=','3')->take(1000)->orderBy('receiptdaterequested','desc')->pluck('salesid','salesid');

            $p = pack::distinct('APPACKINGGROUPID')->pluck('APPACKINGGROUPID','APPACKINGGROUPID')->all();

            return \View::make('listas/index',compact('p','s'));
        }

        function Process(Request $request)
        {
            $r = new packing();
            $v = $r->customer('Mexico','PVE0111998');
            $items = $this->startProcess($v,false);
            return \View::make('listas/print',compact('items'));

        }

        private function startProcess($dbitems, $ajust=false)
        {
            $bultos = 1;

            $items = array();

            foreach ($dbitems as $dbitem)
            {
            //Some process

                $items[]=$item;
            }

            $s = Items::Instance();

            if (count($items) > 0 ) {
                $s->setItems($items);
            }

            return $items;
        }

        function printPdf()
        {
            $s = Items::Instance();
            $s->getItems();

            dd($s->getItems());
        }
    }
2
  • self::$i = $items where is $items defined or is it $dbitems? Commented Jul 3, 2018 at 15:38
  • $items are the result of $dbitems process, is one array, I try with singleton without luck, I really don't understand Commented Jul 3, 2018 at 17:50

1 Answer 1

2

Why are you tryings to declare a static var or singleton to pass values between the methods? Maybe you should try with save data in session:

session()->set('items',$dbtitems);

And...

$items = session()->get('items);
Sign up to request clarification or add additional context in comments.

2 Comments

I try that, but still null,
session()->put('items',$items); session()->save(); $items = session()->get('items');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.