So I don't how to use posts with categories in my foreach.
Controller
$Categories = \App\Category::Where('displayed',1)->get();
foreach ($Categories as $Category){
$CategoryPosts[]=$Category->load('posts');
}
dd($CategoryPosts);
return view('welcome', compact($CategoryPosts));
Page
#forelse($CategoryPosts as $post)
<div id="content-number-1" class="col-12 col-md-8 col-lg-8 home-post1 content-number-1">
<div class=""><img class="content-one-img content-img-chapter" src="{{$post->image}}"></div>
<div class="content-text-img p-3"><p><a class="m-0 underImage aTagsHover" style="font-size: 20px" href="/inside/{{$post->id}}">{{$post->title}}</a><br><span class="date-font"><i class="fa fa-clock-o" aria-hidden="true"></i>{{$post->created_at}}</span><span class="underBusiness"><br>{{$post->short_desc}}</span></p></div>
</div>
#empty
<div id="content-number-1" class="col-12 col-md-8 col-lg-8 home-post1 content-number-1">
<div class="content-one-img content-img-chapter"></div>
<div class="content-text-img p-3"><p><a class="m-0 underImage aTagsHover" style="font-size: 20px" href="#">No post</a><br><span class="date-font"><i class="fa fa-clock-o" aria-hidden="true"></i>No time</span><span class="underBusiness"><br>No post</span></p></div>
</div>
#endforelse
I have 2 categories and 3 posts right there.
https://prnt.sc/ujrvxs.
You can go at this from the other direction to only get the Posts:
$posts = Post::whereHas('category', function ($query) {
$query->where('displayed', 1);
})->get();
Assuming you have the inverse of the relationship setup Post -> Category. This is saying get all Posts having Category where displayed equals 1.
Adjust the relationship name as needed.
Laravel 7.x Docs - Eloquent - Relationships - Querying Relationship Existence whereHas
Related
I've saved the categories as JSON to the database, but I can't just foreach a category on the frontend.
So, for example, I want to show all of the 1 digits in the database with foreach. How can I do it?
Controller:
public function index() {
$contents = DB::table('posts')
->orderByDesc('posts.created_at')
->join('categories', 'posts.categories', 'categories.id')
->whereNull('posts.deleted_at')
->latest('posts.created_at')->get();
return view('frontend.index', compact('contents'));
}
Foreach in Blade:
#foreach($contents->where('status', '1')->where('categories', \DB::table('settings')->first()->design_1)->take(6) as $posts)
<div class="item">
<div class="top-post-wrap top-post-wrap-4">
<div class="thumb">
<div class="overlay"></div>
<img style="object-fit: cover; height: 515px;" src="{{route('frontend.cdn', $posts->img_512)}}" alt="img">
<a class="tag top-right tag-red" href="{{route('frontend.category', $posts->slugname)}}">{{$posts->name}}</a>
</div>
<div class="top-post-details">
<div class="meta">
<div class="date">
<i class="fa fa-clock-o"></i>
{{Carbon\Carbon::parse($posts->created_at)->diffForHumans()}}
</div>
</div>
<h4>{{Str::limit($posts->title,70)}}
</h4>
</div>
</div>
</div>
#endforeach
DB "categories" Column in "posts" table:
["1","3","4","5","6","7","8","9","10","11","12","13","14"]
I want to see all posts in category 3 with foreach. For example, there are 3 id numbers in the JSON code I gave as an example.
Page Controller
$Sport_Post = \App\Post::All()->Where('category_id','=',3)->first();
$Sport_Posts = \App\Post::All()->Where('category_id','=',3)->skip(1);
return view('welcome', compact('Sport_Post','Sport_Posts'));
Code of page
#forelse($Sport_Post as $post)
<div id="content-number-1" class="col-12 col-md-8 col-lg-8 home-post1 content-number-1">
<div class=""><img class="content-one-img content-img-chapter" src="{{$post->image}}"></div>
<div class="content-text-img p-3"><p><a class="m-0 underImage aTagsHover" style="font-size: 20px" href="/inside/{{$post->id}}">{{$post->title}}</a><br><span class="date-font"><i class="fa fa-clock-o" aria-hidden="true"></i>{{$post->created_at}}</span><span class="underBusiness"><br>{{$post->short_desc}}</span></p></div>
</div>
#empty
<div id="content-number-1" class="col-12 col-md-8 col-lg-8 home-post1 content-number-1">
<div class="content-one-img content-img-chapter"></div>
<div class="content-text-img p-3"><p><a class="m-0 underImage aTagsHover" style="font-size: 20px" href="#">No post</a><br><span class="date-font"><i class="fa fa-clock-o" aria-hidden="true"></i>No time</span><span class="underBusiness"><br>No post</span></p></div>
</div>
#endforelse
The error starts in src="{{$post->image}}">; What's wrong ? How to fix it
modify your code like below
in controller
$Sport_Post = \App\Post::Where('category_id','=',3)->first();
in blade file no need of for loop because $Sport_Post is a single object not a collection so modify blade code as below:
#if(isset($Sport_Post))
<div id="content-number-1" class="col-12 col-md-8 col-lg-8 home-post1 content-number-1">
<div class=""><img class="content-one-img content-img-chapter" src="{{$Sport_Post->image}}"></div>
<div class="content-text-img p-3"><p><a class="m-0 underImage aTagsHover" style="font-size: 20px" href="/inside/{{$Sport_Post->id}}">{{$Sport_Post->title}}</a><br><span class="date-font"><i class="fa fa-clock-o" aria-hidden="true"></i>{{$Sport_Post->created_at}}</span><span class="underBusiness"><br>{{$Sport_Post->short_desc}}</span></p></div>
</div>
#else
<div id="content-number-1" class="col-12 col-md-8 col-lg-8 home-post1 content-number-1">
<div class="content-one-img content-img-chapter"></div>
<div class="content-text-img p-3"><p><a class="m-0 underImage aTagsHover" style="font-size: 20px" href="#">No post</a><br><span class="date-font"><i class="fa fa-clock-o" aria-hidden="true"></i>No time</span><span class="underBusiness"><br>No post</span></p></div>
</div>
#endif
There is no need of All() change your code with following
$Sport_Post = \App\Post::Where('category_id','=',3)->first();
This query will fetch a single record from the database so you don't need for each loop.
now if you still getting the error Trying to get property of non-object
then convert the $Sport_Post variable to object or try to access the value as array
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
I am using Laravel 5.8 and in my controller I am showing 4 elements with paginate():
public function index()
{
$proyects = Proyect::latest()->paginate(4);
return view('proyect.index', compact('proyects'));
}
In the view the 4 elements are in a single row but I would like to show 2 elements for each row.
<div class="row">
<div class="col-10 col-lg-12 col-md-6">
<div class="card-deck">
#forelse($proyects as $proyect)
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $proyect->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">
{{ $proyect->descripcion }}</h6>
<p class="card-text">You can use the cap image as an
overlay for the body</p>
<a href="{{ route('proyect.show', $proyect) }}"
class="card-link">Ver mas</a>
</div>
<div class="card-footer">
<small class="text-muted">
{{ $proyect->created_at->diffForHumans() }}</small>
</div>
</div>
#empty
<li> Empty </li>
#endforelse
</div>
</div>
How to show 2 items per row instead of all items in a row?
public function index()
{
$proyects = Proyect::latest()->paginate(10);
return view('proyect.index', compact('proyects'));
}
array_chunk is a php function ca splice your array.
#forelse(array_chunk($proyects->all(),2) as $rows)
<div class="row">
#foreach($rows as $proyect)
<div class="col-10 col-lg-12 col-md-6">
<div class="card-deck">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $proyect->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{ $proyect->descripcion }}</h6>
<p class="card-text">You can use the cap image as overlay for the body</p>
Ver mas
</div>
<div class="card-footer">
<small class="text-muted">{{ $proyect->created_at->diffForHumans() }}</small>
</div>
</div>
</div>
</div>
#endforeach
</div>
#empty
<div>Empty ...</div>
#endforelse
May be this is your solution:
<div class="row">
#forelse($proyects as $proyect)
<div class="col-6 col-lg-6 col-md-6">
<div class="card-deck">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $proyect->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">
{{ $proyect->descripcion }}</h6>
<p class="card-text">You can use the cap image as an
overlay for the body</p>
<a href="{{ route('proyect.show', $proyect) }}"
class="card-link">Ver mas</a>
</div>
<div class="card-footer">
<small class="text-muted">
{{ $proyect->created_at->diffForHumans() }}</small>
</div>
</div>
#empty
<li> Empty </li>
</div>
#endforelse
</div>
I am making a forum and when the user clicks on a theme, a page shows up with every topic that belongs to that theme. The problem is, how do I do this?
I made a for each loop which shows ALL the topics from the database instead of the topics that belong to that theme I clicked on. How can I do this?
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{id}', 'ThemesController#show')->name('showtheme');
ThemesController.php
I only show the show method because I believe that's the only one necessary here
public function show($id)
{
$topics = Topic::all($);
return view('themes/topics')->with('topics', $topics);
}
topics.blade.php
#extends('layouts.default')
#section('content')
<div class="container main-content">
<div class="row first-row">
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 100, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Gepost door: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Laatste reply</h6>
<p class="center-align">Naam</p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
</div>
</div>
#endsection
I believe I'm doing something obviously wrong but I can't see what.
Help will be appreciated. Thanks in advance
If I miss some code, Please inform me.
How are you defining the relationship between a theme and it's topics? You'll have to have something like a one to many or many to many established in your database for you to be able to query correctly. Do you have anything like that yet?
****edit****
public function show($id)
{
$topics = Theme::find($id)->topics;
return view('themes/topics')->with('topics', $topics);
}