Laravel get multiple objects in the same panel - php

I'm trying to get a list of the staffs on my application.
So now I got this view:
#foreach($getteam as $team)
<!-- Begin row -->
<div class="panel-group col-sm-offset-1 col-sm-10">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $team->role->name }}</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item">
<img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $team->username }}" width="75" height="75">
<h4 style="color:{{ $team->role->colour }};">{{ $team->username }}</h4>
<p>Een klein beetje info over wie ik ben enzo... :p</p>
</li>
</ul>
</div>
</div>
</div>
<!-- End row -->
#endforeach
With this query:
SELECT * FROM `user`
INNER JOIN `role` ON `user`.`role_id` = `role`.`id`
WHERE `role_id` >= '4'
ORDER BY `role`.`id` DESC
or
User::with('Role')
->join('role', 'user.role_id', '=', 'role.id')
->where('role_id', '>=', '4')
->orderBy('role.id', 'DESC')
->get();
Now, so for so good,
This works to get the users exept this is the result:
http://prntscr.com/8a6v60
That is not what I want.
This is what I want: http://prntscr.com/8a6vgo
So when the groups are the same, they need to be in the same UL.
Edit
This is my view now:
<!-- Begin row -->
<div class="panel-group col-sm-offset-1 col-sm-10">
#foreach($roles as $role)
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $role->name }}</h3>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($role->user() as $user)
<li class="list-group-item">
<img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $user->username }}" width="75" height="75">
<h4 style="color:{{ $role->colour }};">{{ $user->username }}</h4>
<p>Een klein beetje info over wie ik ben enzo... :p</p>
</li>
#endforeach
</ul>
</div>
</div>
#endforeach
</div>
<!-- End row -->
This is the controller:
public function ForumTeam()
{
$roles = Role::all();
$getTeam = User::with('Role')->join('role', 'user.role_id', '=', 'role.id')->where('role_id', '>=', '4')->orderBy('role.id', 'DESC')->get();
return View::make('team')->with('getteam', $getTeam)->with('roles', $roles);
}
And this one is the model.
<?php
class Role extends Eloquent
{
protected $table = 'role';
public function user()
{
return $this->belongsTo(User::class);
}
}
Now I only get all the role names, not the correct stuff.

Use the reverse of the relationship between users and roles. Instead of fetching the users, get the roles and for each role you can get the corresponding users (this of course requires the Role model to have a users() method with a relation defined:
// You can of course use conditions here to filter out what roles you want loaded
$roles = Role::all();
Then iterate through the roles, and for each role iterate through its users:
<div class="panel-group col-sm-offset-1 col-sm-10">
#foreach($roles as $role)
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $role->name }}</h3>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($role->users as $user)
<li class="list-group-item">
<img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $user->username }}" width="75" height="75">
<h4 style="color:{{ $role->colour }};">{{ $user->username }}</h4>
<p>Een klein beetje info over wie ik ben enzo... :p</p>
</li>
#endforeach
</ul>
</div>
</div>
#endforeach
</div>

Related

How to show image from database with eager loading?

I want to display multiple product image from database with eager loading, and when i tried to displaying it, console said localhost:8000/storage/ 404 not found. Any ideas how to solve this? this is my first time using eager loading & relationship on Laravel. Thank you!
Here is my controller :
public function homeProduct(){
$products = Product::with('productCategory')->get();
$images = Product::with('productImage')->get();
return view('home.index', compact('products', 'images'));
}
Product Model :
public function productImage()
{
return $this->hasMany(ProductImage::class, 'product_id', 'id');
}
Product Image Model :
public function product()
{
return $this->belongsTo(Product::class, 'id');
}
Here are the index.blade.php View :
#foreach ($products as $p)
<div class="col-lg-6 col-12">
<div class="portfolio-thumb mb-5" data-aos="fade-up">
<div id="yoreBeans" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-inner">
#foreach ($images as $key => $image)
<div class="carousel-item {{ $key == 0 ? 'active' : '' }}">
<a href="{{ asset('storage/'.$image->path) }}" class="image-popup">
<img src="{{ asset('storage/'.$image->path) }}" class="img-fluid portfolio-image" alt="">
</a>
</div>
#endforeach
</div>
</div>
<div class="portfolio-info">
<h3 class="text-black">{{ $p->name }}</h3>
<h4 class="text-danger">{{ $p->productCategory->name }}</h4>
</div>
</div>
</div>
#endforeach
Your method $images = Product::with('productImage')->get(); will also return Product objects with images.
What you exactly want is to eager load both realtions and then reference it at the same time:
public function homeProduct(){
$products = Product::with(['productCategory', 'productImage'])->get();
return view('home.index', compact('products'));
}
And your view like this:
#foreach ($products as $p)
<div class="col-lg-6 col-12">
<div class="portfolio-thumb mb-5" data-aos="fade-up">
<div id="yoreBeans" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-inner">
<!-- // this line changed --->
#foreach ($p->productImage as $key => $image)
<div class="carousel-item {{ $key == 0 ? 'active' : '' }}">
<a href="{{ asset('storage/'.$image->path) }}" class="image-popup">
<img src="{{ asset('storage/'.$image->path) }}" class="img-fluid portfolio-image" alt="">
</a>
</div>
#endforeach
</div>
</div>
<div class="portfolio-info">
<h3 class="text-black">{{ $p->name }}</h3>
<h4 class="text-danger">{{ $p->productCategory->name }}</h4>
</div>
</div>
</div>
#endforeach

Laravel 8 using group by in view/blade by foreach

I have this code in my controller:
public function videoGallery()
{
$videoLinksRecord = VideoLinks::all()->sortBy('video_category_id')->groupBy('video_catetgory_id');
return view('video.gallery')->with('videoLinksRecord', $videoLinksRecord);
}
which return this query:
{
"1": [
{"id":1,"video_category_id":1,"name":"Laszlo - Supernova","link":"PKfxmFU3lWY","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":2,"video_category_id":1,"name":"Subtact - Restart","link":"M5A46A2txC4","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":3,"video_category_id":1,"name":"Sushi Killer & Kevin Villeco - Anime Bae","link":"YHHjfYxLyuA","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":4,"video_category_id":1,"name":"Hyper Potions & Subtact - Adventures","link":"TKZUhs9Gcdo","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:53.000000Z","deleted_at":null}
],
"2": [
{"id":5,"video_category_id":2,"name":"Laszlo - Supernova","link":"PKfxmFU3lWY","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":6,"video_category_id":2,"name":"Subtact - Restart","link":"M5A46A2txC4","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":7,"video_category_id":2,"name":"Sushi Killer & Kevin Villeco - Anime Bae","link":"YHHjfYxLyuA","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null}
]
}
I want to display the result grouped by its video category similar to this blade:
{{-- name of the category --}}
<h2 class="poppins-medium text-lg text-darkergreen text-center">Video Category Name Here</h2>
<div class="container-fluid">
<div class="row">
<div class="col-4"></div>
<div class="col-4 borderline-darkergreen-md"></div>
<div class="col-4"></div>
</div>
</div>
{{-- loop for each result sharing the same "video_category_id" --}}
<div class="container-fluid">
<div class="row d-flex justify-content-center px-0">
<div class="col col-lg-2 d-none d-lg-block d-xl-none"></div>
<div class="col col-xl-2 d-none d-xl-block"></div>
{{-- video --}}
<div class="col-12 col-md-6 col-lg-4 container-fluid d-flex justify-content-center spacer-md">
<div class="row d-flex justify-content-center">
<div class="col-12 d-flex justify-content-center container-fluid px-0">
{{-- video border --}}
<div class="video-box-border position-relative spacer-md">
<a href="">
{{-- video card image --}}
<div class="video-image position-absolute"></div>
{{-- video card background --}}
<div class="video-card"></div>
</a>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-2 px-0"></div>
<div class="col-8 borderline-darkergreen-sm px-0"></div>
<div class="col-2 px-0"></div>
</div>
</div>
{{-- video name--}}
<p class="poppins-normal text-md text-gray text-center">Video Name Here
</p>
</div>
</div>
</div>
</div>
How should I use the $videoLinksRecord in a foreach logic to display it by its groupBy logic? This is my first time grouping query result so I do not know how to construct the logic for the said desired result.
Edit the code below is the relationship code in the model:
VideoLinks Model:
public function categories()
{
return $this->hasMany(VideoCategories::class);
}
VideoCategories Model:
public function links()
{
return $this->belongsTo(VideoLinks::class);
}
#foreach ($videoLinksRecords as $video_category_id => $videoLinks)
{{ $video_category_id }}
#foreach ($videoLinks as $videoLink)
{{ $videoLink->id }}
{{ $videoLink->video_category_id }}
{{ $videoLink->name }}
{{ $videoLink->link }}
{{ $videoLink->description }}
{{ $videoLink->home_video }}
{{ $videoLink->created_at }}
{{ $videoLink->updated_at }}
{{ $videoLink->deleted_at }}
#endforeach
#endforeach
Your relationship between the VideoLinks and VideoCategory models should be as follows
# VideoLinks model
public function category()
{
// 2nd parameter is optional if you follow the naming conventions
return $this->belongsTo(VideoCategory::class, 'video_category_id');
}
In your query eager load the relationship using with('category') but then group by the category's name and finally replace all() with get().
$videoLinksRecord = VideoLinks::with('category')->get()->sortBy('video_category_id')->groupBy('category.name');
#foreach ($videoLinksRecords as $category_name => $videoLinks)
{{ $category_name }}
#foreach ($videoLinks as $videoLink)
...

Why do I get Trying to get property 'username' of non-object while "owner_user" has information in it?

HTML Code
#foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">{{ $post->owner_user->username }}</p>
</div>
</div>
#endforeach
Laravel code
public function testpage()
{
return Posts::with(['OwnerUser','like'])->get();
}
when I just return Posts table, it has column owner_user with user information in it.
relations work, but somehow I get "Null" as a result in {{ $post->owner_user->username }} when I return it in HTML code.
because that is not a single object try
#foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">#(foreach $post->owner_user as $user){{$user->username }}#endforeach</p>
</div>
</div>
#endforeach

create view for each category - laravel

I currently print all categories in one view, with them I mark a difference between its subcategories
#foreach($tipos as $tipo)
<div class="card">
<h4 style="margin-top: 5px;">{{$tipo->tipo }}</h4>
</div>
#foreach($tipo->categories as $category)
<div style="margin-top: 20px;" class="col-lg-4 col-sm-6 mb-4">
<div class="card h-100">
<a href="{{ route('category-detail',['slug'=>$category->slug]) }}">
#if(!empty($category->image))
<img class="card-img-top" src="{{url($category->image)}}" alt="">
#else
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
#endif
</a>
<div class="card-body">
<h4 class="card-title">
{{$category->name}}
</h4>
<p class="card-text">{{$category->descripcion}}</p>
</div>
</div>
</div>
#endforeach
#endforeach
I get the names from:
categories table:
Now I need to create a view for each of them, enter it using url,
controller:
public function list($id){
$tipos = App\Tipo::findOrFail($id);
return view('list.tipos', compact('tipos'));
}
route:
Route::get('/list{id}','HomeController#list')->name('list.categories');
pivot table:
How can I print the content of each category? help pls
Modify your controller like this
Note: Change the Model Names (App\Pivote) and (App\Category) to what you have set in your code.
public function list($id){
$tipos = App\Tipo::findOrFail($id);
$pivot_data = App\Pivote::select('category_id')->where('tipo_id',$id)->get()->toArray();
$categories = App\Category::whereIn('id',$pivot_data)->get()->toArray();
return view('list.tipos', compact('tipos','categories'));
}
And modify your view something like this
<div class="card">
<h4 style="margin-top: 5px;">{{$tipos->tipo }}</h4>
</div>
#foreach($categories as $key => $category)
<div style="margin-top: 20px;" class="col-lg-4 col-sm-6 mb-4">
<div class="card h-100">
<a href="{{ route('category-detail',['slug'=>$category['slug']]) }}">
#if(!empty($category['image']))
<img class="card-img-top" src="{{asset($category['image'])}}" alt="">
#else
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
#endif
</a>
<div class="card-body">
<h4 class="card-title">
{{$category['name']}}
</h4>
<p class="card-text">{{$category['descripcion']}}</p>
</div>
</div>
#endforeach

Laravel get data from 2 tables where not null

I'm trying to get some data from two different tables,
So it does look like this now:
But I don't want the empty fields (Globale-Moderator, Moderator and Proef-Moderator) to be shown. How can I do this whit this code;
Controller:
public function ForumTeam()
{
$roles = Role::where('id', '>', '4')->orderBy('id', 'DESC')->get();
return View::make('team')->with('roles', $roles);
}
View:
<div class="panel-group col-sm-offset-1 col-sm-10">
#foreach($roles as $role)
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $role->name }}</h3>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($role->user as $user)
<li class="list-group-item">
<img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $user->username }}" width="75" height="75">
<h4 style="color:{{ $role->colour }};"><strong>{{ $user->username }}</strong></h4>
<p>{{ $user->usertitle }}</p>
</li>
#endforeach
</ul>
</div>
</div>
#endforeach
</div>
So I want to hide the not filled in fields, how should I do dis propperly?
My other option is to show some text inside of the 'empty' field. Like 'There is nobody with this rank'.
Thanks!
You can/should be able to use:
#foreach($roles as $role)
#if($role->user != null || !empty($role->user->name))
#endif
#endforeach
You can then check to see if the role->user is NOT NULL OR the name of the role is not empty.WHERE name is the name of the role, i.e. "Admin", "Moderator"
Alternatively, try:
#foreach($roles as $role)
#if(count($role->user) >= 1)
// Do stuff
#endif
#endforeach
Since you are getting User which has a one-to-many relationship, therefore there will be more than one user.
You could add an if statement after the first #foreach, to check if the role
has users,
#foreach($roles as $role)
#if(isset($role->user))
...
#endif
#endforeach

Categories