Display image with relations in Laravel - laravel

I want to display through the user's photo relationships but it does not show
And this photo also exists, but when I take dd(), it says null
this is Blade code And they all work except the user_image
#foreach($threads as $thread)
<div class="card my-3 rounded-lg mr- ">
<div class="card-body shadow ">
<blockquote class="blockquote mb-0">
<div class=" d-flex">
<p style="font-size: x-large; font-weight: bold; "><a class="" href="/threads/{{$thread->slug}}">{{\Illuminate\Support\Str::limit($thread->title,50)}}</a>
</p>
</div>
<div class=" d-flex">
<img class="img-profile rounded-circle mr-1 " style="width: 60px; height: 60px;"
src="{{$thread->user->user_image}}"> {{-- this is not working--}}
<p class="mt-3 text-secondary"
توسط {{$thread->user->name}} آپدیت شد</p>
</div>
<footer
class="blockquote-footer text-dark">{{\Illuminate\Support\Str::limit($thread->description,150)}}
</blockquote>
</div>
</div>
#endforeach

Maybe replace
public function user()
{
return $this->belongsTo(User::class);
}
by something like
public function images()
{
return $this->belongsTo(User::class);
}
and for users do
hasMany (images)
Also you have to do migrations to add foreign keys, did you do this ?

Related

I want to show assigned users using laravel

I am trying to show assigned users to the user I have made relationship b/w user and user_permission but I am facing an error.how to fix it thanks. please check this error https://flareapp.io/share/q5Y12O5X#F52 Can anyone provide me a solution that where I am wrong.
Database table
user_permission table
please see here https://ibb.co/7486n95
user table
please see here https://ibb.co/5jmqb0H
Controller
public function index()
{
$users=User::with('permission')->where('id' ,'!=' ,Auth::id())->paginate(6);
return view('index',compact('users'));
}
User Model
class User extends Authenticatable
{
public function permission()
{
return $this-
>hasMany('App\Users_permissions','user_Access_id','id');
}
}
Users_permissions Model
class Users_permissions extends Model
{
protected $table = 'user_permission';
protected $fillable = ['id','user_id', 'user_Access_id'];
public function user()
{
return $this->belongsTo('App\User','id');
}
}
Html View
<!-- Default box -->
<div class="card card-solid">
<div class="card-body pb-0">
<div class="row d-flex align-items-stretch">
#foreach($users->permission as $user)
<div class="col-12 col-sm-6 col-md-4 ">
<div class="card bg-light">
<div class="card-header text-muted border-bottom-0">
<div class="card-tools"> <span data-toggle="tooltip" title="3 New Messages"
class="badge badge-primary"></span>
</div>
</div>
<div class="card-body pt-0">
<div class="row">
<div class="col-7">
<h2 class="lead"><b>{{$user->name}}</b></h2>
<p class="text-muted text-sm"><b>Job: </b> {{$user->job}}</p>
<ul class="ml-4 mb-0 fa-ul text-muted">
<li class="small"><span class="fa-li"><i class="fas fa-lg fa-phone"></i></span> Phone
#: {{$user->phone_number}}</li>
</ul>
</div>
<div class="col-5 text-center">
<img src="{{url('public/assets/dist/img/user1-128x128.jpg')}}" alt="" class="img-
circle img-fluid">
</div>
</div>
</div>
<div class="card-footer">
<div class="text-left">
#if($user->isOnline())
<i class="fa fa-circle text-success"></i> Online#else
<i class="fa fa-circle text-default"></i> offline#endif</div>
<div class="text-right">
<a href="{{url('/chat',$user->id)}}" class="btn btn-sm bg-teal"> <i class="fas fa-
comments"></i><b> Chat</b>
</a>
</div>
</div>
</div>
</div>#endforeach</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<nav aria-label="Contacts Page Navigation">
<ul style="list-style-type:none;">
<li>{{$users->links()}}</li>
</ul>
</nav>
</div>
<!-- /.card-footer -->
</div>
<!-- /.card -->
Here, what you are trying to do this is you are directly accessing permission relationship from collection, this is why it is showing error. First you have to loop through users and inside that particular user permission relationship exist. So you have to loop through users model first.
Hope this helps:)
You are applying permissions to the collection of users, but only an individual user has permissions. You need to loop through all of your users, then access the users permissions:
#foreach($users as $user)
...
#foreach($user->permission as $permission)
{{$permission->name}}
#endforeach
#endforeach

laravel blade limiting the showing images proplem

everyone, I have a problem in laravel blade I have a page for my website that show all the image because my website is a sale website but when I load the page it shows all the images but I don't want to show all the images I want to hide a images after three or four columns if someone can help me please.
thanks for everyone.
this is my blade code:
<section class="container custom-margin ">
<h5 class="text-center" style="text-shadow: 1px 1px 2px black; color: #1b1e21">مجموعه ما با ۲۰۰۰ خوابگا در سطح کشور</h5>
<div class="justify-content-center thin-underline-1"></div>
<div class="row justify-content-center">
#foreach($hostels as $hostel)
#foreach($hostel->hostelDetails->attachments as $photo)
<div class=" col-12 col-sm-6 col-md-6 col-lg-3 px-1 mt-4 small-device-hid ">
<div class="card card-shadow custom-height-1 " style="border-radius: 0%">
<a href="{{route('khabgah_detailes.goToDetails',$hostel->id)}}"> <img src="/images/{{ $photo->file_name }}"
class="card-img-top card-img custom-card-img-height" alt=""></a>
<div class="car-body">
<div class="card-footer">
<div class="custom-circle"><p class="custom-circle-text card-text"><b>
#if($hostel->type == 1)
{{ 'ذکور'}}
#else {{ 'اناث' }}
#endif
</b></p></div>
<div class="custom-prices card-text text-left"> کرایه فی ماه : {{$hostel->hostelDetails->remark }} </div>
<div class="row mt-3">
<div class="col-12 col-sm-12 col-md-12 mb-2 ">
<span class="card-text">آدرس : {{$hostel->addresses->state }} {{$hostel->addresses->rood}}
{{$hostel->addresses->station }} {{$hostel->addresses->alley}}
</span>
</div>
</div>
</div>
</div>
</div>
</div>
#endforeach
#endforeach
</div>
</section>
Since every Laravel relation return a collection instance, you could as well use the take method to obtain the first N objects in your collection.
For example:
#foreach($hostel->hostelDetails->attachments->take(3) as $photos)
Another way would be to use the query builder of the attachments relation to limit the results at query level:
#foreach($hostel->hostelDetails->attachments()->limit(3)->get() as $photos)
The latter way would love preferred performance-wise as it would only fetch the needed models from the database. The former would fetch all the models from the database and would only display a few of them.

Laravel upload image through Cloudder and Cloudinary in update method with getRealPath() error "Call to a member function getRealPath() on null"

issue is i also use cloudder in create Products, done. However in edit, error is "Call to a member function getRealPath() on null"
Here is controller update() method
Cloudder::upload($request->file('images')->getRealPath(), $current_time);
$product->images = Cloudder::getResult()['url'];
$product->sale = $request->input('sale');
$product->description = $request->input('description');
$product->detail = $request->input('detail');
$product->save();
return redirect('/admin/product');
Here is views, input file upload image
<div class="row">
<label class="col-sm-2 label-on-left" for="inputSuccess">Upload image</label>
<div class="form-group">
<div class="row ml-1">
<div class="col-sm-2">
<input type="file" name="images" class="mr-2" accept="image/*" onchange="readURL(this);">
<span class="badge badge-green">Choose new image...</span>
<img id="upload-image" src="#" alt="new image here" />
</div>
<div class="col-sm-2">
<span class="badge badge-green">Previous Image</span>
<img src="{{ $product->images }}" class="img-thumbnail" style="width: 150px; height: 150px;"/>
</div>
#if($errors->has('images'))
<label class="text-danger">*{{$errors->first('images')}}</label>
#endif
</div>
</div>
</div>
You can use getPathname it is the method of SplFileInfo which is extended by laravel UploadedFile class
Cloudder::upload($request->file('images')->getPathname(), $current_time);

"Property does not exist on this collection instance" error while joining in laravel 5.4

UserAcademyController.php
public function view_video()
{
$materialList = DB::table('material')
->join('material_group', 'material.group_id', '=', 'material_group.id')
->select('material.*', 'material_group.group_thumbnail_url')
->get();
return view('user-academy.view_video', ['materialList' => $materialList]);
}
view_video.blade.php
#foreach ($materialList as $material)
<div class="col-sm-6 col-lg-4 col-md-4">
<div class="thumbnail">
<a href="{!! url('user-academy/view/' . $material->id)!!}">
#if($material->thumbnail_url !== null)
<img src="{{$material->thumbnail_url}}"
style="width: 700px; height: 300px">
#elseif($materialList->group_thumbnail_url !== null)
<img src="{{$materialList->group_thumbnail_url}}"
style="width: 700px; height: 300px">
#elseif($material->thumbnail_url !== null && $materialList->group_thumbnail_url !== null)
<img src="http://www.independentmediators.co.uk/wp-content/uploads/2016/02/placeholder-image.jpg"
style="width: 700px; height: 300px">
#endif
</a>
<div class="caption">
<h4 class="pull-right text-success">${{$material->price}} </h4>
<h4>
{{ $material->title }}
</h4>
<hr>
<p class="description">{!! $material->description !!} </p>
</div>
</div>
</div>
#endforeach
This is the "material" table
This is the "material_group" table
I get this error even though I copied everything exactly from laravel documentations and followed many videos on this topic I am a new programmer so this may be a very obvious mistake but I have spent a fair share of time looking at the code and just cannot find where the error is!
You are trying to access group_thumbnail_url property on Collection class which is for sure not exist, that's why you got that error.
the solution is to replace $materialList->group_thumbnail_url by material->group_thumbnail_url

laravel 5.4 pagination issue invalid argument supplied for foreach()

I have around 50 objects in an array.
$user = User::with('abc','def','ghi','jkl','mno','pqr')
->where([['id', '<>', Auth::user()->id],['role', '<>', 2]])->simplePaginate(15);
$users = json_decode($user,true);
Have I placed ->simplePaginate(15) this correctly? Can I do json_decode after that? like I am doing? I dont get any error in controller.
In Views
#section('content')
<div class="container-fluid">
<div></br></br></div>
#include('errors.list')
<div class="row">
#foreach($users as $user)
<a href="#" onclick=document.getElementById('{{$user->id}}').style.display='block'>
<div class="col-md-3" style="margin-bottom:50px;">
#if($user["search_status"] == 'matched')
<div class="w3-card-12-disabled" style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
#else
<div class="w3-card-12" style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
#endif
<img align="center" src="{{$user->photo}}" style=" border-radius:50%; ; border-style: solid; border-color: #333; border-width: 5px; margin-left:15%;" width="150px !important" height="150px !important" />
<h3 style="color:#000;
padding-top: 20px;" align="center">{{ $user->firstname}}</br>{{ $user->lastname}}</h3>
<div style="display:flex; justify-content: center;">
<div class="tooltip2">
<img align="middle" style="" src="{{$user->natives->flag}}"/>
<span class="tooltiptext">{{$user->natives->language}}</span>
</div>
</div>
</br>
</div>
</div>
</a>
#endforeach
<div class="row">
#foreach($users as $user)
<div id="{{$user->id}}" class="w3-modal">
<div class="w3-modal-content w3-animate-top w3-card-8">
<div id="div1" style="padding:40px 30px 30px 30px;">
<img style="border-radius:50%; border:5px solid #333; margin-left:18px" src="{{$user->photo}}" width="200px !important" height="200px !important">
<div style="text-align: center; padding-top:70px;">
{!! Form::open(['url' => '/users/'. $user->id.'/reportc']) !!}
{!! Form::hidden('_method', 'POST') !!}
{!! Form::hidden('friend', $user->id, null, ['class'=>'form-control']) !!}
{!! Form::submit('Report', ['class'=>'btn', 'style' => 'padding: 10px 10px 10px 10px; color:#000; border-color:#000;']) !!}
{!! Form::close() !!}
</div>
</div>
<div id="div2" style="padding:40px 0px 40px 0px;">
<span onclick=document.getElementById('{{$user->id}}').style.display='none'
class="w3-closebtn">×</span>
<h1>{{ $user->firstname}} {{ $user->lastname}}</h1>
</br>
</br>
<h5 style="padding-top:30px;"><b>About Me:</b> {{ $user->bio}} </h5>
<h5 style="padding-top:10px;"><b>Campus:</b> {{$user->campus->campus_name}}</h5>
<h5 style="padding-top:10px;"><b>School:</b> {{ $user->schools->schools_name}} </h5>
<h5 style="padding-top:10px;"><b>Year:</b> {{ $user->years->years_name}} </h5>
<h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user->natives->language}} </h5>
<h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{ $user->snatives->language }} </h5>
<h5 style="padding-top:10px;"><b>Seeking Speaker of:</b>
#foreach($seeks as $seek)
#if($seek->user_id === $user->id)
{{ $seek->language}},
#endif
#endforeach </h5>
</div>
</div>
</div>
#endforeach
</div>
{!! $users->links() !!}
</div>
</div>
#endsection
Even if I dont put this code {{ $users->links() }} in view I still get same error. I guess issue is with the pagination code on controller.
Edited
I have this small issue still:
Before
<h5 style="padding-top:10px;"><b>Campus:</b> {{$user['campus']['campus_name']}}</h5>
<h5 style="padding-top:10px;"><b>School:</b> {{ $user['schools']['schools_name']}} </h5>
<h5 style="padding-top:10px;"><b>Year:</b> {{ $user['years']['years_name']}} </h5>
<h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user['natives']['language']}} </h5>
<h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{$user['snatives']['language']}} </h5>
<h5 style="padding-top:10px;"><b>Seeking Speaker of:</b>
#foreach($seeks as $seek)
#if($seek->user_id === $user['id'])
{{ $seek->language}},
#endif
#endforeach </h5>
After
<h5 style="padding-top:10px;"><b>Campus:</b> {{$user->campus->campus_name}}</h5>
<h5 style="padding-top:10px;"><b>School:</b> {{ $user->schools->schools_name}} </h5>
<h5 style="padding-top:10px;"><b>Year:</b> {{ $user->years->years_name}} </h5>
<h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user->natives->language}} </h5>
<h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{$user->snatives->language}} </h5>
<h5 style="padding-top:10px;"><b>Seeking Speaker of:</b>
#foreach($seeks as $seek)
#if($seek->user_id === $user->id)
{{ $seek->language}},
#endif
#endforeach </h5>
This gives After code gives me error trying to get property of non-object which is why I was using json_decode() and Before code. How do I fix these?
result
{
"per_page":12,
"current_page":1,
"next_page_url":"http:\/\/localhost:8000\/users\/community?page=2",
"prev_page_url":null,
"from":1,
"to":12,
"data":[
{
"id":5,
"firstname":"Beth",
"lastname":"Hanley",
"email":"bah3#org.uk",
"role":1,
"photo":"uploads\/2.jpg",
"bio":"Hockey fan, foodie, gamer, Saul Bass fan and HTML5 Guru.",
"campus":{
"id":1,
"campus_name":"name"
},
"school":"1",
"year":"1",
"native_lang":"2",
"native_lang_flag":"2",
"search_status":"available",
"created_at":null,
"updated_at":null,
"lastlogin":null,
"verification_code":null,
"isverify":"0",
"native_lang_2":null,
"native_lang_flag_2":null,
"schools":{
"id":1,
"schools_name":"School of Energy, Geoscience, Infrastructure and Society"
},
"years":{
"id":1,
"years_name":"First"
},
"flags":{
"id":2,
"name":"\u00c5land Islands",
"flag":"\/flags\/AX-32.png"
},
"natives":{
"id":2,
"language":"English",
"flag":"\/flags\/GB-32.png"
},
"snatives":null
},
User Model
public function friends()
{
return $this->hasMany('App\Friend','friend_id','id');
}
public function seeks()
{
return $this->belongsToMany('App\Language');
}
public function natives()
{
return $this->hasOne('App\Language','id','native_lang');
}
public function snatives()
{
return $this->hasOne('App\Language','id','native_lang_2');
}
public function roles()
{
return $this->hasOne('App\Role','id','role');
}
public function campus()
{
return $this->hasOne('App\Campus','id','campus');
}
public function schools()
{
return $this->hasOne('App\School','id','school');
}
public function years()
{
return $this->hasOne('App\Year','id','year');
}
public function flags()
{
return $this->hasOne('App\Flag','id','native_lang_flag');
}
Campus Model
public function users()
{
return $this->belongsTo('App\User','campus','id');
}
Year Model
public function users()
{
return $this->belongsTo('App\User','year','id');
}
You don't need to json encode anything and access the object property directly.
Controller
$users = User::with('abc','def','ghi','jkl','mno','pqr')
->where([
['id', '<>', auth()->id()],
['role', '<>', 2]
])->simplePaginate(15);
return view('someview', compact('users'));
View
<div class="row">
#foreach($users as $user)
<a href="#" onclick="document.getElementById('{{ $user->id }}').style.display='block'">
<div class="col-md-3" style="margin-bottom:50px;">
#if($user->search_status == 'matched')
<div class="w3-card-12-disabled"
style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
#else
<div class="w3-card-12"
style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
#endif
<img align="center" src="{{ $user->photo }}"
style=" border-radius:50%; ; border-style: solid; border-color: #333; border-width: 5px; margin-left:15%;"
width="150px !important" height="150px !important"/>
<h3 style="color:#000;
padding-top: 20px;" align="center">{{ $user->firstname }}<br/>{{ $user->lastname}}</h3>
<br/>
</div>
</div>
</div>
</a>
#endforeach
</div>
{!! $users->links() !!}

Resources