Laravel: I Can't Send More Then 2 Variables from Controller to a View

Laravel - Pass more than one variable to view

Just pass it as an array:

$data = [
'name' => 'Raphael',
'age' => 22,
'email' => 'r.mobis@rmobis.com'
];

return View::make('user')->with($data);

Or chain them, like @Antonio mentioned.

Laravel 8 problem with pass variable from controller to view

First update you routes. You cannot have similar route.
Update this line of code in Dashboardcheck Controller.

return view('Pages.Dashboard.usercontroll', compact('query'));

You did not pass any $query variable. So it is showing error.

Best Way to return multiple variables to view in laravel

you can create an array like this

$data['bus']=Bus::all();
$data['user']=User::all();
$data['employer']=employer::all();
return view('create',['data'=>$data]);

Can't pass more than two arguments to a route

You may not Send the request to the Controller so for testing purpose put a question mark after arguments to make it optional like below :

Route::get('formicare/{user_slug?}/{taskable_name?}/{taskable_slug?}/tasks/create/{action_name?}', 'TaskController@create')->name('tasks.create');

and then in your controller :

 public function create($user_slug = null, $taskable_name = null, $taskable_slug, $action_name = null)
{
dd('$user_slug');
dd('$taskable_name');
dd('$action_name');
}

Try the Above DD one by one to See Which of your Requests has results and which returns null that causes that error then you can work on it better .

Passing data from controller to view in Laravel

Can you give this a try,

return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));

While, you can set multiple variables something like this,

$instructors="";
$instituitions="";

$compactData=array('students', 'instructors', 'instituitions');
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions);

return View::make("user/regprofile", compact($compactData));
return View::make("user/regprofile")->with($data);

Pass variables to multiple view in laravel

You can make a variable accessible in multiple views using one of these methods for example:

AppServiceProvider ( reference: https://laravel.com/docs/5.6/providers ) with ViewComposer ( reference: https://laravel.com/docs/master/views#view-composers )

You'll need to add to your ServiceProvider boot() method something similar to this:

public function boot()
{
View::share('variable_name', 'some_value_here');
}

Or inside a controller:

public function __construct() {
$something = 'just a test';
View::share('something', $something);
}

Laravel: can't pass variable to the view

The problem is for index method where you display home.index This file include home.main and in this file you use $batteries variable.

You should not include home.main template here or add extra checking if $batteries are set:

@if (isset($batteries))
<ul class="list-group">
@foreach ($batteries as $battery)
<li class="list-group-item">{{{ $battery->name }}}</li>
@endforeach
</ul>
@endif

To display name of $battery, you should also use {{{ $battery->name }}} and not just $battery->name

EDIT

The problem is your route, it should be to use your controller method:

Route::get('/', ['uses' => 'HomeController@index']);

and now you can change your index method into:

$batteries = Battery::all();
$view = View::make('home.main', compact('batteries'))->render();
return Response::json(['view' => $view]);


Related Topics



Leave a reply



Submit