How can I fix undefined variable problem? - php

My $links and $sliderimages variables work great. The $klas variable has been made by the same way like the first two I said, and for some reason it says that it is undefined variable.
First $klas was meant for another view, however I decided to test it out on my home page view where the other 2 variables were working were, but when i have put the $klas the same error occurs.
Route::get('/', function () {
$sliderimages = App\Sliderimage::all();
$links = App\Link::all();
$klas = App\Kla::all();
return view('home', compact('sliderimages'), compact('links'), compact('klas'));
});
That is on web.php file
#foreach($links as $link)
<div class="col-lg-4">
<img src="{{ Voyager::image( $link->image ) }}" class="rounded-circle" width="140" height="140" style="margin-left:25%">
<h2>"{{$link->title}}"</h2>
<p>"{{$link->text}}"</p>
<p><a class="btn btn-secondary" href="{{$link->slug}}" role="button">За повече информация. »</a></p>
</div>
#endforeach
</div>
#foreach($klas as $kla)
<p>"{{$kla->klastitle}}"</p>
#endforeach
That is on home.blade . php
The error is:
Undefined variable: klas (View: D:\xampp\htdocs\koko\diplomnata\resources\views\home.blade.php)

Change this:
return view('home', compact('sliderimages'), compact('links'), compact('klas'));
To this:
return view('home', compact('sliderimages', 'links', 'klas'));
Or for more longer but IMO more readable syntax:
return view('home')
->with('sliderimages', $sliderImages)
->with('links', $links)
->with('klas', $klas);
https://laravel.com/docs/master/views#passing-data-to-views

Related

Undefined variable $data

error:
Undefined variable $data
#foreach ($data as $d)
<div class="col-md-4 mb-3">
<div class="bg-welcome3">
<img src="{{ $d->image }}" alt="">
</div>
</div>
#endforeach
my route:
Route::resource('welcome', WelcomeController::class);
WelcomeController:
public function index()
{
$data = Product::all();
return view('welcome',compact('data'));
}
I want to display data in view('welcome') but the data is undefine, even though the routes and controllers that I use are correct
try this instead Check and make sure that welcome.blade.php is present in inside [yourprojectname]\resources\views

Fetching particular data from <table> (using Id) in Laravel

View:
<td>
<div class="template-demo">
<button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt/{$id}') }}'" class="btn btn-outline-info btn-icon-text">
<i class="ti-search btn-icon-append"></i>View
</button>
</td>
Route:
`Route::get('View_PL_Accnt', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt/{$id}');`
Controller:
public function View_PL_Accnt(Request $request){
$id = $request->id;
$data = User::find($id);
return view('dashboards.SupAd.pages.index.View_PL_Accnt', compact (['data', 'id']));
}
View_PL_Accnt.blade.php :
<h3 class="SupAd_name">{{ data['name'] }}</h3>
Error:
Use of undefined constant data - assumed 'data' (this will throw an Error in a future version of PHP) (View: C:\Users\CuatrosMarias\Documents\GitHub\IPS\resources\views\dashboards\SupAd\pages\index\View_PL_Accnt.blade.php)
Error
You need to send variables using with() in your controller
return view('dashboards.SupAd.pages.index.View_PL_Accnt')->with('data',$data)->with('id',$id);
Your View Accnt.blade.php you have used data as constant you need to use it as variable
Eloquent result gives object so you can access the name property of your result object like below
{{ $data->name }}
typo error {{ $data['name'] }} $ is missing at the view
Try this one works fine in my side always..
->with(compact('data', 'id'));
View:
<button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt', [$user->id]) }}'" class="btn btn-outline-info btn-icon-text">
<i class="ti-search btn-icon-append"></i>View
</button>
Route:
Route::get('View_PL_Accnt/{id}', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt');
Controller:
public function View_PL_Accnt($id){
$data = User::find($id);
return view('dashboards.SupAd.pages.index.View_PL_Accnt', compact (['data', 'id']));
View_PL_Accnt.blade.php :
<h3 class="SupAd_name">{{ $data->name }}</h3>

FatalErrorException Call to a member function except() on null

I am building a QA, and when i try to show a question, I am getting this error. FatalErrorException Call to a member function except() on null in web.php line 10
Here is the web.php code
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('questions', 'QuestionsController')->except('show');
Route::get('/questions/{slug}', 'QuestionsController#show')->name('questions.show');
in the QuestionsController.php file the show function is
public function show(Question $question)
{
$question->increment('views');
return view('questions.show', compact('question'));
}
in addition, here is the view part when the question is displayed
<div class="card">
<div class="card-header">
<div class="d-flex align-items-center">
<h>{{ $question->title }} </h1>
<div class="ml-auto">
Back to all Questions
</div>
</div>
</div>
<div class="card-body">
{{ !! $question->body_html !! }}
</div>
</div>
so if my question is clear, how can I get rid of this error? Thank you for your help, Sirs!
Try to below code:
Route::resource('questions', 'QuestionsController', ['except' => ['show']]);
i think so it is because you should use find() or something elese first.
for example you should find question by id or slug or anything, then you can increment the field you want.
for example:
$question = Question::find(5)->increment('views');
or
$question = Question::where('url', '=', 'some value')->increment('views');
Correct your route. by following this sample code. Here is the official document you can check it [https://laravel.com/docs/5.4/controllers#resource-controllers]
Route::resource('questions', 'QuestionsController')->except(['show']);
Route::get('/questions/{slug}', 'QuestionsController#show')->name('questions.show');

Issue with passing value to view in Laravel [duplicate]

This question already has an answer here:
Issue with Laravel orderby
(1 answer)
Closed 5 years ago.
)
I am trying to order my products by price and I keep getting the same message error : Undefined index: title (View: C:\xampp\htdocs\eshop\resources\views\content\item.blade.php)
From what I understood my "item" wasn't added correctly to $data but I can't figure it out what is the correct way.
My Route :
Route::get('shop/{category_url}/sorting-{sort?}', 'ShopController#products');
My view in content.products:
#if($products)
<br><br>
High to low |
Low to high
My item.blade.php:
#extends ('master')
#section('content')
<div class="row">
<div class="col-md-12 text-center">
#if('item')
<h1>{{ $item['title']}}</h1>
<p><img width="500" src="{{ asset ('images/' . $item['image'])}}" </p>
<p>{!! $item['article'] !!}</p>
<p><b>Price on site:</b>{{ $item['price']}}$</p>
<p>
#if(Cart::get($item['id']))
<input disabled="disabled" type="button" value="In Cart!" class="btn btn-success">
#else
<input data-id="{{ $item['id']}}" type="button" value="+ Add to cart" class="btn btn-success add-to-cart">
#endif
Checkout
</p>
#else
<p class="text-center" style="font-size: 18px">No product details ...</p>
#endif
</p>
#endsection
My Controller:
public function products(Request $request, $category_url, $sort= 'ASC'){
Product::getProducts($category_url, self:: $data);
$catsort = Categorie::where('url', '=', $category_url)->first();
$products = Product::where('categorie_id', $catsort->id)->orderBy('price', $sort)->get();
return view('content.products', self::$data ,['products' => $products, 'sort' => $sort]);
}
public function item($category_url, $product_url){
Product::getItem($product_url, self::$data);
return view('content.item', self::$data);
}
My Model:
static public function getProducts($category_url, &$data){
$data['products']=$data['category']=[];
if ($category=Categorie::where('url','=', $category_url)->first()){
$category= $category->toArray();
$data['category']=$category;
$data['title']=$data['title']. ' | ' . $category['title'];
if ($products=Categorie::find( $category['id'])->products){
$data['products']= $products->toArray();
}
}
}
static public function getItem($product_url, &$data) {
$data['item'] = [];
if ($product = Product::where('url', '=', $product_url)->first()) {
$product = $product->toArray();
$data['item'] = $product;
$data['title'] .= '|' . $product['title'];
}
}
You are passing $data from the controller to the view with view('content.products', self::$data, ...).
In your model, you add the product data to $data['item'] which will be accessible in the view template through the variable $item, and then you add the product title to $data['title'], which then will be accessible in the view template through the variable $title.
This should be working, as long as your product data is being loaded correctly, which doesn't seem to be the case.
Try to replace in the view template {{ $item['title']}} with {{ $title }}. If this result in an empty title, then it means that the product data is not being loaded correctly. To make sure you can try to dump the data in the view with {{ dd($item) }}.
EDIT
There is a problem with your routes.php and the route you posted here doesn't seem to be the problem, but the other one probably is.
If you defined it as Route::get('shop/{category_url}/{product_url}', ...), both routes will match the URL /category/sorting-asc.
You could add something to the URL to say it is a product URL
Route::get('shop/{category_url}/product/{product_url}', ...);

Laravel Undifined Variable in View

I cant't figure out what's going wrong with a link from my "welcome" page to a "profile" page.
It says "Undefined variable: restaurants (View: /Users/beyerdynamic/Documents/Developer/dev1/resources/views/welcome.blade.php)"
The strange thong is that I pass the Variable to my view.
My PagesController is:
public function welcome() {
$restaurants = User::orderByRaw('RAND()')->take(3)->get();
return view('welcome')->withRestaurants($restaurants);
}
My View is:
#foreach($restaurants as $key => $restaurant)
<div class="col-md-4">
<div class="card">
<div class="image">
<img src="{{asset('images/frontend/profile/dummy/profilehero/hero4.png')}}" alt="..." />
<div class="filter filter-white">
<a href="{{ URL::to('profile/'.$restaurant->id)}}" type="button" class="btn btn-info btn-round btn-fill">
<i class="fa fa-heart"></i> View Restaurant
</a>
</div>
</div>
</div>
</div>
#endforeach
The Restaurants Variable is passed properly to my welcome view, but when I click the link the error occurs on /profile/{id} url.
Change return view('welcome')->withRestaurants($restaurants);
to
return view('welcome')->with('restaurants', $restaurants);
OR
return view('welcome', compact('restaurants');
Try with this,
return view('welcome')->with("restaurants",$restaurants);
OR
return view('welcome',["restaurants" => $restaurants]);
Check laravel docs : https://laravel.com/docs/master/views#passing-data-to-views
Change the following line:
return view('welcome')->withRestaurants($restaurants); // there is nothing like withRestaurants in laravel
to
return view('welcome', array('restaurants' => $restaurants));
and try again.

Categories