Support the ongoing development of Laravel.io →
Views Blade Installation

I want to add extra variables onto my return function. How can I do this?

<?php

    namespace App\Http\Controllers;

    use App\Http\Requests;
    use Illuminate\Http\Request;
    use Auth;
    use App\User;
    use App\Address;
    use Illuminate\Support\Facades\Hash;

    class ProfileController extends Controller
    {
             public function returnView()
    {
    return view('profile')->with('csspath',$this->css)->with('jspath',$this->js)->with('firstname',$this->first_name)->with('lastname',$this->last_name)->with('phone',$this->phone)->with('landline',$this->landline)->with('address',$this->address)->with('page_name','profile')->with('addressbook',$this->addressbook);
     }
     protected function newAddressBook(Request $request)
     {
         $extra_data = "I want this added onto the returnview function";
         return $this->returnView(); // How do I add more? return $this->returnView()->with('extradata',$extra_data); does not work
      }

}

Last updated 3 years ago.
0

to make things simple and for the sake of readability, i would refactor your code like this

public function returnView($data = []){
	return view('profile', array_merge([
		'csspath' => $this->css,
		'lastname' => $this->last_name
		...
		...
	], $data));
}

protected function newAddressBook(Request $request)
{
 	return $this->returnView([
 		'extra_data' => 'this is extra data '
 	]);
}
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

irexjr irexjr Joined 10 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.