0

I have two separate controllers which are PostController for returning input value to blade and APIController for returning data from the database to blade. I have tried passing the variable with SESSION but still no function. Please help.

Index.blade.php

<form class="form-inline" action="{{ action('PostController@index') }}" method="GET">
   <div class="form-group">
      <label class="mb-2 mr-sm-2 col-form-label">Date : </label>
         <input type="date" name="date" id="date" style="width:160px;" class="form-control mb-2 mr-sm-2" value="<?php if(isset($_GET['submit'])) echo $_GET['date']; else echo date('Y-m-d');?>">
   </div>
   <input type="submit" name="submit" class="btn btn-primary mb-2" value="Inquiry">
</form>

PostController.php

<?php
   namespace App\Http\Controllers;

   use Illuminate\Support\Facades\Input;
   use Session;

   class PostController extends Controller
   {
      public function index()
      {     
         $getDate = Input::get('date'); //trying to pass this to APIController for query
         $getCurrentDate = date('Y-m-d');

         Session::put('getDate', $getDate); //for example $getDate = '2019-01-01'

         return view('posts.index')->with('getCurrentDate', $getCurrentDate)->with('getDate', $getDate);
      }
   }

APIController.php

<?php    
   namespace App\Http\Controllers;

   use Illuminate\Support\Facades\DB;
   use Yajra\Datatables\Datatables;
   use Session;

   class APIController extends Controller
   {
      public function getQueries()
      {
         ini_set('memory_limit', '1024M');
         ini_set('max_execution_time', 300);

         $getInputDate = Session::get('getDate'); //don't get anything here

         //dd($getInputDate); //have tried dumping, it returns null

         $getCurrentDate = date('Y-m-d');

         if($getInputDate == NULL)
         {
            $query = DB::select(DB::raw("SELECT * FROM TABLE WHERE date = '$getCurrentDate'");

            return Datatables::of($query)->make(true); 
         }
         else 
         {
            $query = DB::select(DB::raw("SELECT * FROM TABLE WHERE date = '$getInputDate'");

            return Datatables::of($query)->make(true);
         }
      }
   }

Web.php

Route::get('/', 'PostController@index');  

Route::group(['middleware' => 'web'], function () {
    // Put routes in here
    Route::get('/json/getQueries', 'APIController@getQueries')->name('api.posts.index');
});

Api.php

Route::get('/json/getQueries', 'APIController@getQueries')->name('api.posts.index');

Route::group(['middleware' => 'web'], function () {
    // Put routes in here
    Route::get('/', 'PostController@index'); 
});
4
  • which version of laravel are you currently using? Commented Jul 22, 2019 at 6:57
  • @RonS Laravel 5.4. Commented Jul 22, 2019 at 7:01
  • You can accomplish this using laravel custom-channels. Documetation: laravel.com/docs/5.4/notifications#custom-channels Commented Jul 22, 2019 at 7:03
  • @RonS custom channels can pass it even though for separate controller? Commented Jul 22, 2019 at 7:08

2 Answers 2

1

Try putting the affected routes within the web middleware group, so you should share the session between them.

Route::group(['middleware' => 'web'], function () {
    // Put routes in here
});

If I'm not mistaken, this case should be for the api routes, which want to share the sessions with the web routes.

Sign up to request clarification or add additional context in comments.

2 Comments

It's working. It must be added in both routes in order to work. I have updated my post
Great, I'm glad I could help you. A cordial greeting.
0

In controller add use Session; it will work.

https://laravel.com/docs/5.2/session#adding-custom-session-drivers

//use Illuminate\Support\Facades\Session;  
 use Session;

I tried the same concept it's working for me.

5 Comments

I tried it just now. Still not working. I try dd($getInputDate) inside APIController, it returns null.
did you update in both the controller? after update submit the form then check.
In PostController what value you are getting of ' $getDate '?
Date format like this 2019-01-01 but it only will have value when I submit the form. If not submitted yet, then $getDate is null. So in APIController will check if null, display current date of data. If not null, then display data based on the date submitted.
I tried your complete code it's working for me :)). Version is 5.4

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.