Trying to get property 'image' of non-object - php

In Controller
public function index()
{
$profile = Profile::where('user_id', auth()->user()->id)->first();
return view('profile', compact('profile'));
}
In Blade View
<div class="card mb-5">
<img class="card-img-top" src="{{ asset('images/'.$profile->image ? $profile->image : "" ) }}"
alt="Profile User">
<div class="card-body">
<h5 class="card-title">{{Auth::user() ? Auth::user()->name :""}}</h5>
<p class="card-text">{{Auth::user() ?Auth::user()->email :""}}.</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">{{$profile ? $profile->gender :"Belum Di isi"}}</li>
<li class="list-group-item">{{$profile ? $profile->phone :"Belum Di isi"}}</li>
<li class="list-group-item">{{$profile ? $profile->address :"Belum Di isi"}}</li>
</ul>
</div>
But above code throw an error Trying to get property of non-object.
How can you fix these errors, so that the image runs again without an error

In my case! every time this error happens in my code this is because $item->image is not present in my collection instance.

The error is thrown because your $profile variable is not an object, it can be an array or null. Try dd($profile) to make sure that you are passing an object
if you are passing an object then try changing your code like this if you are passing an object
<img class="card-img-top" src="{{ asset('images/' . $profile ? $profile->image : "" )}}"/>
or like below if you are passing an array
<img class="card-img-top" src="{{ asset('images/' . isset($profile['image'] ? $profile['image'] : "" )}}"/>

Do a Auth check before use the auth.
Use firstOrFail instead of first, it will throw a 404 error if get null value :
use Illuminate\Support\Facades\Auth;
public function index()
{
if (Auth::check())
{
$profile = Profile::where('user_id', Auth::id())->firstOrFail();
return view('profile', compact('profile'));
} else {
return "You are not logged in";
}
}

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

Problem with Load More Button in laravel 6 and ajax

so i have a problem in my laravel application when i access to the route where i posted the json i get the 500 error so there is the source :
the route :
web.php
Route::get('loadmore', 'MoviesController#loadmore');
The controller :
public function loadmore(Request $request)
{
$movies_list = new Movies;
if ($request->has('page')) {
$movies_list = Movies::where('status', 1)->orderBy('id', 'DESC')->paginate(4);
}
$returnHTML = view('pages.ajax_movie', compact('movies_list'))->render();
return response()->json([
'html' => $returnHTML,
'page' => $movies_list->currentPage(),
'hasMorePages' => $movies_list->hasMorePages(),
])
}
and finally the view :
location : pages/ajax_movies.blade.php
#foreach($movies_list as $movies_data)
<a href="{{ URL::to('movies/'.$movies_data->video_slug.'/'.$movies_data->id) }}" class="movie">
<span class="r"><i class="i-fav rating"><i>{{$movies_data->imdb_rating}}</i></i></span>
<img src="{{URL::to('upload/source/'.$movies_data->video_image_thumb)}}" alt="" title="">
<span class="title">{{$movies_data->video_title}}</span>
<span class="ribbon ">
<span>{{$movies_data->video_access}}</span>
</span>
</a>
#endforeach
everyone can help me ?
thank you in advance

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

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.

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