Sorting timestamps in Eloquent - laravel

Newbie here in Laravel 4. Say I have a model ("Activity") that has a timestamp column, along with other metadata. When I print it out in HTML, it is arranged from earliest to latest, which won't be ideal.I want to sort it from latest to earliest, because I'm going after an activity log.
Here's the code on the view:
#foreach ($activities as $activity)
<tr class="activity">
<td>{{ $activity->timestamp }}</td>
<td>{{ $activity->activity }}</td>
</tr>
#endforeach
Here's the controller code, which arranges these stuff:
public function getActivity()
{
return View::make('app.website.activity', array('activities' => Activity::all()));
}
Is there anything I can do with Activity::all() to sort it out by time?
Thanks!

If you are using the default laravel timestamps you can get the activities in descending order like this:
Activity::orderBy('created_at','desc')->get();
If you have manually added your timestamp column you can get them in descending order like this:
Activity::orderBy('timestamp','desc')->get();
Hope this is what you are looking for.

Related

How to traverse to correct model when I have composite primary key

I am struggling to figure out how to display the proper data to the view when I have a composite primary key.
I have two tables involved:
user_to_drug_mappings: contains columns for user_id, pharma_ApplNo, pharma_ProductNo
pharma_products: contains columns for ProductNo, ApplNo, Form. The combination of the ProductNo and ApplNo in this table form a composite primary key. I am not sure if that has to be defined somewhere in my migrations.
I have the following in my controller:
//Query UserToDrugMappings table for all of user's records
$drugs = DB::table('user_to_drug_mappings')
->where('user_id', $user)
->get();
return view ('my-health-hub', compact('drugs'));
I have the following in my view:
#foreach($drugs as $drug)
<tr>
<td>{{$drug->pharma_ApplNo }}</td>
<td>{{$drug->pharma_ProductNo }}</td>
</tr>
#endforeach
Now I need to somehow use the ApplNo and ProductNo to search the pharma_products table and display the Form column within my view. I don't know how I would do this

Display data with relationship in Laravel & Vue

I'm trying to display data with a relationship (using many-to-many) within my Vue component.
I store users in object from rest API:
users: {},
//...
axios.get("api/user").then(({data}) => (this.users = data));
An example of JSON returned from api/user:
{"current_page":1,"data":[{"id":2,"name":"Andrew","description":"Testing","role":[{"id":2,"role_title":"TestTitle","pivot":{"user_id":2,"role_id":2}}
When displaying the data I can do
<tr v-for="user in users.data" :key="user.id">
<td>{{user.name}}</td> // DOES work, displays 'Andrew'
However if I try to do:
<td>{{user.role.role_title}}</td> //does NOT work, should display 'TestTitle'
It displays nothing, what am I doing wrong?
user.role is an array of roles. To display the first role (assuming there will always be at least one role) you can do:
<td>{{ user.role[0].role_title }}</td>

Passing and printing a value of string from controller to views in laravel

This is probably a very simple question.But I am stuck. This is my controller function where I am tryng to pass the sum of columns in the views to be printed at the end of a table
public function totalBillc3()
{
$total = Collection::where('collector_id', '=', 3)->sum('package');
return View::make('users.collector3', compact('total',$total));
}
And in the views I have written
<tr>
<td colspan="4" class="noborders"></td>
<th class="text-right" scope="row">TOTAL</th>
<td class="text-right">{{ $total}}</td>
</tr>
I have my route setup perfectly but the error is showing
Undefined variable: total (View: /Volumes/G/zipbillingsoft.com/resources/views/users/collector3.blade.php). Please help.
compact() takes one or more strings as arguments, then looks for variables named like those strings.
In other words, you should not do
compact('total', $total)
but rather just
compact('total')
And if you have multiple variables, do
compact('total', 'something', 'something_else')
Documentation: http://php.net/compact
You made a mistake in you View::make in using function compact. Try the following code:
return View::make('users.collector3', compact('total',['total']));

returning multiple variables to view

So I am new to Laravel.
I know how to pass an array to a view. I am trying to alter that basic concept by passing other variable to the view as well.
Here is my route:
Route::get('sites', function()
{
$thosting = DB::table('sites')
->select(DB::raw('sum(hosting_fee) as thosting'))
->where('active', True)
->get();
$tdomain = DB::table('sites')
->select(DB::raw('sum(domain_fee) as tdomain'))
->where('active', True)
->get();
$atotal = $thosting + $tdomain;
$sites = Site::where('active', True)->orderBy('updated_at', 'DESC')->get();
return View::make('sites')
->with('sites', $sites)
->with('thosting', $thosting)
->with('tdomain', $tdomain)
->with('atotal', $atotal);
//var_dump($sites);
});
here is view sites.blade.php
#extends('layouts.master')
#section('content')
<div class="container well">
<table class="table-condensed">
<th>Annual Hosting</th><th>Annual Domain Name</th><th>Total Annual Income</th>
<tr>
<td>{{ thosting }}</td>
<td>{{ tdomain }}</td>
<td>{{ atotal }}</td>
</tr>
</table>
<h1>Hosting Accounts - Nearest to expiration </h1>
#foreach($sites as $site)
<p>{{ $site->host_renewal_date }} - {{$site->site}}</p>
#endforeach
#stop
</div>
The $site->Id etc displays a list of sites, as expected. but the 3 new variables I passed, thosting, tdomain and atotal, cause an error when I try to display them like {{ thosting }. The error is ErrorException, defined (E_unknown)
Use of undefined constant thosting - assumed 'thosting' (View: C:\Users\myname\Desktop\websites\laravel\first\app\views\sites.blade.php)
I assume I am not accessing the variables right I tried
echo thosting
that did not work either. The docs does not address returning multiple variables or not array variables. Any help from a seasoned vet is appreciated. In the end, all im trying to figure out is how to return to the view data from several DB queries.
thanks
You need to use $ in PHP variables also inside Blade views, otherwise it will try to find a constant. So just change to
<td>{{ $thosting }}</td>
<td>{{ $tdomain }}</td>
<td>{{ $atotal }}</td>
OK thanks everyone. This is what I finally got to work:
{{ $thosting[0]->thosting }}
Print_r gave me the clue that there was a property name for the object, and since i had 'sum(hosting_fee) as thosting'. I tried to point to thosting(the AS name) and it worked. After all it is an array. Great learning experience.
Would be nice if there was a way to add all these learning examples to the Laravel documentation, which only has several examples of what is possible with a lot left out.
thanks all.

Laravel 4 adding custom attributes to User Model

I'm using this custom package to integrate my SugarCRM into Laravel 4. I'm storing basic user info in my laravel database which I use to access the rest of the users data that is stored in Sugar.
For instance, I have the company name stored into my project database. I use that to find the website, email address, etc. which is stored in Sugar. I don't want duplicate info anywhere, so I figured I would just get the other necessary Sugar user data on the fly.
The problem: I was hoping to append the Sugar data retrieved to the User Model but it's proving difficult and apparently that was the intention of the creator. I'm doing this in my view:
#foreach ($users AS $user)
<tr>
<td>{{ $user->username }}</td>
<td>{{ $user->main_contact_c }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->role }}</td>
<td>[ Get From Sugar API ]</td>
<td><a data-toggle="modal" href="#confirm-user-delete"><span class="glyphicon glyphicon-remove"></span></button></td>
</tr>
#endforeach
I know it's bad design to call model methods from a view so I'm not sure how to properly access my custom data that pertains to each record. I could create the function getMainContactCAttribute() in the User Model but I can't pass info to the function in the view obviously...What are some legit things I can do?

Resources