Undefined variable $data - php

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

Related

Undefined variable: data , $data is undefined. Laravel 8

I got an error says undefined variable data referring to my foreach in my blade
so my errors is :
Undefined variable: data (View: C:\Users\adila\Desktop\onlineshop\onlineshop\resources\views\user\product.blade.php)
so this is my blade file :
<div class="latest-products">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="section-heading">
<h2>Latest Products</h2>
view all products <i class="fa fa-angle-right"></i>
</div>
</div>`enter code here`
#foreach($data as $product)
<div class="col-md-4">
<div class="product-item">
<img src="assets/images/product_06.jpg" alt="">
<div class="down-content">
<h4>{{$product->title}}</h4>
<h6>$ {{$product->harga}}</h6>
<p>{{$product->deskripsi}}</p>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
and this is my controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Product;
class HomeController extends Controller
{
public function redirect(){
$usertype = Auth::user()->usertype;
if($usertype=='1'){
return view('admin.home');
}
else{
return view('user.home');
}
}
public function index(){
if(Auth::id()){
return redirect('redirect');
}
else{
$data = Product::all();
return view('user.home',compact('data', $data));
}
}
}
and my Route :
Route::get('/', [HomeController::class, 'index']);
Please help me
Please remove $data from compact() function
return view('user.home',compact('data'));
You passed the variable ‍$data‍ to the ‍‍‍‍view('user.home', compact('data')) but you have an error in the product.blade I think the problem is on the product. blade file. check that file
You error is in the product.blade.php view and in your controller you're returning the home.blade.php view.
You can avoid the Undefined variable error in your product.blade.php adding an isset in your view like this :
#if(isset($data))
#foreach($data as $product)
(...)
#endforeach
#endif

Why my Laravel blade view returns blank page?

this is my route
Route::get('/product/single/{slug}', 'Front\ShopController#shopSingle')
->name('front.shop_single.ru');
and my view where I use that route to load single product
<div class="col-md-4">
<div class="product">
<figure class="product-image">
<img src="/{{ $img[0] }}" alt="bitcoin, bitcoindoc, bitcoin exchange, exchange, libradoc, libradoc exchange">
<i class="licon-cart"></i>Подробно
</figure>
<div class="product-description">
{{ $product->title }}
<h5 class="product-name">{{ $product->price }}</h5>
</div>
</div>
</div>
finally controller to redirect my desired page
public function shopSingle($slug){
$product = Shop::whereSlug($slug)->whereLang(App::getLocale())->first();
$data['product'] = $product;
return view('front.'.'App::getLocale()'.'.shop_single', $data);
}
unfortunately, whenever I try to go to single product it returns blank page but in the view page resource(I use Chrome) everything is okay. Please help, thanks in advance.
You need to send the data as well to your view
public function shopSingle($slug){
$product = Shop::whereSlug($slug)->whereLang(App::getLocale())->first();
// it could work like this
$data['product'] = $product;
return view('front.'.App::getLocale().'.shop_single', $data);
// or
return view('front.'.App::getLocale().'.shop_single', compact($product));
// or
return view('front.'.App::getLocale().'.shop_single')->withProduct($product);
}
Try to change the last line from your controller to
return view('front.'.App::getLocale().'.shop_single', ['product' => $product]);

Variable doesn't seem to be passing from controller to to view

I'm trying to pass a variable from a controller route to a view so i can display data relating to new assoicate model. It's working for other contrllers, but i can't figure out why it's not here.
Controller
#Index - Not passing down
public function index(){
$Advert = new PropertyAdvert();
return view('pages/Advert/index', compact('Advert'));
}
#Show - Is working, this is just an example.
public function show($id){
$user = Auth::user();
$Advert = PropertyAdvert::where('id', $id)->first();
return view('pages/Advert/show', compact('Advert', 'user'));
}
This is the route
Route::get('/property', 'AdvertisementController#index');
Route::get('/advertise', 'AdvertisementController#create')->middleware('auth');
Route::post('/createadvert', 'AdvertisementController#store');
Route::get('/property/{id}', 'AdvertisementController#show');
This is the template i'm trying to pass down to show.blade.php
<div class="row text-center" style="display:flex; flex-wrap:wrap">
<div class="col-lg-12">
<h3>Our most recent properties</h3>
#foreach ($Advert as $property)
<div class="col-md-3 col-sm-6">
<div class="thumbnail">
<img src="data:image/gif;base64,{{ $property->photo }}">
<div class="caption">
<h4>{{$property->address .', '. $property->town .', '. $property->county}}</h4>
</div>
<p>
More Info!
</p>
</div>
</div>
#endforeach
</div>
</div>
</div>
Try this:
public function index(){
$Advert = PropertyAdvert::get();
return view('pages/Advert/index', compact('Advert'));
}
You need to actually access the data for that model.

Laravel: Check if variable is being passed from controller

I'm trying to check if variable is being passed from controller but isn't working, the controller function looks like here:
public function editClient($id)
{
$client = Client::find($id);
// $client_project = DB::table('client_project')->where('client_id',$id)->first()->project_id;
//$project = DB::table('projects')->where('id',$client_project)->first();
$client_projects = Client::find($id)->projects;
return view('cms.public.views.clients.editclient', ['client' => $client, 'client_projects' => $client_projects]);
}
In the client blade I show the projects of each one with this code:
<h3 class="h3client h3clienteedit">Proyectos de {{$client->name}}</h3>
#foreach ($client_projects as $project)
<div class="col-md-3 col3projectofclient">
<h4 class="h4traductiontitle">{{$project->title}}</h4>
<img src="{{ asset('/storage/projects/'.$project->slug.'/header.jpg') }}" class="imgprojectoneditclient">
</div>
#endforeach
But I only want to show it when have projects if not i don't want to show de .
I'm trying to do it with this code:
#if ( !empty($client_projects['slug']))
//code
#endif
pass the variable directly
#if (!$client_projects)
//code
#endif
or
#if ($client_projects->count())
//code
#endif
more info

eloquent scope to eager load with constriant?

Ok so I'm trying to setup a scope that will restrict which users are returned based on a column in a hasOne relation.
Here are the two methods from my User model:
public function scopeHasImage($query)
{
return $query->with(['profile' => function($query){
$query->where('avatar', '!=', '');
}]);
}
public function profile()
{
return $this->hasOne('App\Profile');
}
When I call it like so:
$users = User::HasImage()->simplePaginate(15);
All of that works ok untill I go to display the code in my blade template like so:
#foreach($users as $user)
<div class="col-xs-12 col-sm-3 col-md-2">
<a href="{{ route('user.profile.public', ['id' => $user->id]) }}">
<img class="img-responsive" src="{{ $user->profile->avatar }}" />
</a>
</div>
#endforeach
That results in this error:
Trying to get property of non-object
I have dumped the $user->profile and it is an object which has attributes listed and one of those attributes is avatar. So I'm not sure why this is not working.
Any thoughts would be greatly appreciated.
Almost certainly one of your users has no profile attached. Because you probably dumped only the the first $user->profile you didn't see that...
You can fix this by wrapping it in an if:
#foreach($users as $user)
#if($profile = $user->profile)
<div class="col-xs-12 col-sm-3 col-md-2">
<a href="{{ route('user.profile.public', ['id' => $user->id]) }}">
<img class="img-responsive" src="{{ $profile->avatar }}" />
</a>
</div>
#endif
#endforeach
Or exclude users without a profile in the first place (which is probably what you wanted all along)
public function scopeHasImage($query)
{
return $query->with('profile')->whereHas('profile', function($query){
$query->where('avatar', '!=', '');
});
}

Categories