how to display nested data in table laravel - 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

Related

Facade\Ignition\Exceptions\ViewException Trying to get property 'id' of non-object

I've been trying to fetch all the rows where the user id is. then I got this error. Please help me.
CONTROLLER:
public function index()
{
$user_id = Auth::user()->id;
$applications = application::where('user_id', '=', $user_id)->first();
if (empty($applications)) {
$applications = "0";
return view('user.application.index',compact('applications'));
}
else{
return view('user.application.index',compact('applications'));
}
}
blade file
#foreach ($applications as $application)
<tr>
<td>{{ $loop->index +1 }}</td>
<td>{{ $application->id }}</td>
<td>{{ $application->created_at }}</td>
<td>{{ $application->surname }}, {{ $application->firstname }} {{ $application->middlename }}</td>
#if ($application->status == 1)
<td><p style="color:green;">APPROVED</p></td>
#elseif ($application->status == 0)
<td><p style="color:rgb(243, 180, 8);">PENDING</p></td>
#else
<td><p style="color:RED;">DISAPPROVED</p></td>
#endif
<td><img src = {{ asset('assets/img/eye.ico') }}/>
</td>
</tr>
Try replacing $applications='0' with $applications=[] and see if it works ;)

how to insert new variable into laraadmin view

i have for each loop in my view i want to check if the target is equal to some value and insert another value i`
#foreach ($modules as $module)
#if ($module->name === "h_car_positions") <?php $module->name = "history_car_positions" ?>
#else <?php $module_name = $module->name; ?>
#endif
<tr>
<td>{{ $module->id }}</td>
<td>{{ $module->name }}</td>
<td>{{ $module->name_db }}</td>
<td>{{ Module::itemCount( $module_name ) }}</td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-key"></i>
<i class="fa fa-sort"></i>
<a module_name="{{ $module->name }}" module_id="{{ $module->id }}" class="btn btn-danger btn-xs delete_module" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-trash"></i></a>
</td>
</tr>
#endforeach
i get undefined variable for that variable :$module_name
You are mixing blade syntax with php, so you should do it this way:
#php
if($module->name == "h_car_positions"){
$module_name = "history_car_positions";
} else {
$module_name = $module->name;
}
#endphp
#foreach ($modules as $module)
#php
$module_name = $module->name;
switch ($module_name) {
case "H_car_positions":
$module_name = "history_car_positions";
break;
default:
$module_name = $module->name ;
$module_count =Module::itemCount( $module_name );
}
#endphp
<tr>
<td>{{ $module->id }}</td>
<td>{{ $module->name }}</td>
<td>{{ $module->name_db }}</td>
<td>{{ $module_count }}</td>

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

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 != '' .

Laravel nested foreach?

Having trouble with nested foreach and despite the other questions, I can't get my head around what could be wrong with my Customers.blade file:
#foreach ($customers as $company)
<tr>
<td></td>
<td>{{ $company->name }}</td>
<td>#if ($company->active == 1) YES #else NO #endif</td>
<td>View</td>
<td>Edit</td>
<td>Remove</td>
</tr>
#foreach ($customersites->customer_id as $company->id)
<tr>
<td></td>
<td>{{ $customersites->sitename }}</td>
<td>#if ($customersites->active == 1) YES #else NO #endif</td>
<td>View</td>
<td>Edit</td>
<td>Remove</td>
</tr>
#endforeach
#endforeach
This results in:
Property [customer_id] does not exist on this collection instance. (View: >C:\Apache24\htdocs\truckrun\resources\views\customers.blade.php)
What I'm doing wrong?
Please try this:
#foreach ($customers as $company)
<tr>
<td></td>
<td>{{ $company->name }}</td>
<td>#if ($company->active == 1) YES #else NO #endif</td>
<td>View</td>
<td>Edit</td>
<td>Remove</td>
</tr>
#foreach ($customersites as $company->id)
<tr>
<td></td>
<td>#if(isset($customersites->sitename) && $customersites->sitename !="") {{ $customersites->sitename }} #endif</td>
<td>#if ($customersites->active == 1) YES #else NO #endif</td>
<td>View</td>
<td>Edit</td>
<td>Remove</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