I need help with passing the variable I have initialized using __construct() to view in Laravel.
Here is my controller code
protected $profileInfo;
public function __construct(){
$this->profileInfo = Profile::with('address')->where('id', '=', '1')->get();
}
public function index(){
$this->profileInfo;
return view('admin_pages.profile', compact('profileInfo'));
}
I get an error undefined variable profileInfo
Advertisement
Answer
When using compact() the parameters used must be defined as variables:
protected $profileInfo;
public function __construct(){
$this->profileInfo = Profile::with('address')->where('id', '=', '1')->get();
}
public function index(){
$profileInfo = $this->profileInfo;
return view('admin_pages.profile', compact('profileInfo'));
}
In this case compact() will create an array like this:
[ 'profileInfo' => $this->profileInfo ]