How to show comments and replycomments in laravel - laravel

I don't know how to show comments and replycomments in my post page. My comments table contains id, sender_id, replyer_id, reply_id, comment_text.
CommentController returns a comment object. In Post.blade.php, how do I write a foreach loop or loops?

Your Controller code must be similar to this:
public function index()
{
$comments = Comment::with(['sender', 'other-relation'])->get();
return view('comments.index', compact('comments'));
}
and your blade code must be similar to this:
<ul>
#foreach($comments as $comment)
<li>{{ $comment->comment_text }}</li>
#if ($comment->sender) // or other relation
<a> {{$comment->sender->name}}<a> // relation name and column name must be fix yourself
#endif
#endforeach
</ul>

Inside blade file:
<ul>
#foreach($comments as $comment)
<li>{{ $comment->comment_text }}</li>
#endforeach
</ul>

Related

Laravel - ERROR: Trying to get property 'depthead' of non-object

In my Laravel-5.8 blade that has this code:
#foreach($employees as $key => $employee)
<li class="list-group-item">
<b>Line Manager:</b>
#if(!$employee->linemanager)
<a class="float-right">N/A</a>
#else
<a class="float-right">{{ $employee->linemanager->fullName() ?? 'None' }}</a>
#endif
</li>
<li class="list-group-item">
<b>HOD:</b>
#if(!$employee->department->depthead)
<a class="float-right">N/A</a>
#else
<a class="float-right">{{isset($employee->department->depthead) ? $employee->department->depthead->first_name. ' ' .$employee->department->depthead->last_name : 'None'}}</a>
#endif
</li>
#endforeach
and its pointing to this line:
#if(!$employee->department->depthead)
How do I resolve it?
Thanks
The root cause of your issue is that $employee->department is either null or not an Object (Instance of Department, assuming you have Model and Relationships setup properly). You've got a check in place, but it's in the wrong spot. Check for $employee->department and $employee->department->depthead:
#if($employee->department)
#if($employee->department->depthead)
{{ $employee->department->depthead->first_name. ' ' .$employee->department->depthead->last_name }}
#else
None
#endif
#else
N/A
#endif
The defaults returned from a properly formed relationship will remove the need for string checks, meaning that #if($employee->department) and #if($employee->department->depthead) will be "truthy" or "falsey" enough to handle your use case.
Sidenote, this complexity is best moved to a Model function. In your Employee model, add this function:
public function getDepartmentHeadAttribute(){
if ($this->department) {
if ($this->department->depthead) {
return "{$this->department->depthead->first_name} {$this->department->depthead->last_name}";
} else {
return 'None';
}
} else {
return 'N/A';
}
}
Then, in your .blade.php file, you can simply do:
#foreach($employees as $employee)
{{ $employee->department_head }}
#endforeach
And it will perform the required if checks inside your Model and output First/Last, None or N/A as requird.
#if(!$employee->department->depthead)
look like the employee->department is not null, but it is not an object too ...
may be it is a string or number ....
#if(!$employee->department->depthead)
you could use 'is_object' method to determine that ...
#if( is_object($employee->department))
#if(!$employee->department->depthead)
....
#endIf
#endIf
but i don't think that the problem ...
the problem maybe you miss loading the relation,
if the property 'department' is a field of type 'json'
don't forget to cast it in model to 'array'

I can't retrieve data from the related table(model) in Laravel6

I can't retrieve data from the related table.
There are 3 models(tables).
User
Chirp (has 'user_id' as foreign key)
Click (has 'chirp_id' as foreign key)
then I want to retrieve User & Click's data from Chirp model.
So I wrote:
Chirp.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Chirp extends Model
{
public $timestamps = false;
protected $guarded = [];
public function user()
{
return $this->belongsTo('App\User');
}
public function clicks()
{
return $this->hasMany('App\Click');
}
}
HomeController.php
class HomeController extends Controller
{
public function index()
{
$chirps = Chirp::with(['user','clicks'])
->orderBy('posted_at', 'desc')
->get();
return view('home', ['chirps' => $chirps]);
}
}
home.blade.php
#foreach($chirps as $chirp)
<div>
<div>by
<b>{{ $chirp->user->name }}</b>
on
<small>{{ $chirp->posted_at }}</small>
</div>
<div>
<p>{{ $chirp->text }}</p>
<p>{{ $chirp->click->ip_address }}</p>
</div>
</div>
#endforeach
at home.blade.php, {{ $chirp->click->ip_address }} can't be retrieved and get error "Facade\Ignition\Exceptions\ViewException Trying to get property 'ip_address' of non-object"
However, if I delete it, I can retrieve {{ $chirp->user->name }} properly.
Why can't I retrieve Click model from Chirp model, While I can retrieve User model from Chirp model?
Thank you.
You need to loop over your clicks as well:
#foreach($chirps as $chirp)
<div>
<div>by
<b>{{ $chirp->user->name }}</b>
on
<small>{{ $chirp->posted_at }}</small>
</div>
#foreach($chirp->clicks as $click)
<div>
<p>{{ $chirp->text }}</p>
<p>{{ $click->ip_address }}</p>
</div>
#endforeach
</div>
#endforeach
Chirp has many clicks (not click). You have to foreach $chirp->clicks in your blade.
#foreach ($chirp->clicks as $click)
<p>This is click id {{ $click->id }}</p>
#endforeach
You've hasMany relation with Chirp and clicks
And here you're getting many clicks instead of click
#foreach($chirp->clicks as $click)
<p>{{ $click->ip_address }}</p>
#endforeach
To debug this problem you can take the following steps:
Check if the chirps variable has any data within the controller.
dd($chirps);
If you know you have the data you can take the steps to make your blade better.
Becasue its a many to many relation you should loop trough the data.
#foreach($chirps as $chirp)
#foreach($chirp->clicks as $click)
<div>
<p>{{ $chirp->text }}</p>
<p>{{ $click->ip_address }}</p>
</div>
#endforeach
#endforeach

modulus with remainder laravel

I am trying to setup columns of 4 from a laravel array which works correctly. The problem is how do you handle remainders? With my loop I end up with 2 extra divs that are empty at the end of my output. I have 10 items that i'm looping over which would give me a remainder of 2.5 Here is my code.
<div class="serviceListingColumn">
<ul>
#foreach($serviceListings as $serviceListing)
#if ($serviceListing->service_category == $services->title)
<li class="serviceListingItem">{{$serviceListing->service_name}}</li>
#endif
#if($loop->iteration % 4 === 0)
</ul>
</div>
<div class="serviceListingColumn">
<ul>
#endif
#endforeach
</ul>
I would suggest chunking the original array into groups, then just looping those. Easier for the template logic.
// Use the Collection's group() functionality in your controller.
// Use collect() if it isn't a Collection already.
$array = collect($array)->chunk(4);
// Your template then doesn't need to worry about modulus,
// and can focus on displaying the chunked groups.
#foreach ($array as $group)
<div class="serviceListingColumn">
<ul>
#foreach ($group as $serviceListing)
<li class="serviceListingItem">{{ $serviceListing->service_name }}</li>
#endforeach
</ul>
</div>
#endforeach

The following code of laravel in not working in sublime

I want the names I have stored in the array but it is not picking up the names besides showing this output!!!
<UL>
#foreach($customers as $customer)
<Li>({ $customer})</Li>
#endforeach
</UL>
Customers
({ $customer })
({ $customer })
({ $customer })
Change <Li>({ $customer})</Li> to <li>{{ $customer }}</li>
It is also better not to use caps in HTML tags (use li instead of Li for example).
Your blade tag isn't right. You appear to be using a bracket and a curly brace. But you should just be using two curly braces such as {{customer}} rather than ({customer}).
Try this:
<ul>
#foreach($customers as $customer)
<li>{{ $customer}}</li>
#endforeach
</ul>

Displaying a Table, Undefined Offset Error

I want display tables in a blade view that should be like this:
Table Image
dynamically ...
When i add another User and attach a category it should display more tables.
I have 3 Tables in my Database:
User, user_category and category
I defined the realtionship in my model files.
A User can have many Categories.
I tried it with PHP variables and a #while blade expression.
#php
$i=0;
#endphp
#foreach($users as $user)
<!-- Display Table Header -->
#while($Category[$i]->user_id == $user->id)
<!-- Display Table Rows -->
#php
$i++;
#endphp
#endwhile
#endforeach
This gave me an error: Undefined Offset: 6.
I have currently 6 rows in my Table. I can display the content when i 'hardcode' the index, like this for example: $Category[5].
The Join Query in my Controller works. I sorted it ascendent by User ID
How do i solve this problem? I'm a beginner and currently learning laravel.
Yes, i googled my problem before. Sorry for my english
If I understand your question correctly, maybe this could help you. It's just an example but you get the point.
<ul>
#foreach ($users as $user)
#foreach ($user->categories as $category)
<li>{{ $category->property_1 }}</li>
<li>{{ $category->property_2 }}</li>
<li>{{ $category->property_3 }}</li>
#endforeach
#endforeach
</ul>

Resources