why show undefined variable when pass frontend - laravel

When I pass a variable in the fronted site it shows that the variable is undefined: why does this happen? I've also tried using compact but it does not work for me. Is there any way to solve the problem?
HomeController file code:
public function index()
{
$category =DB::table('categories')->where('status', 1)->get();
$brand =DB::table('brands')->where('status', 1)->get();
$home=view('home.main_content');
return view('home')->with('home', $home)->with('category', $category);
}
Fronted code using a foreach loop:
#foreach($category as $result)
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">{{$result->name}}</h4>
</div>
</div>
#endforeach
Error:
Undefined variable: category (View: C:\xampp\htdocs\laravelblog\resources\views\home\sidebar_left.blade.php)

Your view is called home and the view in which you are trying to use the variable is called sidebar_left.
So if you use the blade #include directive to load that view you will need to pass the variable to that view.
You can do that like this:
#include('sidebar_left', ['category' => $category])
Then you will be able to use it.

Related

how can I resolve this while working with for else loop and compact function in Laravel?

I'm using laravel version 7.30.1. it shows the error undefined variable $services.
code for home controller.
class HelloController extends Controller
{
public function services()
{
$services = [
'service 1',
'service 2'
];
return view('/services',compact('services'));
}
}
code for blade file.
#section('content')
<H1>welcome to laravel 6 from services</H1>
<ul>
#forelse($services as $service)
<li>{{ $service }}</li>
#empty
<li>this list is empty</li>
#endforelse
</ul>
#endsection
code for route file
route::view('/services','services');
route::view('/services','services');
here your only loading view not controller that's why your data is not pass in to view
use get req. which will be in controller then form controller load view with data
Route::get('services',[\App\Http\Controllers\HelloController::class, 'services']);

i am trying to show single data but it is showing" Trying to get property 'id' of non-object"

**after clicking "read more" i want to show a single post**
<a href="{{ URL::to('single/blog/'.$post->id) }}" class="btn btn-primary
float-right">Read More →</a>
** **the route is****
Route::get('single/blog/{id}','Web\Site\HomeController#show');
**the controller is**
public function show($id)
{
$posts = Post::findOrFail($id);
return view('site.home.singleblog',compact('posts'));
}
****the single section is****
#foreach($posts as $post)
<img class="img-responsive" src="{{asset("uploads/posts/$post->id/image/$post->image") }}" alt=""
{{ $post->name }}
{{$post->description}}
#endforeach
Your variable $posts is a single Post instance from Post::findOrFail($id) (where $id is coming from a route parameter, so a single value). You don't want to be iterating an instance of a Model. Use it in your view like a single model instance not a collection.
public function show($id)
{
view('site.home.singleblog', [
'post' => Post::findOrFail($id),
]);
}
Then in the view just remove the #foreach and #endforeach.
Try this:
{{ $post['name'] }} {{$post['description']}}
If you fetched $posts successfully, it should work. The error says $post is not an object but you are using object syntax to get value by key. So use array syntax.

Undefined variable: professors

In the show function in my controller I passed the variable $professors
In the controller method I am passing the variable like this -
public function show(Professor $professor)
{
$professors = Professor::all();
return view('professor.show',compact('professors'));
}
I am trying to display the professor name in the show.blade.php like this
<div class="form-group">
<div class="row">
<strong class="col-sm-2">Name:</strong>
{{$professors->name}}
</div>
</div>
I'm getting this error-
Property [name] does not exist on this collection instance.
Where am I doing wrong ?
I found the solution
public function show(Professor $professor)
{
$professors = Professor::find($professor->id);
return view('professor.show',compact('professors'));
}
I forgot the find($professor->id); works for me now guys

The result of the code is not displayed in the browser and there is no error in laravel

This is postController.php code:
public function index()
{
$posts = post::orderBy('created_at', 'DESC')->get();
return view('posts.index', compact('posts'));
}
Also I make Route ( web.php ):
Route::resource('post','PostController');
This is view ( index.blade.php ) code:
#section('content')
#foreach($posts as $post)
<div class="panel">
<div class="panel-heading">
<h3>{{ $post -> title }}</h3>
</div>
<div class="panel-body">
{{ $post -> body }}
</div>
</div>
#endforeach
#endsection
After all of that I cannot find the error why my result doesn't display from data base
This tables should be displayed from database
When I try another way the result was displayed u can see that when I write at postController.php code:
public function index()
{
$posts = post::orderBy('created_at', 'DESC')->get();
return $posts;
}
It showed from data base but there is also problem here ! Why the code is not displayed in a neat way in browser
try print post variable on post.index.blade using
{{dd($posts)}}. if your getting $posts variable on blade than everything fines.if not than dd("some meassage") into your index() to check if your index method actually get call.
if your index() method does not print dd("some message") than in terminal type php artisan route:list to check if your routes is prefix with something.

Building drop-down-list in Laravel (without models)

I am trying to make a drop down list in Laravel with the select options being values from my database and I have some issues. In other tutorials in this site, doing drop down list inquires building the models for your database. I have not created model classes and I do not intend to.
Routes.php
Route::get("/user/charname", array(
'as' => 'profile-character',
'uses' => 'ProfileController#dropDownList'
));
ProfileController.php
class ProfileController extends BaseController
{
public function dropDownList()
{
$list = DB::table('characters')->where('char_id', '128')->pluck('char_name');
return View::make('layout.profile')->with('character_options',$list);
}
}
In the profile.blade.php (view)
<div class="selected_char">
<form action="{{ URL::route('profile-character') }}" method="post">
<li>
{{ Form::select('character_options', $list ,Input::old('character_options')) }}
</li>
</form>
</div>
By doing this it says that $list from my view is undefined. Where do I define $list in the view and how will I carry $list from my Controller to the View because this line doesn't seem to do it's job
return View::make('layout.profile')->with('character_options',$list);
You need to use $character_options not $list in the View.
You actually specify that the variable should be 'character_options' in your View::make() call, so you need to refer to it as $character_options.
Additionally, ->lists('char_name') is better than using ->pluck('char_name') as it'll give you the full list. Pluck just returns the first item it finds.
Additionally to that, using ->lists('char_name', 'id') gets you the list keyed by the id column, which would be useful if you were to use this list to determine IDs for a foreign key field. But no biggie if not.
I have partially solved the issues by doing this in my view:
{{ Form::select('list', DB::table('characters')->where('char_id', '128')->lists('char_name') ,Input::old('list')) }}
Personally I do not see it as a viable solution, but a temporary one
{{ Form::select('character_options', $character_options ,Input::old('character_options')) }}
Error: undefined variable character_options
{{ Form::select('character_options', $list ,Input::old('character_options')) }}
Error: undefined variable list
Maybe I do not understand how i send the variable from the Controller to the View.
You need to use lists as mentioned by #alexrussel. Do note that View::make('layout.profile')->with('character_options',$list);, here you are passing the variable $character_options with the value contained in the $list and $list will no longer be available to the view.
ProfileController.php
<?php
class ProfileController extends BaseController
{
public function dropDownList()
{
$list = DB::table('characters')->where('char_id', '128')->lists('char_name');
return View::make('layout.profile')->with('character_options',$list);
}
}
profile.blade.php
<div class="selected_char">
<form action="{{ URL::route('profile-character') }}" method="post">
<li>
{{ Form::select('character_options', $character_options ,Input::old('character_options')) }}
</li>
</form>

Resources