I cant send a variable to Blade in Laravel 5.1 - php

I'm trying to send a variable to blade view, but throw this error:
Undefined variable: data (View: D:\wamp\www\tienda\resources\views\cliente.blade.php)
This is my Route:
Route::resource('cliente','ClienteController');
This is my Controller Cliente:
public function index(){
$data = Cliente::all();
return view('cliente',compact($data));
}
And my Blade:
#foreach ($data as $user)
<tr>
<td>{{$user->nombre}}</td>
</tr>
#endforeach
What I'm doing wrong?
Additionally, if a try to do for example this
Controller Cliente:
public function index(){
return view('cliente', ['name' => 'James']);
}
And Blade:
{{$name}}
That yes work... Only the variables and arrays, doesnt work.

Try this on your Controller:
public function index(){
$data = Cliente::all();
return view('cliente',compact('data'));
}
From the compact documentation: "Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; compact() handles it recursively. "

you can try this way
public function index(){
$data['data'] = Cliente::all();
return view('cliente', $data);
}
Then you can catch it in blade as like this
#foreach ($data as $user)
<tr>
<td>{{$user->nombre}}</td>
</tr>
#endforeach

Related

Laravel get the data and display it in the blade html failed

I am trying to get data from the database and simply display it in the view
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests', compact($messages));
}
The view
#foreach ($messages as $message)
<h1>{{ $message->first_name }}</h1>
#endforeach
But I am getting this error
compact(): Argument #1 must be string or array of strings, Illuminate\Database\Eloquent\Collection given
The PHP method compact() is a little tricky with its syntax. I make this same mistake all the time.
Change:
return view('dashboard/projects-interests', compact($messages));
to:
return view('dashboard/projects-interests', compact('messages'));
compact() looks for a string representation of the variable.
There are multiple ways you could write this.
using compact where you pass strings representing the variable
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests', compact('messages'));
}
using ->with
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests')->with(['messages' => $messages]);
}
With using laravel magic
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests')->withMessages($messages);
}
Personally, I prefer this form as it avoids pointless variables
public function index()
{
return view('dashboard/projects-interests')
->withMessages(ProjectInterestedMessages::get());
}
You don't need $
return view('dashboard/projects-interests', compact('messages'));

Invalid argument supplied for foreach() in view file

may i know why this code showing invalid argument supplied for foreach() and how i should correct it ?
#foreach($project_details as $project) {
#foreach($project->projectusers as $users) {
{{ $user->name }}
}
#endforeach }
#endforeach
Controller
public function index()
{
$this->project = Project::with('projectusers')->get();
return view('projects.index', [
'project_details' => $this->project
]);
}
projectusers is a relationship ? Do you use the with() function in your Eloquent query to get that relationship within the collection ?
In addition, in your foreach code, you use the plural for users ($project->projectusers as $users), and then in the double brackets, you use the singular {{ $user->name }}
Remove the 's' from users in the foreach and it should work.
I would suggest reading up on foreach() a little more:
http://php.net/manual/en/control-structures.foreach.php
But if this is your entire code, you have not set any array values or assigned any values from a database or anything
$arr = array(1, 2, 3, 4);
Using the website above you can see it is quite simple to fill an array yourself depending on how many values you need to be added.
Just use compact as a second argument of your view()
public function index()
{
$projects = Project::with('projectusers')->get();
return view('projects.index', compact('projects'));
}
Then in your projects.index just call :
#foreach($projects as $project) {
#foreach($project->projectusers as $users) {
{{ $user->name }}
}
#endforeach }
#endforeach
Try this one:
public function index() {
$projects = Project::whereHas('user')->get();
return view('projects.index', compact('projects''));
in your blade:
#foreach($projects as $project)
{{$project->user->name}}
#endforeach

error from laravel's foreach loop

Getting error message from Laravel's foreach loop:
Invalid argument supplied for foreach()
My foreach loop is :
#foreach ($task as $tasks)
<td>{{$tasks->name}}</td>
#endforeach
My controller is:
public function show($id){
$task = Task::find($id);
return view('tasks.show')->withTask($task);
;
}
You are giving a wrong data type to the foreach loop.
A foreach requires an array or a Collection.
The statement
$task = Task::find($id);
will give you a Task model (not a Collection).
Hence you cannot iterate over it.
Also, since you are trying to display the Task resource (assuming this as you are calling the controller's show() function), you don't need to iterate over the model - as you will be displaying a single entity's attributes.
Just do this
Controller:
public function show($id){
$task = Task::find($id);
return view('tasks.show', compact('task'));
}
In the view:
Name: {{ $task->name }}
// other attributes here
I Never seen to set variable like you, try this
return view('tasks.show')->with('task', $task);
Reverse the foreach variables
#foreach ($tasks as $task)
<td> {{ $task->name }} </td>
#endforeach
You should write this. This will solve your problem
Your Controller
public function show($id){
$task = Task::findOrFail($id);
return view('tasks.show', compact('task'));
}
As it gets only one value so no need to use foreach. You should write this
<td>{{$tasks->name}}</td>
The findOrFail method return only one value. So No need to loop it.
Hopefully this will solve your problem.

Undefined variable: stations in view

I have undefined variable and it doesn't call variable.
I get missing argument 1 error when I try accessing a page. This is my code.
Part of the view:
#foreach($stations as $station)
<span> {{ $stations->station }} </span>
#endforeach
Controller:
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
return view('configuration.configuration', $stations);
}
Route:
Route::get('configuration/', 'ConfigurationController#show');
Try with below code in controller
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
$data['stations'] = $stations;
return view('configuration.configuration', $data);
}
View
#foreach($stations as $station)
<span> {{ $station->station }} </span>
#endforeach
You are directly passing the array value to view. It will not work like that. You have to assign the values to an array index and then call that index like $index_name in view. Then it will give you the desired output
or
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
return view('configuration.configuration')->with(stations);
}
if you dont want to neft your $station into unused $data
Try with below code in controller:-
public function show($id)
{
$stations = DB::table('stations')->pluck('station');
return view('configuration.configuration', compact('stations'));
}

Using where with variable

I have this controller:
public function category($name)
{
$users = User::all()->where('category','$name');
return View('index',['users'=>$users]);
}
returning to the view:
You chose <mark> {{$users[0]->category}}</mark>
gives me error:
undefined offset:0
Oh boy. The problem is here '$name', change it to simple $name (without quotes).
If You want more explanations whats wrong with Your code (not related to this problem), just say it.
try this
Controller
public function category($name)
{
$user = User::where('category',$name)->select('category')->first();
return View('index')->with('user', $user);
}
VIEW
You chose <mark> {{ $user }}</mark>
Try this:
Controller :
public function category($name)
{
$users=User::where('category',$name)->get();
return View('index',['users'=>$users]);
}
View :
#foreach($users as $user) // to access all records
{{ $user->category }}
#endforeach
{{$users[0]->category}} // to access first record

Categories