Laravel foreach in a table. Some <tds> are being skipped - laravel

I have a table for some for each loop. For some records, the title is being skipped, even though it exists. Instead of showing null, there is one less td in the row, which skews the table.
Here is the loop
#foreach($comments as $comment)
<tr>
<td> {{ $comment->id }}</td>
#foreach($comment->articles as $article)
<td>{{ $article->title ?: 'No Title' }}</td>
#endforeach
<td>{{ $comment->body ?: 'No Comment' }}</td>
</tr>
#endforeach
Any idea why the title is being skipped in some instances.

You don't have to put the td inside a loop, else you'll end up with messy table, you may need to use a list instead to list the articles:
#foreach($comments as $comment)
<tr>
<td> {{ $comment->id }}</td>
<td> Articles :
<ul>
#foreach($comment->articles as $article)
<li>{{ $article->title ?: 'No Title' }}</li>
#endforeach
</ul>
</td>
<td>{{ $comment->body ?: 'No Comment' }}</td>
</tr>
#endforeach
That will keep your table with the same size, and organize your article.

The number of columns has to match in the table and the code above is not ensuring that. A comment can has 0 to N articles so you will end with a table containing from 2+0 to 2+N columns. Maybe you can try to show the article titles in a table within the 2nd column and replace the foreach with a forelse and use the loop variable. More info at https://laravel.com/docs/5.6/blade
Something like this
#foreach($comments as $comment)
<tr>
<td> {{ $comment->id }}</td>
<td>
#forelse($comment->articles as $article)
#if ($loop->first)
<table>
#endif
#if ($loop->last)
</table>
#endif
<tr><td>{{ $article->title ?: 'No Title' }}</td></tr>
#else
No articles
#endforelse
</td>
<td>{{ $comment->body ?: 'No Comment' }}</td>
</tr>
#endforeach

depends on how you want to display the data, look at this
if comment have 2 article, <td></td> <td></td>
what about next comment have 3 article, <td></td> <td></td> <td></td>
so your code is breaking the table
#foreach($comment->articles as $article)
<td>{{ $article->title ?: 'No Title' }}</td>
#endforeach

Try using the below code you did mistake in ternary condition
Ternary syntax : if(condition ? true : false)
e.g if($article->title=='new' ? $article->title : 'No Title')
#foreach($comments as $comment)
<tr>
<td> {{ $comment->id }}</td>
#foreach($comment->articles as $article)
<td>{{$article->title ? $article->title : 'No Title'}}</td>
#endforeach
<td>{{ $comment->body ? $comment->body : 'No Comment' }}</td>
</tr>
#endforeach
Note : Comment may or maynot have articles so do check for it
e.g
#foreach($comments as $comment)
<tr>
<td> {{ $comment->id }}</td>
#foreach($comment->articles as $article)
#if($article)
<td>{{$article->title ? $article->title : 'No Title'}}</td>
#else
<td>No Article</td>
#endif
#endforeach
<td>{{ $comment->body ? $comment->body : 'No Comment' }}</td>
</tr>
#endforeach

You have to print title if it's true in a ternary operator. if(condition ? true : false).
so condition be like
#foreach($comment->articles as $article)
<td>{{ $article->title != null ? $article->title : 'No Title' }}</td>
#endforeach
if the title will not null it will display title otherwise it will display 'No Title'.
You can change condition as per your requirement. like $article->title != null or $article->title != '' .

Related

how to display nested data in table laravel

How can I get the result as drawn on the right
in Controller
$programs = Program::with(['activities', 'activites.subactivities'])->get();
in Blade View
#foreach ($programs as $programKey => $program)
#foreach ($program->activities as $activity)
#foreach ($activity->subactivities->toArray() as $subactivity)
<tr>
<td>{{ $programKey + 1 }}</td>
<td>{{ strtoupper($program->name) }}</td>
<td>{{ $activity->name }}</td>
<td>{{ $subactivity['name'] }}</td>
</tr>
#endforeach
#endforeach
#endforeach
I would say to save in a variable and if different the show
Like this:
#foreach ($programs as $programKey => $program)
#php
$newProgram = true;
#endphp
#foreach ($program->activities as $activityKey => $activity)
#php
$newActivity = true;
#endphp
#foreach ($activity->subactivities->toArray() as $subactivity)
<tr>
<td>
#if($newProgram == true)
{{ $programKey + 1 }}
#endif
</td>
<td>
#if($newProgram == true)
{{ strtoupper($program->name) }}
#php
$newProgram = false;
#endphp
#endif
</td>
<td>
#if($newActivity == true)
{{ $activity->name }}
#php
$newActivity = false;
#endphp
#endif
</td>
<td>
{{ $subactivity['name'] }}
</td>
</tr>
#endforeach
#endforeach
#endforeach

ASK I want to use 2 FOREACH in 1 table body VIEW laravel

I want to use 2 FOREACH in 1 table body VIEW laravel
like this for the EXCEL.VIEW.BLADE code
<table>
<thead>
<tr>
<th>Booking Status</th>
<th>CreatedAT</th>
<th>New row</th>
</tr>
</thead>
<tbody>
#foreach($datas as $hasil)
foreach($pp as $res)
<tr>
<td>{{ $hasil->BookingStatus }}</td>
<td>{{ $hasil->DeliveryCharge }}</td>
<td>{{ $res->BookingStatus }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
and for the code in the controller like this
$data = TR_BookingHeader::where('LogisticId', $this->qty)->get();
$pp = TR_BookingHeader::all();
return view('Excel', [
'datas' => $data, 'pp' => $pp
]);
Try this one
<tbody>
#foreach($datas as $hasil)
#foreach($pp as $res)
<tr>
<td>{{ $hasil->BookingStatus }}</td>
<td>{{ $hasil->DeliveryCharge }}</td>
<td>{{ $res->BookingStatus }}</td>
</tr>
#endforeach
#endforeach
</tbody>
Another example
[
state
college
college
state
college
college
college
...
]
#foreach ($states as $state => $colleges)
<h1 style="center">{{ $state }}</h1>
<ul class="nospace clear">
#foreach ($colleges as $college)
<li class="one_quarter first">
<h3 class="center">{{ $college->college }}</h3>
</li>
#endforeach
</ul>
#endforeach

Laravel-5.8::Invalid argument supplied for foreach()

hi i have multiple db tables ('dealers,suppliers,histories') and trying to show data of suppliers which are related to dealers(in dealers table supplier_id is using as foreign key)at dealers index but it is showing error: Invalid argument supplied for foreach() ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,m trying to get history data form dealers index by supplier id,,,,,,,,,,,,
note only dealers are showing on index and I am using resource route for it
code of index:
#foreach ($data as $row)
<tr>
<td>{{ $row->dealer_name }}</td>
<td>
#foreach($row->supplier as $sp)
{{ $sp->supplier_name }}<br>
#endforeach
</td>
</tr>
#endforeach
Controller code:
$data = Dealer::all();
return view('dealer.index', compact('data'));
supplier model:
public function dealerHistory()
{
return $this->hasoneThrough('App\History', 'App\Dealer');
}
all other models only have protected fillable in them according to this https://laravel.com/docs/5.8/eloquent-relationships#has-one-through
Here as you mentioned dealers are showing then problem must be in #foreach($row->supplier as $sp)
As error says Invalid argument supplied for foreach() you need to verify $row->supplier is array.
#foreach ($data as $row)
<tr>
<td>{{ $row->dealer_name }}</td>
<td>
#if(is_array($row->supplier) && !empty($row->supplier))
#foreach($row->supplier as $sp)
{{ $sp->supplier_name }}<br>
#endforeach
#endif
</td>
</tr>
#endforeach
#foreach ($data as $row)
<tr>
<td>{{ $row->dealer_name }}</td>
<td>
#if(!empty($row->supplier()))
#foreach($row->supplier as $sp)
{{ $sp->supplier_name }}<br>
#endforeach
#endif
</td>
</tr>
#endforeach

Trying to get property 'column_name' of non-object while displaying data on view

I want to show data on my blade view from relationship data but when am trying to display data whereas table contains only one row of data it's showing on view but if I insert more than one data in table it's giving me an error.
I have three tables courses, sections, course_section. In course_section table these are the following columns course_id and section_id.
I have tried {{ $section->courses()->first()->courseTitle }} this snippet in view found on stackoverflow.
My Section Model code:-
class section extends Model
{
public function courses(){
return $this->belongsToMany('App\Model\admin\course\course','course_sections','course_id');
}
}
My Section Controller code:-
$sections = section::with('courses')->orderBy('id','DESC')->get();
return view('backend.courses.section.all',compact('sections','courses'));
My view code:-
#foreach ($sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>{{ $section->courses()->first()->courseTitle }}</td>
</tr>
#endforeach
I am getting this error
"Trying to get property 'courseTitle' of non-object (View:
resources/views/backend/courses/section/all.blade.php)"
Here is the following you are doing wrong:
Replace $section->courses() with $section->courses as you are already doing early loading. And $section->courses() will query to db again.
Check if relational data exists then show.
So your code would be as follow:
#foreach ($sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>
#php
$course = $section->courses->first();
#endphp
{{ $course->courseTitle or "" }}
</td>
</tr>
#endforeach
Let me know if it helps!
Edited:
As per conversation the relationship has been changed like course ->hasMany -> sections and section ->belongsTo -> course so the blade would be change like.
#foreach ($sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>
#php
$course = $section->course;
#endphp
{{ $course->courseTitle or "" }}
</td>
</tr>
#endforeach
Section Controller:
$sections = section::with('courses')->orderBy('id','DESC')->get();
return view('backend.courses.section.all', compact('sections'));
In the View you have to loop the sections and then the courses and create each row. For ex:
#foreach ($sections as $section)
#foreach ($section->courses as $course)
<tr>
<td>{{ $section->title }}</td>
<td>{{ $course->courseTitle }}</td>
</tr>
#endforeach
#endforeach
Note it's $section->courses and not $section->courses(), because the related courses are already there, you doesn't need to query them again.
Update
Or you can do the query over course
$courses = course::with('sections')->get();
return view('backend.courses.section.all',compact('courses'));
And in the View:
#foreach ($courses as $course)
#foreach ($course->sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>{{ $course->courseTitle }}</td>
</tr>
#endforeach
#endforeach

How to hide variable in laravel

I have a code like below
#foreach ($model as $rows)
<tr>
<td>{{ $i++}}</td>
<td>
{{
Employee::where('nik','=',$rows->nik)->first(['nama'])
}}
</td>
<td>{{ $rows->nik }}</td>
<td>
{!! HTML::link('employee/profile_app/'.$rows->nik,'view', array('class'=>'fa fa-pencil-square-o action-button')) !!}
</td>
</tr>
#endforeach
and the result is like this
enter image description here
How to hide that "nama" variable
#foreach ($model as $rows)
<tr>
<td>{{ $i++}}</td>
<td>
{{
Employee::where('nik','=',$rows->nik)->first(['nama'])->nama
}}
</td>
<td>{{ $rows->nik }}</td>
<td>
{!! HTML::link('employee/profile_app/'.$rows->nik,'view', array('class'=>'fa fa-pencil-square-o action-button')) !!}
</td>
</tr>
#endforeach

Resources