Property [medias] does not exist on this collection instance - php

I have a website that works on localhost, but not on production server.
I got this error :
Property [medias] does not exist on this collection instance
This is a portion of the foreach loop that works on localhost :
#foreach($critiques as $critique)
<div class="max-w-md bg-white rounded-xl shadow-md overflow-hidden md:max-w-2xl">
<div class="md:flex">
<div class="md:shrink-0">
#if (preg_match('/img/', $critique->medias->picture) )
<img class="h-48 w-full object-cover md:h-full md:w-48" src="{{ $critique->medias->picture ?? '' }}" alt="{{ $critique->medias->title }}">
#elseif(!preg_match('/img/', $critique->medias->picture) && $critique->medias->plateform != 'Cinéma' )
<img class="h-48 w-full object-cover md:h-full md:w-48" src="{{ $critique->medias->picture ?? '' }}" alt="{{ $critique->title }}">
I do not understand what happens.

$critique is not an object but a collection. I guess that this is not a simple Model collection but you are doing some manipulation like grouping.
$critiques are probably a collection of collections and you need to do one more loop.

Related

img of admin auth doesn't loading in laravel 8

i am trying to show the image of the admin user but no thing show .
. and name email of the user showing correctly
#php
$adminData = Auth::user();
#endphp
< div class="widget-user-header bg-black" >
<h3 class="widget-user-username">Admin Name : {{ $adminData->name }}</h3>
Edit Prfile
<h6 class="widget-user-desc">Email : {{ $adminData->email }}</h6>
</div>
<div class="widget-user-image">
<img class="rounded-circle" src="{{(!empty(asset($adminData->profile_photo_path) ))? url('upload/admin_images/'.$adminData->profile_photo_path):url('upload/no_image.jpg')}}">
</div>
You have to use like below
#if($adminData->profile_photo_path)
<img class="rounded-circle" src="{{url('/'.$adminData->profile_photo_path)}}">
#else
<img class="rounded-circle" src="{{url('/no_image.jpg')}}">
#endif
You Can check if admin photo contains null value or not
<img class="rounded-circle" src="{{$adminData->profile_photo_path != null? url('upload/admin_images/'.$adminData->profile_photo_path):url('upload/no_image.jpg')}}">
Let's do it like this. Make sure you have a default image like no_image.jpg as a fallback function. It does display the image in case if the profile_photo_path attr exists or falls into no_image if not. However, I think your issue is because you don't point to storage folder.
<img src="{{ storage_path($adminData->profile_photo_path ?? 'admin_images/no_image.jpg')) }}" />

Trying to get property 'profile_picture' of non-object backend\students\contact.blade.php

I am getting this error
Trying to get property 'profile_picture' of non-object (View: D:\projet7899\laravel-only-school-managemnt\resources\views\backend\students\contact.blade.php`)
In contact.blade.php
<img class="w-20 h-20 sm:w-32 sm:h-32 rounded" src="{{ asset('images/profile/' .$student->user->profile_picture) }}" alt="avatar">
In the StudentController.php
I have this function
public function contact(Student $student) {
$class = Grade::with('subjects')->where('id', $student->class_id)->first();
return view('backend.students.contact', compact('class','student'));
}
It is the same structure of other function named a show that is working properly but this one is not well working I just need to get the value of the email of the user
I don't know if it is legitimate to share the whole GitHub link code
. Thanks in advance
The problem happens because you are trying to access profile_picture on the user model when the user model doesn't actually exist.
What you need to do is in your view check to make sure the user model actually exists
<img class="w-20 h-20 sm:w-32 sm:h-32 rounded" src="{{ !empty($student->user) ? asset('images/profile/' .$student->user->profile_picture) : asset('link to a generic image') }}" alt="avatar">
Access profile_picture from the student object
<img class="w-20 h-20 sm:w-32 sm:h-32 rounded"
src="{{ asset('images/profile/' .$student->profile_picture) }}" alt="avatar">

String replace in Laravel Blade

I have project in Laravel 5.8.
I have this code:
<a href="{{ route('user', ['url_address' => $user->url_address, 'id' => $user->id]) }}"><img
src="{{ $user->images->first()->path ?? $placeholder }}"
class="img-responsive center" alt="{{ $user->name }}"></a>
This return me:
<img src="upload/images/UserImage/c561b1742783ed038521c488d1cd59a1.jpg" class="img-responsive center" alt="angel">
I need replace this:
upload/images/UserImage/c561b1742783ed038521c488d1cd59a1.jpg
to:
upload/images/UserImage/thumbs3/c561b1742783ed038521c488d1cd59a1.jpg
How can I make it?
You have a few options here. You could update your DB data, add a new column to store the thumb path, or modify the existing path in place.
Here's one way to do it using https://laravel.com/docs/5.8/helpers#method-str-replace-first
<?php
use Illuminate\Support\Str;
...
$search = 'upload/images/UserImage/';
$replace = 'upload/images/UserImage/thumbs3/';
$original = $user->images->first()->path;
$replaced = Str::replaceFirst($search, $replace, $original );
// Now, use `$replaced` as your image src.
<a href="{{ route('user', ['url_address' => $user->url_address, 'id' => $user->id]) }}">
<img src="{{ $replaced ?? $placeholder }}"
class="img-responsive center" alt="{{ $user->name }}">
</a>
I should also mention, that in my experience it is easier to maintain a system where all you store in the DB is the file name of the uploaded document. In this case, c561b1742783ed038521c488d1cd59a1.jpg.
Your views then control which directory to look in: ...public_path('upload/images/thumbs/' . $user->profile_image)

How to highlight displaying records acording to values of table?

I am using Laravel 5.6 and mysql for my web application. in My application I have table named as vehicles as following,
id name number adtype
----------------------------
1 car 123 1
2 van 256 0
3 car 248 0
4 van 159 1
5 car 158 1
etc
and I am displaying above data on VehicleController as following
public function index(){
$vehicles = Vehicle::with('uploads')->get();
return view('vehicles.index')->withVehicles($vehicles);
}
upload is related table witch contain images of the vehicles
and My index.blade.php is like this
<div class="col-md-7 col-md-offset-1">
#forelse( $vehicles as $vehicule )
#if( $vehicule->uploads->count() > 0 )
<a href="{{ route('vehicles.show', $vehicule->id) }}">
#php
$upload = $vehicule->uploads->sortByDesc('id')->first();
#endphp
<div style="border-style: solid; color: black; ">
<img src="/images/{{ $upload->resized_name }}" height="150" width="250"></a>
#endif
</div>
it is working fine. but now I need highlight ads which related to adtype value as 1 from the vechicles table. how can do it?
I'm not entirely sure if I get your question right, but where's the problem of just using an if to determine if adtype equals 1 and handle the div's class or style attribute differently?
<div style="color: {{ $vehicule->adtype === 1 ? 'black' : 'blue' }}">
Besides that: You shouldn't lazy load something in a template; that's not the purpose of the template, this is a task for the controller. Use eager loading with a proper relation instead.
Note: You can - or should - also type cast attributes to equal their original purpose and prevent misleading comparisons.
In your case adtype looks like a boolean, so you would add this to your model's $cast property:
'adtype' => 'boolean',
try
<div class="col-md-7 col-md-offset-1">
#forelse( $vehicles as $vehicule )
#if( $vehicule->uploads->count() > 0 )
<a href="{{ route('vehicles.show', $vehicule->id) }}">
#php
$upload = $vehicule->uploads->sortByDesc('id')->first();
#endphp
#if($vehicule->adtype == 1)
<div style="border-style: solid; color: black; ">
<img src="/images/{{ $upload->resized_name }}" height="150" width="250"></a>
</div>
#else
<div style="border-style: solid; color: blue; ">
<img src="/images/{{ $upload->resized_name }}" height="150" width="250"></a>
</div>
#endif
#endif
</div>
I'm not sure I understand all of your code, but you are going to want something like this:
In your css:
<style>
.highlight {
... whatever ...
}
</style>
And then, in your blade,
#if( $vehicule->adtype == 1 )
<span class="highlight"> ...... whatever ..... </span>
#endif
Or, if you want it inline,
<span class="{{ ($vehicule->adtype == 1) ? 'highlight' : '' }}">

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