returning multiple variables to view - laravel

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.

Related

Diff for humans not working in laravel blade

Hi am trying to use diffForHumans in laravel blade but its not working.please help`enter code here
Heres my code
#foreach($students as #student)
$time=$student->created_at->diffForHumans()
#php echo $time;#endphp
#endforeach
$student->created_at is a string, hence you can't call diffForHumans() function on it. Instead, you should parse it via Carbon instead. Here's how to do it
#foreach($students as #student)
#php
$time= \Carbon::parse($student->created_at)->diffForHumans()
#endphp
//Now do what you need to do
#endforeach
Also, you can't just write php code inside blade. If you do that, you need to contain the code inside #php tags as shown above. Although, it's not really clean to write php code in blade. With that being said, if you just want to display the time, here's how I would do it
#foreach($students as #student)
<p>Created At : {{ \Carbon::parse($student->created_at)->diffForHumans() }}</p>
#endforeach
All You need is to parse the date using Carbon like this
{{ Carbon\Carbon::parse($student->created_at)->diffForHumans() }}

get value by name in relational database

by using this code in resource file
#forelse($teachers as $teacher)
{{$teacher->name}} {{$teacher->subjects}}<br>
#empty
Nothing
#endforelse
getting this value
helooo [{"id":3,"name":"Hindi","created_at":"2017-11-18 12:43:33","updated_at":"2017-11-18 12:43:33","pivot":{"teacher_id":16,"subject_id":3}}]
malik [{"id":2,"name":"English","created_at":"2017-11-18 12:43:12","updated_at":"2017-11-18 12:43:12","pivot":{"teacher_id":17,"subject_id":2}}]
hello [{"id":1,"name":"Maths","created_at":"2017-11-18 12:42:34","updated_at":"2017-11-18 12:42:34","pivot":{"teacher_id":18,"subject_id":1}}]
but by using
{{$teacher->name}} {{$teacher->subjects->name}}
getting error
Property [name] does not exist on this collection instance
how can i get just the name value in laravel
Since it's many to many, you need to iterate over subjects of each teacher:
#forelse($teachers as $teacher)
{{ $teacher->name }}<br>
#foreach ($teacher->subjects as $subject)
{{ $subject->name }}<br>
#endforeach
#empty
Nothing
#endforelse
Looks like the data that is being returned is an object in an array. Try adding a zero to select the first obj in this arr. Like this:
$teacher->subjects[0]->name
Good luck!

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']));

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?

Sorting timestamps in Eloquent

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.

Resources