I would like show nested list in my blade template.
If I want quickly display nested list I have to do it by recursive. I created in my template function named renderNode(). But this global function im my template is not good idea and practise. I would like reorganize it.
Do you know good practise, how to organize my code?
<?php
function renderNode($node) {
echo "<li class='dd-item dd3-item' data-id='$node->id'>";
echo "<div class='dd-handle dd3-handle'>Drag</div>
<div class='dd3-content'>
$node->name
<span class='pull-right categories-actions'>
<i class='fa fa-plus-square-o delete-category' title=''></i>
<i class='fa fa-cogs delete-category' title=''></i>
<i class='fa fa-trash-o delete-category' title=''></i>
</span>
</div>";
if ( $node->children()->count() > 0 ) {
echo "<ol class='dd-list'>";
foreach($node->children as $child) renderNode($child);
echo "</ol>";
}
echo "</li>";
}
?>
<div id="content" class="col-lg-10 col-sm-11">
<div class="row">
<div class="col-lg-12">
<div>
<button id="add-new-category" class="btn btn- primary">#lang('categories.new_category')</button>
</div>
<div class="dd" id="nestable3">
<ol class="dd-list">
#if(isset($categories))
#foreach($categories as $category)
<?php renderNode($category); ?>
#endforeach
#endif
</ol>
</div>
</div><!--/col-->
</div><!--/row-->
You are right, this is not good practice. The best thing to do would be to put this code in a model of your choosing. Models > Category would be the most appropriate imo. Just make a public function in your Category model like so...
public function renderNode($node)
{
// place code here
return something here;
}
then call the model function in your blade template
<?php Category::renderNode( $node ) ?>
You can pass $node through the Make::view in your route using with.
However, you might want to consider a different approach. Like this...
#if ( $node->children()->count() > 0 )
<ol class='dd-list'>
#foreach ($node->children as $child)
{{ Category::renderNode($child) }}
#endforeach
</ol>
#endif
<ol class="dd-list">
#foreach($categories as $category)
<li class='dd-item dd3-item' data-id="$category->id">
<div class='dd-handle dd3-handle'>Drag</div>
<div class='dd3-content'>
{{ $node->name }}
<span class='pull-right categories-actions'>
<i class='fa fa-plus-square-o delete-category' title=''></i>
<i class='fa fa-cogs delete-category' title=''></i>
<i class='fa fa-trash-o delete-category' title=''></i>
</span>
</div>
#if ( $node->children()->count() > 0 )
#foreach ($node->children as $child)
<li class='dd-item dd3-item' data-id='$child->id'>
<div class='dd-handle dd3-handle'>Drag</div>
<div class='dd3-content'>
{{ $child->name }}
<span class='pull-right categories-actions'>
<i class='fa fa-plus-square-o delete-category' title=''></i>
<i class='fa fa-cogs delete-category' title=''></i>
<i class='fa fa-trash-o delete-category' title=''></i>
</span>
#endforeach
#endif
</div>
</li>
#endforeach
</ol>
something like this
Related
Hi i am trying to paginate data from the database and i am getting the error below
"Call to undefined method App\Material::links() (View: C:\xampp\htdocs\project104\resources\views\swapview.blade.php)"
I don`t know where i am getting this wrong, i tried changing variables but still got an error
below is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Material;
class SwapViewController extends Controller
{
public function index()
{
return view('swapview');
}
public function show()
{
$inf = Material::paginate(4);
$info = Material::orderBy('created_at', 'DESC')->get();
return view('swapview',['info'=>$info]);
}
}
below is my view
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
#foreach($info as $inf)
<div style="background-color:#162238; color:white;" class="card mb-3" style="max-width: 540px;">
<div class="row g-0">
<div class="col-md-4">
<img
src="{{asset('images/' . $inf->imogi)}}"
alt="Trendy Pants and Shoes"
/>
</div>
<div class="col-md-8">
<div class="card-body">
<div style="text-align: right;">
<i style="color:#3490dc" class="fa fa-building-o" style="font-size:24px"></i> <small>{{$inf->organazation}}</small> <small><i class="fa fa-calendar" style="color:#3490dc" aria-hidden="true"></i> {{ $inf->created_at->format('d/m/Y') }}</small>
</div>
<br/>
<!--LIVEWIRE online status-->
#if($inf->isOnline())
<small><div class="circle"></div></small> <small style="color:#22bb33;">operative</small>
#else
<small><div class="ocircle"></div></small> <small>inoperative</small>
#endif
<h5 class="card-title"><i class="fa fa-user-circle-o" style="color:#3490dc" aria-hidden="true"></i> {{$inf->name}}</h5>
<small style="color:#3490dc;"><i class="fa fa-clock-o" style="color:#3490dc;"></i></small> <small style="color:#3490dc;"> posted {{ \Carbon\Carbon::parse($inf->created_at)->diffForHumans() }}</small><br/>
<small><i class="fa fa-map-marker" style="color:#3490dc" aria-hidden="true"></i> {{$inf->location}}</small> | <small><i class="fa fa-phone" style="color:#3490dc" aria-hidden="true"></i>{{$inf->contact}}</small>
<hr color=3490dc>
<p class="card-text">
<small><small style="color:#3490dc;"><i class="fa fa-exchange" aria-hidden="true"></i></small> {{$inf->swap}}</small>
</p>
<hr color=3490dc>
<p class="card-text">
<small><small style="color:#3490dc;"><i class="fa fa-exchange" style="font-size:10px"></i></small> {{$inf->exchange}}</small>
</p>
</div>
</div>
</div>
<a style="color:red;" href="{{ route('report') }}"><i class="fa fa-flag" style="color:red;" aria-hidden="true"></i> <small style="color:#3490dc;">Report</small></a>
</div>
<br/>
#endforeach
{{ $inf->links() }}
</div>
</div>
</div>
#include('footer')
#endsection
I will appreciate any help, thanks in advance
In your controller, this is your show method:
public function show()
{
$inf = Material::paginate(4);
$info = Material::orderBy('created_at', 'DESC')->get();
return view('swapview',['info'=>$info]);
}
So in your return, you send the $info variable to your view, but not you $inf.
And then, in your view, you are using $info as $inf, so your $inf variable is one Material item, not the paginate Material items you get in the controller. So then, when you use $inf->links(), Laravel can't get you some pagination links... because you are not on a paginate element.
It seems there is some confusion in variable naming.
I have three tables. First is user which has an 'email' as a username. Second is customer which alos has email column. And third table is shipments. The shipment has relationship with customer table. User can login with email id which is present in User and Customer table. When user is logged in with their email I wamt to show shipments related to respective user, filterdered by email address of current logged in user. Please guide be. below is my index.blade.php
#extends('layouts.app')
#section('content')
<div class="page-titles">
<h2> Vishal {{ $pageTitle }} <small> {{ $pageNote }} </small></h2>
<h4></h4>
</div>
<div class="card">
<div class="card-body">
<div class="toolbar-nav" >
<div class="row">
<div class="col-md-4 col-4">
<div class="input-group ">
<input type="text" class="form-control form-control-sm onsearch" data-target="{{ url($pageModule) }}" aria-label="..." placeholder=" Type And Hit Enter ">
</div>
</div>
<div class="col-md-8 col-8 text-right">
<div class="btn-group">
#if($access['is_add'] ==1)
<a href="{{ url('shipments/create?return='.$return) }}" class="btn btn-sm btn-primary"
title="{{ __('core.btn_create') }}"><i class="fas fa-plus"></i> {{ __('core.btn_create') }}</a>
#endif
<div class="btn-group">
<button type="button" class="btn btn-sm btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="fas fa-bars"></i> Bulk Action </button>
<ul class="dropdown-menu">
#if($access['is_remove'] ==1)
<li class="nav-item"><a href="javascript://ajax" onclick="SximoDelete();" class="nav-link tips" title="{{ __('core.btn_remove') }}">
Remove Selected </a></li>
#endif
#if($access['is_add'] ==1)
<li class="nav-item"><a href="javascript://ajax" class=" copy nav-link " title="Copy" > Copy selected</a></li>
<div class="dropdown-divider"></div>
<li class="nav-item"> Import CSV</li>
#endif
<div class="dropdown-divider"></div>
#if($access['is_excel'] ==1)
<li class="nav-item"> Export Excel </li>
#endif
#if($access['is_csv'] ==1)
<li class="nav-item"> Export CSV </li>
#endif
#if($access['is_pdf'] ==1)
<li class="nav-item"> Export PDF </li>
#endif
#if($access['is_print'] ==1)
<li class="nav-item"> Print Document </li>
#endif
<div class="dropdown-divider"></div>
<li class="nav-item"> Clear Search </li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!-- Table Grid -->
{!! Form::open(array('url'=>'shipments?'.$return, 'class'=>'form-horizontal m-t' ,'id' =>'SximoTable' )) !!}
<div class="table-responsive">
<table class="table table-hover table-striped " id="{{ $pageModule }}Table">
<thead>
<tr>
<th style="width: 3% !important;" class="number"> No </th>
<th style="width: 3% !important;">
<input type="checkbox" class="checkall filled-in" id="checked-all" />
<label for="checked-all"></label>
</th>
#foreach ($tableGrid as $t)
#if($t['view'] =='1')
<?php $limited = isset($t['limited']) ? $t['limited'] :'';
if(SiteHelpers::filterColumn($limited ))
{
$addClass='class="tbl-sorting" ';
if($insort ==$t['field'])
{
$dir_order = ($inorder =='desc' ? 'sort-desc' : 'sort-asc');
$addClass='class="tbl-sorting '.$dir_order.'" ';
}
echo '<th align="'.$t['align'].'" '.$addClass.' width="'.$t['width'].'">'.\SiteHelpers::activeLang($t['label'],(isset($t['language'])? $t['language'] : array())).'</th>';
}
?>
#endif
#endforeach
<th style="width: 10% !important;">{{ __('core.btn_action') }}</th>
</tr>
</thead>
<tbody>
#foreach ($rowData as $row)
<tr>
<td class="thead"> {{ ++$i }} </td>
<td class="tcheckbox">
<input type="checkbox" class="ids filled-in" name="ids[]" value="{{ $row->id }}" id="val-{{ $row->id }}" />
<label for="val-{{ $row->id }}"></label>
</td>
#foreach ($tableGrid as $field)
#if($field['view'] =='1')
<?php $limited = isset($field['limited']) ? $field['limited'] :''; ?>
#if(SiteHelpers::filterColumn($limited ))
<?php $addClass= ($insort ==$field['field'] ? 'class="tbl-sorting-active" ' : ''); ?>
<td align="{{ $field['align'] }}" width=" {{ $field['width'] }}" {!! $addClass !!} >
{!! SiteHelpers::formatRows($row->{$field['field']},$field ,$row ) !!}
</td>
#endif
#endif
#endforeach
<td>
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" data-toggle="dropdown"><i class="fas fa-tasks"></i> </button>
<ul class="dropdown-menu">
#if($access['is_detail'] ==1)
<li class="nav-item"> {{ __('core.btn_view') }} </li>
#endif
#if($access['is_edit'] ==1)
<li class="nav-item"> {{ __('core.btn_edit') }} </li>
#endif
<div class="dropdown-divider"></div>
#if($access['is_remove'] ==1)
<li class="nav-item"><a href="javascript://ajax" onclick="SximoDelete();" class="nav-link tips" title="{{ __('core.btn_remove') }}">
Remove Selected </a></li>
#endif
</ul>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<input type="hidden" name="action_task" value="" />
{!! Form::close() !!}
<!-- End Table Grid -->
</div>
#include('footer')
</div>
</div>
<script>
$(document).ready(function(){
$('.copy').click(function() {
var total = $('input[class="ids"]:checkbox:checked').length;
if(confirm('are u sure Copy selected rows ?'))
{
$('input[name="action_task"]').val('copy');
$('#SximoTable').submit();// do the rest here
}
})
});
</script>
#stop
So you want to display shipments of a logged-in user? Then at this point, I'm expecting that you have already defined the Eloquent Relationships as methods on your Eloquent User and Shipment model classes and actually able to save the data.
In your Controller
use Illuminate\Support\Facades\Auth;
public function index()
{
$user = Auth::user()->load('shipments');
return view('sample.index', ['user' => $user]);
}
With the Auth::user() it checks if the current user is authenticated (returns true if the user is logged-in) via the Auth facade. Since the User model has already been retrieved, you can use Lazy Eager Loading with the load() to load the Shipment model.
And in your blade, you can display user's info and iterate over the shipment collection like so:
#foreach ($user->shipments as $shipment)
<p>{{ $shipment->field }}</p>
#endforeach
I have this application where I am trying to get the the author of a question to be able to mark an answer as the best answer.
I have an AcceptAnswerController created which has been registered in the routes/web.php file as below:
AcceptAnswerController.php
class AcceptAnswerController extends Controller
{
public function __invoke(Answer $answer)
{
$this->authorize('accept', $answer);
$answer->question->acceptBestAnswer($answer);
return back();
}
}
web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('questions', 'QuestionsController')->except('show');
Route::resource('questions.answers', 'AnswersController')->only(['store', 'edit', 'update', 'destroy']);
Route::get('questions/{slug}', 'QuestionsController#show')->name('questions.show');
Route::post('answers/{answer}/accept', 'AcceptAnswerController')->name('answers.accept');
In my Answers view, i have the following:
<div class="row mt-4">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="card-title">
<h2>{{ $answersCount . " " . str_plural('Answer', $answersCount) }}</h2>
</div>
<hr>
#include ('layouts._messages')
#foreach ($answers as $answer)
<div class="media">
<div class="d-fex flex-column vote-controls">
<a title="This answer is useful" class="vote-up">
<i class="fas fa-caret-up fa-3x"></i>
</a>
<span class="votes-count">1230</span>
<a title="This answer is not useful" class="vote-down off">
<i class="fas fa-caret-down fa-3x"></i>
</a>
#can ('accept', $answer)
<a title="Mark this answer as best answer"
class="{{ $answer->status }} mt-2"
onclick="event.preventDefault(); document.getElementById('answer-{{ $answer->id }}').submit();"
>
<i class="fas fa-check fa-2x"></i>
</a>
<form id="answer-{{ $answer->id }}" action="{{ route('answers.accept', ['answer' => $answer->id]) }}" method="POST" style="display:none;">
#csrf
</form>
#else
#if ($answer->is_best)
<a title="The question owner accepted this answer as best answer"
class="{{ $answer->status }} mt-2"
>
<i class="fas fa-check fa-2x"></i>
</a>
#endif
#endcan
</div>
<div class="media-body">
{!! $answer->body_html !!}
<div class="row">
<div class="col-4">
<div class="ml-auto">
#can ('update', $answer)
Edit
#endcan
#can ('delete', $answer)
<form class="form-delete" method="post" action="{{ route('questions.answers.destroy', [$question->id, $answer->id]) }}">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure?')">Delete</button>
</form>
#endcan
</div>
</div>
<div class="col-4"></div>
<div class="col-4">
<span class="text-muted">Answered {{ $answer->created_date }}</span>
<div class="media mt-2">
<a href="{{ $answer->user->url }}" class="pr-2">
<img src="{{ $answer->user->avatar }}">
</a>
<div class="media-body mt-1">
{{ $answer->user->name }}
</div>
</div>
</div>
</div>
</div>
</div>
<hr>
#endforeach
</div>
</div>
</div>
</div>
Once the author of the question clicks on the check icon, it should mark the answer as the best answer but i get the following error:
Sorry, the page you are looking for could not be found
I see that the answer id is missing in the url as follows:
http://localhost:8000/answers//accept
when it should actually be something like this:
http://localhost:8000/answers/1/accept
I can't seem to figure out why as i passed it as a route parameter in the form action. Same thing happens if a user is trying to edit his answer.
Can I suggest tweaking your strategy a little bit here and bypassing the need for a form submission?
If you swap out the POST for a GET request, this is actually pretty straight forward.
Answers.blade.php
#foreach( $answers as $answer )
#can('accept', $answer)
<a href="answers/{{ $answer->id }}/accept" title="Mark this answer as best answer" class="{{ $answer->status }} mt-2">
<i class="fas fa-check fa-2x"></i>
</a>
#else
<p>Do your thing</p>
#endcan
#endforeach
routes.web.php
Route::get('answers/{answer}/accept', 'AcceptAnswerController');
So, after scrutinizing my code a bit more carefully everything else seemed okay the controller, routes, views. I was able to isolate the problem down to this region
#can ('accept', $answer)
<a title="Mark this answer as best answer"
class="{{ $answer->status }} mt-2"
onclick="event.preventDefault(); document.getElementById('answer-{{ $answer->id }}').submit();"
>
<i class="fas fa-check fa-2x"></i>
</a>
<form id="answer-{{ $answer->id }}" action="{{ route('answers.accept', ['answer' => $answer->id]) }}" method="POST" style="display:none;">
#csrf
</form>
#endcan
I was certian the $answer->id was supposed to return the correct id but i wasn't too sure about the $answer->status so I decided to check its accessor defined in the Answer model.
public function getStatusAttribute()
{
return $this->isBest() ? 'vote-accepted' : '';
}
public function isBest()
{
return $this->id = $this->question->best_answer_id; /** here is the problem **/
}
There the problem was staring right back at me. The isBest method above was supposed to return a boolean value but i was mistakingly assigning. This was the simple fix.
public function isBest()
{
return $this->id === $this->question->best_answer_id; /** here is the problem **/
}
Here is my view its #include which call in layouts.master:
<li id="alert_notificatoin_bar" class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="
{{url('/admin/notification')}}">
<i class="icon-bell-l"></i>
<span class="badge bg-important">{{ $users}}</span>
</a>
<ul class="dropdown-menu extended notification">
<div class="notify-arrow notify-arrow-blue"></div>
<li>
<p class="blue">You have notifications</p>
</li>
<li>
<a href="{{url('/admin/notificationshow')}}">
#foreach($users1 as $u)
<span class="label label-success"><i class="icon_like"></i></span>
{{$u->name}}
<span class="small italic pull-right"> {{$u->created_at}}</span>
#endforeach;
</a>
</li>
<li>
See all notifications
</li>
</ul>
</li>
here is my controller
public function notification()
{
$users = DB::table('users')->where("Active", 0)->count();
$users1 = DB::table('users')->where("Active", 0)->get();
return view('admin.layout.master')->with('users',$users)->with('users1',$users1);
//return view('');
}
public function notificationshow()
{
$users1 = DB::table('users')->where("Active", 0)->get();
return view('admin.dashboard.notificationshow')->with('users1',$users1);
}
public function updatenotification(Request $request)
{
DB::table('users')
->where($request)
->update(['Active' => 1]);
return view('admin.dashboard.notificationshow');
}
}
here is my another view which getting error notificationshow
<div class="row">
<div class="col-lg-12">
<div>
#foreach($users1 as $u)
<span class="label label-success"><i class="icon_like"></i>
{{$u->name}}</span>
<span class="small italic pull-right"><label> <h5>Requested at</h5></label>{{$u-
>created_at}}</span>
<label><h5>Waiting for Approval</h5></label>
#endforeach
<form class="login-form" role="form" method="POST" action="{{ url('/admin/')
}}">
{{ csrf_field() }}
<button class="btn btn-primary btn-lg btn-block" type="submit">Approve</button>
</div>
</form>
why its giving me error please help i checked using dd($user1); its give value but why i getting error when passing value in other view notificationshow
This is a case where code neatness, especially indentation, can help you identify bugs in your code. Looking at the code provided above, there are unmatched <div> and <form> tags, and line breaks in odd places that possibly are causing errors.
I have this blade in my view. Right now, I have 6 blocks of them in my view because I'm not sure how to refactor it.
<div class="row filemanager">
<div class="col-sm-12">
#foreach ($devices as $device)
#if( $device->vlan_id == 100 AND $device->device_activity == 'ACTIVE' )
<div class="col-xs-6 col-sm-4 col-md-2 text-center">
<div class="thmb">
<div class="btn-group fm-group" style="display: none;">
<button type="button" class="btn btn-default dropdown-toggle fm-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu fm-menu" role="menu">
<li id="device-menu">
<a class="changeDeviceNameBtn" href="#"><i class="fa fa-pencil"></i> Change Device Name </a>
</li>
</ul>
</div>
<div class="thmb-prev">
<a href="/{{$cpe_mac}}/device/{{$device->device_mac}}">
#if(isset($device->device_name))
{{-- Show base on device name --}}
<img src="/images/photos/devices/{{img($device->device_name)}}.jpg" class="draggable img-responsive" alt="">
#else
{{-- No Device Name Set --}}
#if($device->hostname != '')
{{-- Show base on hostname --}}
<img src="/images/photos/devices/{{img($device->hostname)}}.jpg" class="draggable img-responsive" alt="">
#else
{{-- Show default --}}
<img src="/images/photos/devices/no-img.jpg" class="draggable img-responsive" alt="">
#endif
#endif
</a>
</div>
<h5 class="fm-title device_name">
<a href="/{{$cpe_mac}}/device/{{$device->device_mac}}">
#if($device->hostname == '')
No Devicename
#else
{{ $device->hostname}}
#endif
</a>
</h5>
<h5 class="text-muted device_ip">{{$device->ip_address}}</h5>
<h5 class="text-muted device_mac">{{$device->device_mac}}</h5>
<?php
$status = ucfirst(strtolower($device->device_activity));
if ($status == 'Active'){
$color = '#1CAF9A';
}else{
$color = '#D9534F';
}
?>
<h5>{{ $status }}
<i class="fa fa-circle" style="color:{{$color}}; margin-left: 7px;"></i>
</h5>
</div>
</div>
#endif
#endforeach
</div>
</div>
I want to make a function containing that blade, and only replace my
$device->vlan_id, and my $device->device_activity.
Example,
public static deviceRow(100,ACTIVE){
... my blade ...
}
Now, I just that function 6 times, rather than duplicate that block of code 6 times.
Is it even possible ?
Any hints / suggestions on this will be much appreciated !
You can make a partial with your blade and send a variable as a parameter:
In your parent view do something like this:
#foreach($somelist as $item)
#include('view.partial', ['name' => $item->name])
#endforeach
And in a file called partial.blade.php, do something like this:
{{ $device->$name }}
It's the main idea. Tell me if it helps...
You could create a new view and send some parameters with it while including:
#include('my.view', ['device' => $myDevice, 'activity' => 'ACTIVE'])
The keys of the array will be available as variables in your view.
The variable $myDevice would be available as $device in the view my.view