Using where with variable - php

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

Related

How do I pass variables from the controller to the view in laravel react?

I'm using laravel in my backend and react as my frontend. When I try to pass variables from my controller to the view with this:
public function index()
{
return view('welcome')->with('name', 'San Juan Vacation');
}
And want to retrieve it in my welcome blade file with this:
<p>{{ $name }}</p>
It doesn't work for some reason.
It says undefined variable $name. Any idea what might solve this issue?
Pass variable Controller to view
public function index()
{
$name = 'San Juan Vacation';
$color = 'red';
// .....
return view('welcome', compact('name', 'color'));
}
If you want to pass with, that time :
return view('welcome')->with(['name'=>$name,'color'=>$color]);
Write in welcome.blade.php
<p>{{ $name }}</p>
I hope this work for your case.
To send data from a controller to the view use this code: (you were close)
public function index()
{
$name = 'San Juan Vacation';
$color = 'red';
// .....
return View::make('welcome', compact('name', 'color'));
}
<p>{{ $name }}</p>
Modify you code as below
public function index()
{
$name = "San Juan Vacation";
return view('welcome')->with(['name'=>$name]);
}
Note: Avoid passing hard coded value to blade file, set variable for each.

Laravel Undefined Variable in View from database

i tried to show some data from database to my view. But i get undefined variable error.
Route::get('pages/tab-content-games', 'Admin\TournamentCrudController#get_data');
and this is my function
public function get_data(){
$data['data'] = \DB::table('tournament')->get();
return view('pages.tab-content-games', ['data' => $data]);
}
in tab-content-games.blade.php
i just print the variable
{{ print_r($data) }}
can someone helped me, what part i'm doing wrong. thankyou
Please try with this return view('pages.tab-content-games', compact('data'));
Try this:
i think you have to use rural name as your database name tournaments instead of tournament. also you can use compact.
public function get_data(){
$data = \DB::table('tournaments')->get();
return view('pages.tab-content-games', compact('data');
}
PS: consider using model in your controller instead of using DB for simple functions.
use App\Tournament;
public function get_data(){
$data = Tournament::all();
return view('pages.tab-content-games', compact('data')
}
Try this:
return view('pages.tab-content-games')->with('data', $data);

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'));
}

Cant show related variable in Laravel blade

I have trouble with showing related variable in laravel blade
public function GetAll()
{
$news=DB::table('news')->get();
return View('index',['news'=>$news]);
}
In View:
#foreach($news as $new)
...
{{$new->comments()->count()}} Comments
...
#endforeach
Its also doesnt work for any variables of object but working good for first item:
public function Count()
{
$news=News::find(1);
echo $news->comments()->count();
}
public function GetAll()
{
$news = News::with('comments')->get();
return View('index',['news'=>$news]);
}
Use ORM instead of DB.

I cant send a variable to Blade in Laravel 5.1

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

Categories