I need help with passing the variable I have initialized using __construct()
to view in Laravel.
Here is my controller code
JavaScript
x
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:
JavaScript
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:
JavaScript
[ 'profileInfo' => $this->profileInfo ]