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>
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 want to save comments, posts and profiles that a user views in a viewable table. Posts and comments are displayed in a stream similar to social networks. How can I store the data in a viewable table in the background?
Table:
viewables
user_id
viewable_id
viewable_type
For example, if a user views or sees a post in the stream, the database should store the user_id and post_id. The same should be possible with comments and profiles.
EDIT:
The posts are issued in a forelse loop and the comments in a foreach.
#forelse ($posts as $post)
#if ($post->type === 1)
{{$post->body}}
#foreach ($post->comments as $comment)
{!!$comment->body!!}
#endforeach
#elseif ($post->type === 2)
{{$post->image}}
#foreach ($post->comments as $comment)
{!!$comment->body!!}
#endforeach
#elseif ($post->type === 3)
show post with type 3
#elseif ($post->type === 4)
show post with type 4
#else
show remaining
#endforelse
Based on your edit, there are a few different ways to handle this. Technically, since all posts are included on the page, it could be assumed that the user has read them all and simply save the user id for all of the posts loaded before rending the view.
If you wanted something more refined, however, you could use an ajax method to post the user id, post id, etc. when scrolling over it, though I don't find that very practical, as that could cause problems if someone scrolls through too fast.
The best option (without requiring user interaction) would be to load a couple posts at a time. Either automatically when scrolling (see this answer), or with the help of Laravel's pagination (https://laravel.com/docs/5.7/pagination) and set the user id when loading the data.
Example:
$posts = Posts::paginate(5);
foreach($posts as $p) {
DB::table('viewables')->insert([
'user_id' => $user->id,
'viewble_id' => $p->id,
`viewable_type` => $p->type,
]);
}
I have a search result view that has a generated list of tables from another sites API. I'm trying to pass off the Artist ID to another next action via GET. The Artist ID will be then used to interact with the API.
I try to pass off the Artist ID like so but I receive a 'array_combine(): Both parameters should have an equal number of elements'.
The view file in question:
#foreach($parser->Artists->Artist as $Artist)
<table class = "table table-striped">
<thead>
{{ var_dump(strval($Artist['ID']))}}
<h3>{{ $Artist['ListName'] }}</h3>
</thead>
<tbody>
The vardump was to test that a string was in fact being passed off to the action(). I can confirm that it is, before it was a simple XML object.
Here's the corresponding route:
Route::get('/artist/detail/{$artist}','ArtistController#detail');
And the controller action:
<?php
// app/controllers/ArtistController
class ArtistController extends BaseController
{
public function detail($Artist) {
return 'Artist ID is ' . $Artist;
}
{
The problem was that Laravel was too smart. My $Artist variable was not read as a string but as a model because of the binding the routes file.
routes.php
Route::model('artist','Artist');
I changed $Artist from $Artist_ID and it worked just fine.
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?
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.