hello guys im having a problem with passing variable from my controller to views, as it does not identify its variable, here is my code:
RegisterController.php
use App\angkatan;
public function index()
{
$select = Angkatan::all();
return view('/auth/register')->with('name', $select);
}
My Route
web.php
Route::get('register', 'RegisterController#index');
and my view
register
#foreach($name as $ps)
#endforeach
the error say
Undefined variable: name (0)
im very thankful if anyone can help
You are just passing the wrong way $select variable to your view.
when you use Illuminate\View\View::with method you should pass an associative array which as key => value pairs
return view('/auth/register')->with(['name' => $select]);
You can also use compact which allows to pass variable which are accessible inside of the scope of your controller to the view and the string passed as argument to that function will be the name of variable accessible inside of the view file
$select = Angkatan::all();
return view('/auth/register', compact('select'));
You can not pass the variable in this way to the view. You have to pass an array in the second parameter of the with() method - it should be something like this:
return view('greeting', ['name' => 'James']);
return view('/auth/register', ['name' => $select]);
you can pass a second massive parameter to the view,
and also in with method, you need to pass massive as I remember.
Related
I'm trying to follow a small laravel tutorial.
In my application I have a controller called, HomeController.
In my HomeController, I have the followinf index function
public function index()
{
try {
//get autheticated users selected organisation
$organisation = $this->getSelectedOrganisationByUser(Auth::user());
//set application timezone
$this->setApplicationTimezone($organisation);
$this->setSiteConnection($organisation);
$this->setGAConnection($organisation);
if ($organisation->disabled) {
Auth::logout();
return redirect(route('login'));
}
//sales today
$sum='23';
return view('dashboard.index')
->with('organisation',$organisation,'sum',$sum);
} catch (\Throwable $exception) {
\Sentry\captureException($exception);
}
}
Here I'm trying to send this $sum value to my blade and display it. But, Every time i tried to display this value on my blade I get $sum is undefined error.
I tried to echo this on my blade as follows,
{{ $sum }}
What changes should I make, in order to display that on my blade.
I tried hard, but yet to figure out a solution
You need to pass the variables as array. Try this:
return view('dashboard.index', ['organisation' => $organisation, 'sum' => $sum]);
or another way is by using compact() like this
return view('dashboard.index', compact('organisation', 'sum'));
The method with() from /Illuminate/View/View.php accepts only two parameters.
To send multiple variable, you can
//Call with() for each variable
return view('dashboard.index')
->with('organisation',$organisation)
->with('sum',$sum);
//use an array as second parameter of view
return view('dashboard.index', ['organisation' => $organisation,'sum' => $sum]);
//use compact to create the array using the variable name
return view('dashboard.index', compact('organisation', 'sum'));
//use an array as first parameter in with (using compact or not)
return view('dashboard.index')->with(compact('organisation', 'sum'));
Im passing data to my blade view with return View::make('blog', $posts); and in my blade view I'm trying to run an #foreach ($posts as $post) I end up with an error saying that $posts isn't defined.
My question is how would the $posts array be called?
You can pass data to the view using the with method.
return View::make('blog')->with('posts', $posts);
As of Laravel 5, the View::make() facade method from the 2013 accepted answer has been deprecated. Passing data to the view is now done using the view() helper function like this:
return view("blog", ["posts"=>$posts]);
Or, the same thing:
return view("blog", compact("posts"));
This can be combined with the with() method if desired:
return view("blog", compact("posts"))->with("message", "Comment posted");
Documentation is available here.
If you want to pass just one variable to view, you may use
In Controller
return view('blog')->withTitle('Laravel Magic method.');
In view
<div>
Post title is {{$title}}.
</div>
If you want to pass multiple variables to view, you may use
In Controller
return view('blog')->withTitle('Laravel magic method')->withAuthor('Mister Tandon');
In view
<div>
Post title is {{$title}}.Author Name is {{$author}}
</div>
You can also pass an array as the second argument after the view template name, instead of stringing together a bunch of ->with() methods.
return View::make('blog', array('posts' => $posts));
Or, if you're using PHP 5.4 or better you can use the much nicer "short" array syntax:
return View::make('blog', ['posts' => $posts]);
This is useful if you want to compute the array elsewhere. For instance if you have a bunch of variables that every controller needs to pass to the view, and you want to combine this with an array of variables that is unique to each particular controller (using array_merge, for instance), you might compute $variables (which contains an array!):
return View::make('blog', $variables);
(I did this off the top of my head: let me know if a syntax error slipped in...)
Tips1:
Using With(), This is a best practice
return view('about')->withName('Author Willson')->withJob('Writer');
return View::make('home')->with(compact('about'))
return View::make('home')->with('comments', $comments);
Tips2:
Using compact()
return view(about, compact('post1','post2'));
Tips3:
Using Second Parameters:
return view("about", ["comments"=>$posts]);
controller:
use App\your_model_name;
funtion index()
{
$post = your_model_name::all();
return view('index')->with('this_will_be_used_as_variable_in_views',$post);
}
index:
<h1> posts</h1>
#if(count($this_will_be_used_as_variable_in_views)>0)
#foreach($this_will_be_used_as_variable_in_views as $any_variable)
<ul>
<p> {{$any_variable->enter_table_field}} </p>
<p> {{$any_variable->created_at}} </p>
</ul>
#endforeach
#else
<p> empty </p>
#endif
Hope this helps! :)
use TCG\Voyager\Models\Jobtype;
class FormController extends Controller
{
public function index()
{
$category = Jobtype::all();
return view('contact', compact('category'));
}
}
use your model
assign it to a variabel
pass the object to the view
Here is an example:
You can also do the same thing in another way,
If you are using PHP 5.5 or latest one then you can do it as follow,
Controller:
return view(index, compact('data1','data2')); //as many as you want to pass
View:
<div>
You can access {{$data1}}. [if it is variable]
</div>
#foreach($data1 as $d1)
<div>
You can access {{$d1}}. [if it is array]
</div>
#endforeach
Same way you can access all variable that you have passed in compact function.
Hope it helps :)
You can also do like this
$arr_view_data['var1'] = $value1;
$arr_view_data['var2'] = $value2;
$arr_view_data['var3'] = $value3;
return view('your_viewname_here',$arr_view_data);
And you access this variable to view as $var1,$var2,$var3
You can pass data to the view using the with method.
return view('greeting', ['name' => 'James']);
You can pass your table data to view using compact.
$users = RoleModel::get();
return view('super-admin',compact('users'));
You can also write for passing multiple data from your controller to a view
return \View::make('myHome')
->with(compact('project'))
->with(['hello'=>$hello])
->with(['hello2'=>$hello2])
->with(['hello3'=>$hello3]);
OK all the answers tell you how to pass data to the view but no one explains how to read it in the view .
if you use :
//Routes/web.php
...
Route::get('/user', function () {
return view('profile', [
'variable1' => 'value1' ,
'variable2'=> 'value2' , // add as much as you want
]);
});
To read these variables use in your views (in this example it's profile.blade.php file):
#if($variable1)
<p> variable1 = {{ $variable1 }}</p>
#endif
Laravel does all the necessary work and creates $variable1 for you .
For example, you have an array with your data.
$array1 = $data;
$array2 = $data;
From your controller you can pass this variable using compact. You can pass as many array as you want.
return view('data.index',compact('array1','array2'));
Here data is a folder in view and index is the file with extension index.blade.php
In view you can call the arrays like this
#foreach ($array1 as $something)
// some operation
#endforeach
For any one thinking it is really tedious in the case where you have tons of variables to pass to a view or you want the variables to be accessible to many views at the same, here is another way
In the controller, you define the variables you want to pass as global and you attribute the values to these variables.
Example global $variable; $variable = 1;
And now in the view, at the top, simply do
<?php global $variable;?>
Then you can now call your variable from any where in the view for example
{{$variable}}
hope this helps someone.
I have an action method which has in its body multiple Variables something like this :-
$bus=Bus::all();
$user=User::all();
$employer=employer::all();
what am doing to return those variables objects to the view is using code like this
return view('create')->with(compact('bus', $bus))->with(compact('user', $user))->with(compact('employer', $employer));
is there a way a method or something to return this objects at once without the need for all of this code some thing like
return view('create')->($user,$bus,$emp);
just example of what i want .
you can create an array like this
$data['bus']=Bus::all();
$data['user']=User::all();
$data['employer']=employer::all();
return view('create',['data'=>$data]);
You have few options here.
First approach is:
Use with() and pass an array explicitly:
return view('create')->with('data', [
'bus' => $bus,
'user' => $user,
'employer' => $employer
]);
Use with() and list your variables in compact():
return view('create')->with('data', compact('bus','user','employer'));
Your objects would be accessible in the view like an associative array $data with keys 'bus','user' and 'employer'.
Another approach is:
Pass an array as a second argument to view() function:
return view('create', [
'bus' => $bus,
'user' => $user,
'employer' => $employer
]);
Pass an compact() function (which would create an array like in the previous example behind the scenes) as a second argument to view() function:
return view('create', compact('bus','user','employer'));
If you use it this manner you will get three variables $bus, $user and $employer accessible in your view.
You can do pretty much what you're after with
return view('create', compact('bus', 'user', 'employer'));
or do the $data option above
In my blade I tried to pass the ID in an action link.
test
I get the passed id like this in my controller:
public function edit()
{
$input = Input::get();
dd($input);
}
but the output of dd() is:
array:1 [▼
5 => ""
]
But why is it an array? I only want the number but I don't see why laravel gives me the input as an array.
Input::get() gives you the whole input array. Use the specific key to get a specific value, for instance Input::get('id'). But your action() call appears to just append the id without a key. Try e.g. action('DomainController#edit', ['id' => $domain->id]).
That said, you may want to look at named routes and route model binding - makes the whole thing easier.
I think that You should pass $id as a parameter in edit:
public function edit($id)
{
dd($id);
}
Im passing data to my blade view with return View::make('blog', $posts); and in my blade view I'm trying to run an #foreach ($posts as $post) I end up with an error saying that $posts isn't defined.
My question is how would the $posts array be called?
You can pass data to the view using the with method.
return View::make('blog')->with('posts', $posts);
As of Laravel 5, the View::make() facade method from the 2013 accepted answer has been deprecated. Passing data to the view is now done using the view() helper function like this:
return view("blog", ["posts"=>$posts]);
Or, the same thing:
return view("blog", compact("posts"));
This can be combined with the with() method if desired:
return view("blog", compact("posts"))->with("message", "Comment posted");
Documentation is available here.
If you want to pass just one variable to view, you may use
In Controller
return view('blog')->withTitle('Laravel Magic method.');
In view
<div>
Post title is {{$title}}.
</div>
If you want to pass multiple variables to view, you may use
In Controller
return view('blog')->withTitle('Laravel magic method')->withAuthor('Mister Tandon');
In view
<div>
Post title is {{$title}}.Author Name is {{$author}}
</div>
You can also pass an array as the second argument after the view template name, instead of stringing together a bunch of ->with() methods.
return View::make('blog', array('posts' => $posts));
Or, if you're using PHP 5.4 or better you can use the much nicer "short" array syntax:
return View::make('blog', ['posts' => $posts]);
This is useful if you want to compute the array elsewhere. For instance if you have a bunch of variables that every controller needs to pass to the view, and you want to combine this with an array of variables that is unique to each particular controller (using array_merge, for instance), you might compute $variables (which contains an array!):
return View::make('blog', $variables);
(I did this off the top of my head: let me know if a syntax error slipped in...)
Tips1:
Using With(), This is a best practice
return view('about')->withName('Author Willson')->withJob('Writer');
return View::make('home')->with(compact('about'))
return View::make('home')->with('comments', $comments);
Tips2:
Using compact()
return view(about, compact('post1','post2'));
Tips3:
Using Second Parameters:
return view("about", ["comments"=>$posts]);
controller:
use App\your_model_name;
funtion index()
{
$post = your_model_name::all();
return view('index')->with('this_will_be_used_as_variable_in_views',$post);
}
index:
<h1> posts</h1>
#if(count($this_will_be_used_as_variable_in_views)>0)
#foreach($this_will_be_used_as_variable_in_views as $any_variable)
<ul>
<p> {{$any_variable->enter_table_field}} </p>
<p> {{$any_variable->created_at}} </p>
</ul>
#endforeach
#else
<p> empty </p>
#endif
Hope this helps! :)
use TCG\Voyager\Models\Jobtype;
class FormController extends Controller
{
public function index()
{
$category = Jobtype::all();
return view('contact', compact('category'));
}
}
use your model
assign it to a variabel
pass the object to the view
Here is an example:
You can also do the same thing in another way,
If you are using PHP 5.5 or latest one then you can do it as follow,
Controller:
return view(index, compact('data1','data2')); //as many as you want to pass
View:
<div>
You can access {{$data1}}. [if it is variable]
</div>
#foreach($data1 as $d1)
<div>
You can access {{$d1}}. [if it is array]
</div>
#endforeach
Same way you can access all variable that you have passed in compact function.
Hope it helps :)
You can also do like this
$arr_view_data['var1'] = $value1;
$arr_view_data['var2'] = $value2;
$arr_view_data['var3'] = $value3;
return view('your_viewname_here',$arr_view_data);
And you access this variable to view as $var1,$var2,$var3
You can pass data to the view using the with method.
return view('greeting', ['name' => 'James']);
You can pass your table data to view using compact.
$users = RoleModel::get();
return view('super-admin',compact('users'));
You can also write for passing multiple data from your controller to a view
return \View::make('myHome')
->with(compact('project'))
->with(['hello'=>$hello])
->with(['hello2'=>$hello2])
->with(['hello3'=>$hello3]);
OK all the answers tell you how to pass data to the view but no one explains how to read it in the view .
if you use :
//Routes/web.php
...
Route::get('/user', function () {
return view('profile', [
'variable1' => 'value1' ,
'variable2'=> 'value2' , // add as much as you want
]);
});
To read these variables use in your views (in this example it's profile.blade.php file):
#if($variable1)
<p> variable1 = {{ $variable1 }}</p>
#endif
Laravel does all the necessary work and creates $variable1 for you .
For example, you have an array with your data.
$array1 = $data;
$array2 = $data;
From your controller you can pass this variable using compact. You can pass as many array as you want.
return view('data.index',compact('array1','array2'));
Here data is a folder in view and index is the file with extension index.blade.php
In view you can call the arrays like this
#foreach ($array1 as $something)
// some operation
#endforeach
For any one thinking it is really tedious in the case where you have tons of variables to pass to a view or you want the variables to be accessible to many views at the same, here is another way
In the controller, you define the variables you want to pass as global and you attribute the values to these variables.
Example global $variable; $variable = 1;
And now in the view, at the top, simply do
<?php global $variable;?>
Then you can now call your variable from any where in the view for example
{{$variable}}
hope this helps someone.