This is my table NewTheme_Comment:
id
parent_id
id_theme
user
text
upVotes
downVotes
And this is my controller:
class Comments extends Controller {
public function GenerateComments($id){
$Comments = NewTheme_Comment::where('id_theme', $id)->paginate(5);
return view('comments', ['Comments'=>$Comments]);
}
Id is the link to the general post(each posts have different section of comments), so dynamically at the click of user, user is redirected to the ('comments view') section of comments accordingly.
So I have the array Comments which is populated with the values from the table NewTheme_Comment, the problem for me consists of how can I use the values to create a Threaded Comments section.
Like so in my view(the problem is that this makes a comment section as like as every parent_id is equal to 0 which is not that I am looking for:
#foreach ($Comments as $Comment) {{-- Comments threads --}}
<div class="media">
<div class="media-left">
</div>
<div class="media-body">
<p class="media-heading">{{ $Comment->text }}</p>
<p class="media-heading">{{ $Comment->user }} / {{ $Comment->created_at }} </p>
</div>
</div>
#endforeach {{-- Comments threads --}}
The end result:
0
1
2
3
2
1
1
0
1
0
My idea is to make comments like(with parent_id):
0
1
2
3
2
1
1
0
1
0
But I can't find the right way to do this thing logically, so that in the end to look like a simple threaded comment section the same that reddit uses( for my little web application ).
If my approach is bad, I would greatly appreciate other better ways on how to solve this.
It is a multi-step solution. You should prefer using lazychaser/laravel-nestedset package as the threaded comment is a form of nested set.
The other way round, if you want to continue with your current approach, will be having child function in your Comment Model, as follows
public function children(){
return $this->hasMany('App\Models\Comment', 'parent_id'); //Change as per your application architecture
}
Then in views, you can check for the children of a given node by calling a partial view recursively, containing
//print the details of $Comment
#foreach($Comment->children as $childComment)
//call this partial view again for $childComment & print the details
#endforeach
List every comment that doesn't have a parent_id, then when you load your comments check if any of those comments have childs comments and list those with indentation.
Related
Limit to item list:
More Description from the Pics:
I want to limited max item list at Laravel, As you if select all filter in admin panel, its look like this. So how can limited first 5 item to popular filters ?
<div class="g-attributes">
<span class="attr-title" style="color: orange"><b><i class="icofont-medal"></i> {{$translate_attribute->name ?? ""}}:</b> </span>
#foreach($termsByAttribute as $term )
#php $translate_term = $term->translateOrOrigin(app()->getLocale()) #endphp
<span class="item {{$term->slug}} term-{{$term->id}}" style="color: green" >{{$translate_term->name}}</span>
#endforeach
</div>
You can use the Collection's take() method to grab the first 5 elements:
#foreach($termsByAttribute->take(5) as $term)
Your question isnt very clear, but I am assuming that you want to be able retrieve the last 5 rows of the table, in your controller, you can get the records like this
$termsByAttribute = Table::latest()->take(5)->get();
First, my apologies if the issue has been resolved. I read a lot of similar posts, but not quite the right one for my case.
I am setting up a simple project in Laravel 5.8. I want to create a simple bookstore and I need a book to have multiple authors so each title is followed by the author, or authors - if many - separated by commas and the last one by the word 'and'.
I have set up two Models, 'Author' and 'Book' and their respective tables, as well as a pivot table since they are in a relation belongsToMany. Everything works like a charm, I get my results as expected. However, I cannot get to format the results as I need. In the case of multiple authors, I always get an extra comma in the end. Since I cannot get the commas right I haven't yet tried to add the last 'and' in the case of the last author.
In the case of a single author the solution is easy, I just use a conditional.
However in the case of multiple authors, thing get complicated.
The most popular method to similar problems was the use of implode() in various similar ways. The problem with this method is since Eloquent is using its internal logic to perform the query, when I loop through 'books', there is no author column, just a reference to the pivot table. In addition, the authors' include first name and last name in different columns. So, when I try to manually create an 'implodable()' array by fetching the respective data otherwise, I get double the item size, since each name consists of the first name and theist name. And on top of that, the whole thing runs inside a double loop, making things even more complicated for me.
I am sure there is a simple way around this.
This is a sample of my blade code as of now. Of course the conditionals should be rearranged accordingly when the problem will be solved, to implement the 'and' case:
<ul>
#foreach ($books as $book)
<li>{{ $book->title }} by
#if (count($book->author) == 1)
#foreach ($book->author as $name)
{{ $name->last_name }}
{{ $name->first_name }}
#endforeach
#else
#foreach ($book->author as $name)
{{ $name->last_name }}
{{ $name->first_name }}
{{-- print a comma here if there are other names, or an 'and' if it the last one. Problem seems that it needs to be solved outside the conditional, but how? --}}
#endforeach
#endif
</li>
#endforeach
</ul>
My DB structure:
'authors': 'id', 'last_name', 'first_name','created_at', 'updated_at'
'books': 'id', 'title', 'created_at', 'updated_at'
'author_book': 'id', 'author_id', 'book_id','created_at', 'updated_at'
Expected result:
Title 1, by Author 1
Title 2, by Author 1 and 2
Title 3, by Author 1, 2 and 3
Actual Result:
Title 1, by Author 1
Title 2, by Author 1 and 2 and
Title 3, by Author 1 and 2 and 3 and
If you didn't need the "and" mechanism, then implode with comma would do the job. For "and" mechanism use below code:
<ul>
#foreach ($books as $book)
<li>{{ $book->title }} by
#for ($i = 0; $i < count($book->author); $i++)
{{ $book->author[$i]->last_name }}
{{ $book->author[$i]->first_name }}
#if ($i == count($book->author) - 2)
and
#endif
#if ($i < count($book->author) - 2)
,
#endif
#endfor
</li>
#endforeach
</ul>
added non-breaking-space   wherever necessary to not get them linked to each other
Delete #if (count($book->author) == 1), if you use foreach inside.
You can't use #else, when before you close condition with #endif.
This #else has wrong construction, use #elseif instead.
If you want check authors count and show extra value, do it inside foreach with $book->author()->count().
What relation u have for book -> author? One book can have many authors or not?
Just display implode(', ', $book->author), based on your actual result you don't need to do complicated checking if/else anymore.
#foreach ($books as $book)
<li>{{ $book->title }} by {{print implode(', ', $book->author)}}</li>
#endforeach
I have two tables.
contenttype
content
contenttype returns me list of content types and I show them on page with foreach. e.g. Ambulance service, Blood Bank , clinic etc. as shown in snapshot.
At the same time I am fetching total number of contents of each type from another table(contents).
I was successful to get total number of contents of each type and show on the blade with foreach.
But situation is I want to show the number of contents on every content type.
Like this
Ambulance sevice 8,
Blood Bank 7,
Clinic 4.
My controller method is:
public function index()
{
if (Gate::allows('edit-content', auth()->user())) {
// below line returns list of content type e.g Ambulance service
$type = DB::table('contenttype')->distinct('label')->orderBy('label', 'asc')->paginate(10);
//below line counts the number of each content type e.g. Ambulance service 10.
$count = Content::selectRaw('type, count(*)total')->groupBy('type')->get();
return view('admin.content_listing', compact('type', 'count'));
} else {
abort(403, "Unauthorized");
}
}
This is blade code:
#foreach ($count as $c)
<span class="label label-danger">{{ $c->total }} </span>
#endforeach
This red number list is output:
#foreach ($type as $t)
<div class="list-group-item">
<a href="{{ route('content.type.listing', $t->type ) }}" > {{ $t->label }}
<span class=" pull-right glyphicon glyphicon-search"></span></a>
<span class="col-md-1 glyphicon glyphicon-plus-sign"></span>
</div>
#endforeach
Output is:
If I place above loop in second loop then Of course it will become nested loop that I don't want.
I need to show Ambulance 8,
Beauty clinic 8,
Blood Bank 1,
etc.
If anybody knows the solution kindly share it!
I have tried different ways but no success.
Rather than creating two queries and attempting to combine their results in the view, have you tried performing a join in a single query? Making certain assumptions about your column names and leaving aside the pagination, the actual SQL would be something akin to:
SELECT contenttype.*, count(content.id) FROM contenttype LEFT JOIN content ON contenttype.id = content.type GROUP BY contenttype.label ORDER BY contenttype.label ASC;
The code necessary to implement this query with Laravel's query builder functionality is pretty well documented in the documentation.
So I am pretty new to Laravel and PHP, so I had a few questions.
Right now I have a Post model that has many Likes.
Currently I am trying to display all posts on a view using #foreach($posts as $post)to retrieve the posts from the controller (this part works already,code for retrieving post content not relevant) I want to display all the likes and dislikes that a post has in a concise manner.
Currently a Like->Like == 1 is a like and a Like->like ==0 is a dislike.
So if a post has 5 dislikes, it will display:
Bob Jane and 4 others Dislike this
Or if it has 2 dislikes:
Jane Doe and John Teach Dislike this
<div class = "dislikes">
#if(count($post->likes->where('like',0))==0)
#elseif(count($post->likes->where('like',0))==1)
{{ $post->likes->where('like', 0)->first()->user->first_name }} {{ $post->likes->where('like', 0)->first()->user->last_name }} Disikes this
#elseif(count($post->likes->where('like',0))==2)
{{($post->likes->where('like', 0))->first()->user->first_name}} {{($post->likes->where('like', 0))->first()->user->last_name}} and {{($post->likes->where('like', 0))->last()->user->first_name}} {{($post->likes->where('like', 0))->last()->user->last_name}} Dislike This
#elseif(count($post->likes->where('like', 0))>=4)
{{($post->likes->where('like', 0))->first()->user->first_name}} {{($post->likes->where('like', 0))->first()->user->last_name}} and {{count($post->likes->where('like', 0)) - 1}} Others Like This
#else
#for($i=0;$i<count($post->likes->where('like',0));$i++)
#if($i+1 == count($post->likes->where('like',0)))
and {{$post->likes->where('like',0))->get()[$i]->user->first_name}} {{$post->likes->where('like',0)->get()[$i]->user->last_name}} Like This
#else
{{$post->likes->where('like',0)->get()[$i]->user->first_name}} {{$post->likes->where('like',0)->get()[$i]->user->last_name}} ,
#endif
#endfor
#endif
</div>
I have played around wiht the code for a few hours, but
($post->likes->where('like',0))[$i]->user->first_name is not working in my for loop, and ($post->likes->where('like',0))[$i]->user->first_name will not work either.( I have played around with the parentheses placement a bit to try and fix this)
I am at a loss. Any suggestions about a better way I could approach this would be greatly appreciated.
Thank you.
I don't know your model or table structure. But let say you have table and model named Post, Like and Dislike. You should have relationship in Post model such as :
public function like()
{
return $this->hasMany(Like::class);
}
public function dislike()
{
return $this->hasMany(Dislike::class);
}
then you can retrieve data in controller such as
$posts = Post::with('like', 'dislike')->get();
return view('view', compact('posts'));
now you can loop in view such as
#foreach($posts as $post)
#if($post->like != null)
<div class = "like">
#foreach($post->like)
// render the view here...
#endforeach
</div>
#endif
#if($post->dislike != null)
<div class = "dislikes">
#foreach($post->dislike)
// render the view here...
#endforeach
</div>
#endif
#endforeach
Halo here is my need;
i want to include different view as apart of different view in laravel php frame work.
class DashboardController extends BaseController {
public function comments( $level_1=''){
// process data according to $lavel_1
return View::make('dashboard.comments', $array_of_all_comments);
}
public function replys( $level_2=''){
// process data according to $lavel_1
return View::make('dashboard.replys', $array_of_all_replys);
}
these both data can now accessed from
www.abc.com/dashboard/comments
www.abc.com/dashboard/replys
And in my view what i need is to generate replys according to the comments id ($lavel_2)
// dashboard/comments.blade.php
#extends('layout.main')
#section('content')
#foreach($array_of_all_comments as $comment)
comment {{ $comment->data }},
//here is what i need to load reply according to the current data;
//need to do something like this below
#include('dashboard.replys', $comment->lavel_2) //<--just for demo
.................
#stop
and in replys also got
#extends('layout.main')
#section('content')
// dashboard/replys.blade.php
#foreach($array_of_all_replys as $reply)
You got a reply {{ $reply->data }},
...........
#stop
is there any way i can achieve this on laravel 4?
Please help me i wanted to load both comments and replays in one go and later need to access them individually via ajax also
please help me thank you very much in advance
halo i found the solution here
all we need is to use App::make('DashboardController')->reply();
and remove all #extends and #sections from including view file
the change are like this
// dashboard/comments.blade.php
#extends('layout.main')
#section('content')
#foreach($array_of_all_comments as $comment)
comment {{ $comment->data }},
//<-- here is the hack to include them
{{-- */echo App::make('DashboardController')->reply($comment->lavel_2);/* --}}
.................
#stop
.............
and in replys is now changed to
// dashboard/replys.blade.php
#foreach($array_of_all_replys as $reply)
You got a reply {{ $reply->data }},
...........
#endforeach
-------------
thanks
You probably want to rework your views and normalise your data. Comments and replies are (probably) the same.
If you make a Comment model which belongsTo "parent" (another Comment model) and hasMany "children" (many Comment models), then simply set parent_id to 0 for a top-level Comment, and set it to the ID of another Comment to make it a Reply.
Then your Blade views do something like:
comments.blade.php
#foreach ($comments AS $comment)
#include( 'comment', [ 'comment' => $comment ] )
#endforeach
comment.blade.php
<div>
<p>{{{ $comment->message }}}</p>
#if( $comment->children->count() )
<ul>
#foreach( $comment->children AS $comment )
<li>
#include( 'comment', [ 'comment' => $comment ] )
</li>
#endforeach
</ul>
#endif
</div>