How to pass a variable from one controller to another laravel - laravel-5

I'm trying to pass a variable from a controller to some other controller. I'm doing it like this-
SomeController
public function index(){
$var = 'variable';
return redirect()->route('toAnotherController')->with('var', $var);
}
Route.php
Route::get('/anotherController', 'AnotherController#index')->name('toAnotherController');
Another Controller
public function index(){
echo $var;
}
But, this is giving an error "Undefined variable $var".
What is going wrong here?
Is there any other way to do this?

This will help you:
Route::get('/anotherController/{var}', 'AnotherController#index')->name('toAnotherController');
public function index($var){
echo $var;
}
Then use Redirect:
return redirect()->route('toAnotherController',[$var]);
Good Luck ~~~

I hope this is not too late. But I am sure this might help somebody one day!!!
In your SomeController
Add this instance public $attributes;
Add the variable you want to pass to another controller with this
$request->attributes->add(['var' => $request->var]);
You can equally resolve it with the code below on the (Another Controller)controller where you needed it
$var= app('request')->get('var');
N:B - Laravel 5.7. I am not sure of earlier versions

Related

"app\Http\Controllers\" does not exist") laravel 8

I just want to echo Hello world through Route
Route::get('/user',[addnewcontroller::class, 'getData']);
this is my web.php Route
and this is controller
class addnewcontroller extends Controller
{
function getData()
{
echo "My name is hassan";
}
}
whenever i try to execute it say controller doesnt exist, kindly let me know why im facing this error, ive tried many things like clearing caches, code etc
you need to define path of controller in web.php
like this:
use App\Http\Controllers\addnewcontroller;
You must use App\Http\Controllers\addnewcontroller at the top of routes

Laravel new controller method doesn't work

When I create a new function in the controller for some reason it does not work. When I set the code from this function(getUnitsNotIn) to another function(index), that code works.
Does anyone know why this is happening?
My UnitsController.php action
public function index(){
$items = Unit::select('parent_id')->where('parent_id','!=',NULL)->get()->toArray();
return Units::whereNotIn('id',$items)->get();
}
public function getUnitsNotIn(){
$items = Unit::select('parent_id')->where('parent_id','!=',NULL)->get()->toArray();
return Units::whereNotIn('id',$items)->get();
}
My api.php
Route::apiResource('/units', 'UnitController');
Route::get('/units/notIn', 'UnitController#getUnitsNotIn');
In short, any new controller function that I make will not work. I tried to make a new controller and the same thing happens.
How to fix this problem?
It is not working because of apiResource(). Resource route assumes /units/{id}. so when you call /units/notIn route assume notIn as id And Action call show()
You need to use a different name.
Route::get('/un/notIn', 'UnitController#getUnitsNotIn');
Verb, path , action , route name
GET /units/{id} show units.show
Change this to
Route::get('/units-notIn', 'UnitController#getUnitsNotIn');
You define a resource controller so here in units/notin,notin define a id so
it call your show function default.

Cannot read parameter $1 using CodeIgniter URI Routing

I have strange problem. I have URI routing in my config CI like this:
$route['content/:num'] = "content/read/$1";
I also have controller class "content" with "read" method like this:
class Content extends CI_Controller {
public function read($id) {
echo $id;
}
}
And when I type this on browser localhost/myweb/index.php/content/1
It always echoing "$1" not 1.
Thank you for kindly help
in your route you missed the brackets around :num
$route['content/(:num)'] = "content/read/$1";

how to debug data query in laravel

This is a beginner question, but I searched for an hour and couldn't find an answer.
I am trying to write a simple data query which I included in my HomeController
<?php
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
}
$programs=DB::table('node')->where('type', 'Programs')->get();
$programs is undefined so I am guessing that my query didn't work but I have no idea how to debug it. I tried installing firebug and phpbug and chromphp tools but they don't seem to show anything. My apache log doesn't show anything either. Am I missing something? How do I debug this?
You can't use an expression outside of a method when using a class, instead you need to put it inside a method like:
class HomeController extends BaseController {
public function getPrograms()
{
$programs = DB::table('node')->where('type', 'Programs')->get();
// pass the $programs to the programs view for showing it
return View::make('programs')->with('programs', $programs);
}
}
So, for example, if you have a route like this:
Route::get('/programs', 'HomeController#getPrograms');
Then you may use an URL like: example.com/programs to invoke the getPrograms method in the class HomeController.
Probably this answer doesn't help much but I think you should learn the basics (PHP Manual) first so read books and articles online and check the Laravel website to read the documentation.
You can pass the result of that query to the view like so:
class HomeController extends BaseController {
public function showWelcome()
{
$programs = DB::table('node')->where('type', 'Programs')->get();
return View::make('hello', array('programs' => $programs));
}
}
And in your view you will have access to the $programs variable.
I don't know If i'm missing the point, but can't you use the "dd($programs)" to check what is or not is inside the variable?

CodeIgniter Undefined Variable while trying to create subview

Hi I am new to CodeIgniter and am taking a tutorial. I ran into an error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: subview
Filename: admin/_layout_modal.php
Line Number: 7
An Error Was Encountered
Unable to load the requested file: .php
I followed the tutorial to the tee and I cant seem to find out where I am going wrong.
here is the controller
<?php
class User extends Admin_Controller {
public function __construct() {
parent::__construct();
}
public function login() {
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal', $this->data);
}
}
and the view
<?php $this->load->view($subview); ?>
Please help...
If you gave script from your .php file not tutorial then everything is ok.
You are probably typing the wrong url in when you try to access the page. Make sure you are loading "admin/user/login" instead of "admin/dashboard/modal".
If you are following tutplus tutorial- building cms with codeigniter, First watch two episodes, Managing User Part 1 and Managing User Part 2 and then Start building, Your question will be answered on Part 2.... the dashboard has to declare the subview variable and
CREATE THAT admin/dashboard/index.php file in views
class Dashboard extends Admin_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->data['subview'] = 'admin/dashboard/index';
$this->load->view('admin/_layout_main',$this->data);
}
public function Modal(){
$this->load->view('admin/_layout_modal',$this->data);
}
}
don't need to pass data to $this. Just pass the data to
$data['variable_name']=data;
pass this $data variable to view through load class.

Resources