Below is my code:
<tbody>
#foreach($posts as $post)
<tr>
<th>{{ $post->id }}</th>
<td>{{ $post->title }}</td>
<td>{{ substr($post->body, 0, 50) }} {{ strlen($post->body) > 50 ? "..." : "" }}</td>
<td>{{ date('M j,Y h:i a',strtotime($post->created_at)) }}</td>
<td>
View
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="text-center">
{{ $posts->links() }}
</div>
And below is the code in controller:
public function index()
{
$posts = Post::orderBy('id', 'desc')->paginate(5);
return view('posts.index')->withPosts($posts);
}
In spite of this, i do not get proper styling!
Bootstrap styles not applied. See the image.
This was the issue with new Bootstrap version 4.1 with Laravel's latest version.
I tried with the earlier version of bootstrap and it is resolved now.
Related
I have a question why I'm getting undefined variable in my blade view "Trying to get property 'firstname' of non-object (View: C:\xampp\htdocs\focus\resources\views\assets\asset.blade.php)"
asset.blade.php
<tbody>
#foreach($assets as $asset)
<tr>
<td>{{ $asset->employee->firstname }} {{ $asset->$employee->lastname }}</td>
<!--this where the error came-->
<td>{{ $asset->employee->account->name }}</td>
<td>{{ $asset->employee->position }}</td>
<td>{{ $asset->workstation }}</td>
<td></td>
<td></td>
<td>{{ $asset->remarks }}</td>
<td>
</td>
</tr>
#endforeach
</tbody>
Asset.php
public function employee()
{
return $this->belongsTo('App\Employee');
}
Employee.php
public function asset()
{
return $this->hasMany('App\Asset');
}
I haven't really no idea where this error came from
output of dd($assets)
Try $asset->employee->x instead of $asset->$employee->x.
Notice the $.
When you write $asset->$employee it assumes $employee exists, which does not, hence the error.
In my Demo application, use Pagerfanta to read only a few rows from Database.
public function findAllProductsByCategory($category, int $page = 1): Pagerfanta
{
// DQL = Always select full classes from classes
$query = $this->getEntityManager()
->createQuery('
SELECT p
FROM App\Entity\Product p
WHERE p.category = :category
')
->setParameter('category', $category);
return $this->createPaginator($query, $page);
}
Later in Twig, I loop the preselected results. Everything is fine ...
{% if products.haveToPaginate %}
<div class="navigation text-center">
{{ pagerfanta(products, 'default', {routeName: 'category_show_paginated', 'routeParams' : { 'id': category.id}}) }}
</div>
{% endif %}
<table border="1" cellpadding="5">
<tr>
<th>#</th>
<th>Name</th>
<th>Products</th>
<th>Description</th>
</tr>
{% for product in products %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ product.name }}</td>
<td>{{ product.description }}</td>
</tr>
{% endfor %}
</table>
but on each page, "loop.index" is the same 1,2,3,4 ...
I would like to show the number of results 41,42,43 ... with pagerfanta ;)
Is there a special function?
Thank you
Haven't worked with Pagerfanta for a while, but what you want should be doable with something like this:
{{ pagerfanta.currentPageOffsetStart() + loop.index }}
Example test case: https://hotexamples.com/examples/pagerfanta/Pagerfanta/getCurrentPageOffsetStart/php-pagerfanta-getcurrentpageoffsetstart-method-examples.html
Form query i have several rows record
Controller:
public function statusTaskAction()
{
$em = $this->getDoctrine()->getManager();
$statusAll = $em->getRepository( 'MyBundle:StatusTask' )
->allStatusQuery();
return [ 'rows' => $statusAll ];
}
statusTask.html.twig
<table class="table ">
<tbody>
{% for entry in row %}
<tr>
<td>{{ entry.endDate is null ? '' : entry.endDate }}</td>
<td>{{ entry.groupID }}</td>
<td>{{ entry.name }}</td>
<td>{{ entry.info }}</td>
</tr>
{% else %}
<tr>
<td colspan="4">No rows</td>
</tr>
{% endfor %}
</tbody>
</table>
groupID displayed status:
g - "Green"
r - "Red"
f - "Future to plan"
p - "Planed"
I'll try to separate result by groupID and for this result add label, decription to each displayed grupID in TWIG and separate it in TWIG.
How can I do it in TWIG?
Since row is an Entity, you don't call the column name, but instead the Entity getters like so:
<table class="table ">
<tbody>
{% for entry in rows %}
<tr>
<td>{{ entry.getDate is null ? '' : entry.getDate }}</td>
<td>{{ entry.getGroupID }}</td>
<td>{{ entry.getName }}</td>
<td>{{ entry.getInfo }}</td>
</tr>
{% else %}
<tr>
<td colspan="4">No rows</td>
</tr>
{% endfor %}
</tbody>
</table>
I presume that would be the naming in your Entity class for the Entity StatuTask. Also, I think you need to use rows as specified in your controller.
UPDATE
Think I may have gone off course here, but I still get the same result with the whole page being displayed in the table.
So I am doing this for both the view and status pages. View is the default page which shows all active alerts. Status is the page that will display the filtered alerts. So each of these pages has a controller
public function viewAction()
{
$repository = $this
->getDoctrine()
->getManager()
->getRepository('NickAlertBundle:Alert');
$alerts = $repository->getAllActiveAlerts();
return $this->render('NickAlertBundle:Page:view.html.twig', array(
'alerts' => $alerts,
));
}
public function getActivatedAction(Request $request)
{
$alertStatus = $request->request->get('status', 'active');
$alerts = $this->getDoctrine()->getRepository('NickAlertBundle:Alert')->getAlertByStatus($alertStatus);
return $this->render('NickAlertBundle:Page:status.html.twig', array("alerts"=>$alerts));
}
Each of these pages then has a route
NickAlertBundle_view:
pattern: /view-alerts
defaults: { _controller: NickAlertBundle:Alert:view }
requirements:
_method: GET
NickAlertBundle_status:
pattern: /view-alerts
defaults: { _controller: NickAlertBundle:Alert:getActivated }
requirements:
_method: POST
NickAlertBundle_view calls the view action and renders the page with all active alerts. NickAlertBundle_status calls the getActivated action.
Both of these views are the same, their content is as follows
{% extends 'NickAlertBundle::layout.html.twig' %}
{% block main %}
<div class="col-md-12">
<section class="panel panel-default">
<header class="panel-heading">
<h3 class="panel-title">View Alerts</h3>
<select name="alerts" id="alerts" data-url="{{ path('NickAlertBundle_status') }}">
<option value="active">Active</option>
<option value="inactive">Inactive</option>
</select>
</header>
<div class="panel-body">
<div class="row" id="alert-container">
<table width="100%" cellpadding="0" cellspacing="0" border="0" id="datatable" class="table">
<thead>
<tr>
<th>Id</th>
<th>Search Command</th>
<th>Flight Number</th>
<th>Booking Class</th>
<th>Alert Pseudo</th>
<th>Alert Status</th>
</tr>
</thead>
<tbody>
{% for alert in alerts %}
<tr>
<td>{{ alert[0].id }}</td>
<td>{{ alert[0].searchCommand }}</td>
<td>{{ alert.flight_number }}</td>
<td>{{ alert.classes }}</td>
<td>{{ alert.pseudos }}</td>
<td>{{ alert[0].alertStatus }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</section>
</div>
{% endblock %}
So the data url should call the status route. Both of the views have this because they should always be able to change the select on either page.
And then the javascript is pretty much the same as you have shown me.
UPDATED
The problem is basicaly that you are embedding the html response from viewAction when you actually have to put the code for the alerts, for example:
public function getActivatedAction(Request $request)
{
$alertStatus = $request->request->get('status', 'active');
$alerts = $this->getDoctrine()->getRepository('NickAlertBundle:Alert')->findAllByStatus($alertStatus);
return $this->render('NickAlertBundle:Alerts:show.html.twig', array("alerts"=>$alerts));
}
on the NickAlertBundle:Alerts:show.html.twig
{# NickAlertBundle:Alerts:show.html.twig #}
{% for alert in alerts %}
<tr>
<td>{{ alert[0].id }}</td>
<td>{{ alert[0].searchCommand }}</td>
<td>{{ alert.flight_number }}</td>
<td>{{ alert.classes }}</td>
<td>{{ alert.pseudos }}</td>
<td>{{ alert[0].alertStatus }}</td>
</tr>
{% endfor %}
So then in your NickAlertBundle:Page:view.html.twig :
$('#alerts').change(function(){
$.ajax({
type: "POST",
url: $('#alerts').attr('data-url'), # getActivatedAction route
data:{ status: $(this).val() },
success: function(data){
$('#alert-container table tbody').html(data);
}
});
});
You can add a methond in your Enity Repository File:
# src/Package/AppBundle/Entity/AlertRepository.php
<?php
namespace Package\AppBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* AlertRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class AlertRepository extends EntityRepository
{
public function findAllByStatus($status = "active")
{
$qb = $this->createQueryBuilder('a');
$query = $qb
->where(
$qb->expr()->eq('a.alertStatus', $status)
)
->orderBy('a.id', 'DESC')
->getQuery();
return $query->getResult();
}
}
and then in your controller, you can pass a json with the data or html:
<?php
...
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
...
public function getActivatedAction(Request $request)
{
$alertStatus = $request->request->get('status', 'active');
$alerts = $this->getDoctrine()->getRepository('AppBundle:Alert')->findAllByStatus($alertStatus);
// With JSON
$json = alerts->toJson() // your own method
$response = new JsonResponse();
$response->setData($json);
// With HTML
return $this->render('AppBundle:Alerts:show.html.twig', array("alerts"=>$alerts));
}
Now in yor html file you can use ajax to request the alerts:
<select name="alerts" id="alerts">
<option value="active">Actives</option>
<option value="inactive">Inactive</option>
</select>
<div id="alertsList"></div>
<script>
$('#alerts').change(function(){
$.ajax({
type: "POST",
url: "{{ path('alerts_getActivated') }}",
data:{ status: $(this).val() },
success: function(data){
$('#alertsList').html(data); //Using the html response. With json you have to create the style and html here
}
});
});
</script>
If I didn't miss anything it should work.
If I get you wrong, please let me know, and I hope this will help you.
The $('#alert-container table tbody').html(data); line will set the contento of your tbody table from the response that the server return, so you have to have two different files
{% NickAlertBundle:Page:view.html.twig %}
{% extends 'NickAlertBundle::layout.html.twig' %}
{% block main %}
<div class="col-md-12">
<section class="panel panel-default">
<header class="panel-heading">
<h3 class="panel-title">View Alerts</h3>
<select name="alerts" id="alerts" data-url="{{ path('NickAlertBundle_status') }}">
<option value="active">Active</option>
<option value="inactive">Inactive</option>
</select>
</header>
<div class="panel-body">
<div class="row" id="alert-container">
<table width="100%" cellpadding="0" cellspacing="0" border="0" id="datatable" class="table">
<thead>
<tr>
<th>Id</th>
<th>Search Command</th>
<th>Flight Number</th>
<th>Booking Class</th>
<th>Alert Pseudo</th>
<th>Alert Status</th>
</tr>
</thead>
<tbody>
{% for alert in alerts %}
<tr>
<td>{{ alert[0].id }}</td>
<td>{{ alert[0].searchCommand }}</td>
<td>{{ alert.flight_number }}</td>
<td>{{ alert.classes }}</td>
<td>{{ alert.pseudos }}</td>
<td>{{ alert[0].alertStatus }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</section>
</div>
{% endblock %}
and
{# NickAlertBundle:Page:status.html.twig #}
{% for alert in alerts %}
<tr>
<td>{{ alert[0].id }}</td>
<td>{{ alert[0].searchCommand }}</td>
<td>{{ alert.flight_number }}</td>
<td>{{ alert.classes }}</td>
<td>{{ alert.pseudos }}</td>
<td>{{ alert[0].alertStatus }}</td>
</tr>
{% endfor %}
To solve the url problem you should put:
NickAlertBundle_view:
pattern: /view-alerts
defaults: { _controller: NickAlertBundle:Alert:view }
methods: [GET]
NickAlertBundle_status:
pattern: /view-alerts
defaults: { _controller: NickAlertBundle:Alert:getActivated }
methods: [POST]
I have an attribute immobilier.ref that is not displayed with twig
this is the code
{% for immobilier in listImmobilier %}
<tr>
<td>{{ immobilier.id }}</td>
<td>{{ immobilier.ref }}</td>
<td>{{ immobilier.titre }}</td>
<td><span class="glyphicon glyphicon-eye-open" style="display:block; text-align:center"></span></td>
<td>Edit</td>
<td>Delete</td>
</tr>
{% endfor %}
this is a photo of the table
It(s fine now, it was because I have a function named ref in the entity
/**
* #ORM\PostPersist()
*/
public function ref()
{
$id = $this->getId();
$operation = $this->getOperation()->getRef();
$this->setRef($id.$operation);
}
I have changed her name and it work fine now.