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'));
}
Related
I tried to create show function for main category but there is an error showing that variable categories is undefined. can help to solve it?
controller:
public function show($id)
{
$categories = Categories::find($id);
return view('Admin.categories.show',compact('maincategory'));
}
blade:
{{ $categories->main_cate }}
Route:
Route::get('/admin/show/{id?}',
['uses'=>'AdminCategoriesController#show','as'=>'Admin.categories.show']);
You do not pass the $catgories variable to the view. Do this:
return view('Admin.categories.show', compact('categories'));
I want to foreach some data but it says it doesn't know the variable:
The error I get:
ErrorException in dbda158712a631f22ffd888cd244c74e60f3a433.php line 51:
Undefined variable: albums (View: /var/www/clients/client2/web2/web/resources/views/album.blade.php)
Here is my code:
Album.blade.php
#foreach($albums as $others)
<option value="{{$others->id}}">{{$others->name}}</option>
#endforeach
My album controller function
public function getAlbum($id)
{
$album = Album::with('Photos')->find($id);
return View::make('album')
->with('album',$album);
}
public function getList()
{
$albums = Album::with('Photos')->get();
return View::make('index')
->with('albums',$albums);
}
You are passing album variable with view Album.blade.php, which is single object, not array of object so you can't iterate in a loop.
I think you are doing a mistake.
You want to do foreach in index.blade.php, because here you are passing the albums variable.
or
you need to return view album.blade.php in your getList function.
Try This Code.
public function getAlbum($id)
{
$albums=albums::with('Photos')->get();
$album=albums::with('Photos')->find($id);
return view('album',compact('albums','album'));
}
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
I have the following relationship between my tables
In my Models i have the following code:
Location Model:
public function member() {
return $this->belongsTo('App\Member','member_id');
}
Member Model:
public function locations(){
return $this->hasMany('App\Location','member_id');
}
Now in my Location controller I created the following function.
public function getFinalData(){
$locations = Location::with('member')->whereNotNull('member_id')->get();
return view('locations.final-list',['locations'=>$locations]);
}
In my blade template however, I am unable to iterate through the member properties
<ul>
#foreach($locations as $location)
<li>{{$location->id}}</li>
<li>
#foreach($location->member as $member)
{{$member->id}}
#endforeach
</li>
#endforeach
</ul>
This gives me the following error:
Trying to get property of non-object (View:locations/final-list.blade.php)
update: The result i'm trying to achieve corresponds to this query
SELECT locations.location_id,locations.name,members.first_name, members.last_name , locations.meters
FROM locations,members
WHERE locations.member_id = members.id
Update 2:
So i tried to access a single location with a single attached to it and that works perfectly
public function getFinalData(){
//$locations = Location::with('member')->whereNotNull('member_id')->get();
$location = Location::find(0);
return view('locations.final-list',['location'=>$location]);
}
in final-list
$location->member->first_name
**Update 3 **
Tinker Output for one record :
In your Location model, rewrite below function:-
public function member()
{
return $this->belongsTo('App\Member', 'id');
}
Get all locations with member detail as below:-
$locations = Location::with('member')->get();
Hope it will work for you :-)
For showing related tables information in blade is as shown:
Model Relations
Location Model:
public function member() {
return $this->belongsTo('App\Member','member_id');
}
Member Model:
public function locations(){
return $this->hasMany('App\Location','member_id');
}
Get all locations in your controller
$locations = Location::all();
return view('locations.final-list', compact('locations'));
Or
$locations = Location::orderBy('id')->get();
return view('locations.final-list', compact('locations'));
Inside your view(blade) write the following code:
<div>
#if($locations)
#foreach($locations AS $location)
<div>{{ $location->id }}</div>
#if($location->member)
#foreach($location->member AS $member)
<div>
{{ $member->id }}
</div>
#endforeach
#endif
#endforeach
#endif
</div>
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