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?
Related
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
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>
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!
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.
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.