Undefined variable: categories in blade file - php

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

Related

How to access class variables from within blade template

My controller index file looks like this:
class KJVController extends Controller
{
public $book = "Genesis";
public $chapter = 1;
public function index() {
$results = KJV::where('book', '=', $this->book)->where('chapter', '=', $this->chapter)->get();
return view("bible", compact('results'));
}
}
And in my blade template I want to be able to show the variables above ($book,$chapter).
I tried:
{{ KJVController::book }}
{{ KJVController->book }}
I can't seem to access it. How can I display this information in my blade template?
You need to pass these variables to the view first:
return view("bible", ['results' => $results, 'book' => $this->book, 'chapter' => $this->chapter]);
In the bible view:
{{ $book }}
Or you can just do this:
{{ app('App\Controllers\KJVController')->book }}
There's nothing wrong with your controller query. You just aren't returning the variable. You're trying to return the controller and as you're running ->get(), it returns an array. Therefore, you need to do this within the blade:
#foreach ($results as $book)
{{$book->book}}{{$book->chapter}}
#endforeach
Blade documentation

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

Showing related table information in blade template laravel

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 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