Issue: Converting my Laravel 4 code to Laravel 5.2. I'm at the point of moving and converting my views and cannot get past the following error.
Error: ErrorException in IndexController.php line 27: Attempt to assign property of non-object
Debug Information:
Request: Please assist in figuring out why the error comes up and please explain in detail how to prevent it and what I am doing wrong with example, if possible.
Notes: I used to receive a similar error on Laravel 4 after I would seed the database but I was able to refresh the migrations and reseed the database and everything would start working again. This does not work with this error in Laravel 5. This code works on L4.
Attempts: I have been reading a lot on Google and have tried various items such as php artisan clear-compiled, composer dump-autoload, php artisan optimize to no avail. I believe the error is coming from $numberofpcs = new additionalPCs(); however I have not been able to confirm that. I have also removed all of the variables I am sending to the view and the error still persisted so it looked like $this->layout->content = View::make('index');
IndexController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Library\additionalPCs;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use View;
use App\Models\businesstype;
use App\Models\contractterm;
class IndexController extends BaseController
{
Protected $layout = 'master';
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
/** Wayne - 03-02-2014 - Moved for loop to a method within its own class. */
$numberofpcs = new additionalPCs();
$addtpcs=$numberofpcs->display();
$this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
}
}
additionalPCs.php
<?php
namespace App\Library;
use App\Library\additionalComputer;
class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}
additionalComputer.php
<?php
namespace App\Library;
/** Counts up the Number of Additional PC Options
* and stores them into an array then sends them to the view.
*/
class additionalComputer {
protected function displayMenu() {
$addtpcs= [];
for ($i=0; $i <= 100; $i++) {
$addtpcs[$i] = $i;
}
return $addtpcs;
}
}
BaseController.php - (This is only here to show that I do have a BaseController.php file for the IndexController. I'm aware that L5 doesn't come with one by default.)
<?php
namespace App\Http\Controllers;
class BaseController extends Controller {
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
Patricus assisted me with finding this answer but I wanted to post this because the answer provided still had issues when I attempted it. He was correct however in that I could no longer utilize Controller Layouts.
I changed
$this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
to
return view('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
This resolved that issue.