How to get username from articles in Laravel 5? - php

Let's say each article is related with a user by holding a user_id value that is a foreign key to the id field in the users table.
And we defined the user method inside Article model with $this->belongsTo('App\User'), and the article method inside User model with $this->hasMany('App\Article').
What is the proper way to get the articles along with the username of the creator, in order to use them in the view?

If your Article belongs to a User, then like this:
$articles = Article::with('user')->paginate();
Your Blade template:
#foreach ($articles as $article)
<li>{{ $article->title }} by {{ $article->user->username }}</li>
#endforeach

Related

Laravel - How to access related object other relashionship

I have 3 Models "Category", "Post", and "User".
A Category has a hasMany relationship with Post. And a Post has a belongsTo relationship with User.
I have a Category object $cat1 and i can access its posts (and the user_id) in my view, but i can't access more user data (line name)
#foreach ($cat1->posts as $post)
{{ $post->title }}
{{ $post->user()->name }}
#endforeach
This throws an error
Undefined property:
Illuminate\Database\Eloquent\Relations\BelongsTo::$name
You can access it like this:
{{ $post->user->name }}
When you call the function, it's to query the relationship.

Laravel how to take from another table?

I have a table called "logs" with custom column "auth_id" and i have another table called "users" with same column name "auth_id" and column "username".
How can i take it from "logs" with "logs.auth_id" and get username by "users" table?
Now i'm getting results by:
$match->messages = DB::table('logs')->where('match_id', '=', $match->match_id)->get();
Blade:
#foreach ($match->messages as $message)
{{ $message->auth_id }}:
{{ $message->message }}
Example: Now i'm getting AUTH5556 with {{ $message->auth_id }} but i need to get USERNAME from "users" table by {{ $message->auth_id }}
Don't use joins, this is the whole reason for using something like laravel (eloquent). You need to define a relationship between your models.
On your user model you would define a method
public function logs()
{
return $this->hasMany(Log::class);
}
On your log model
public function user()
{
return $this->belongsTo(User::class);
}
Now you need to make sure on your logs table you have a user_id column.
Now you can simply get the log for example
$log = Log::find($id);
And you can retrieve any information about the user who owns that log, for an example in a blade file.
{{ $log->user->username }}
To get reverse information you need to iterate through the results as the user can have many logs. Eg:
$user = User::find($id);
Then you'd do something like this in your blade
#foreach($user->logs as $log)
{{ $log->column_on_log_you_want }}
#endforeach
You also need to look into eager loading.
This is what makes laravel so clean and nice to use.
Read through the documentation
You need to use JOIN in your query to get username from users table. Here is the link to Laravel manual

Get commentable class attributes using Baum in Laravel

I am using Laravel Commentable that uses Baum
Post Model
class Post extends Model
{
use Commentable;
....
My user model name is User
Comment table structure:
The user_id stores the user id of the user who commented, commentable_id stores the post id of the post where the comment was placed.
The Comments are working as expected.
I am able to insert comments, delete comments.
To display the comments:
$comments = Comment::orderBy('id', 'desc')->get();
#foreach($comments as $comment)
{{ $comment->user->name }} : {{ $comment->body }}
#endforeach
This gives me the name and the comment by the user in the view.
Question:
How to get the post model attributes from a comment?
{{ $comment->post->slug }} <-this doesn't works
Per the code from this package, you have to use the commentable relation. However, there is no way to be sure that this will be a Post model, this can be any model that is commentable.
An example that checks if the commentable is actualy a post object and shows the slug if so:
#if ($comment->commentable_type === \App\Post::class)
{{ $comment->commentable->slug }}
#endif

laravel Eloquent: Relationships get third part relation

iam working in journal project and i have 3 main table related
Volumes->issues->articles
volume has many issue but issue has one volume
issues has many articles but article has one issue
all working fine but i have 1 tricky step
i want to get volume name when view all articles. i want view the following when retrieving articles
article name | vol name/issue name
i success to get issue name as i have relation between article module and issue module but i dont know how to get volume name inside for-each articles
<tr>
<th>Title</th>
<th>Vol/Issue</th>
</tr>
#foreach($articles as $article)
<td>{{ $article->title }}
<td>{{ ??? }} / {{$article->issue->name}}</td>
#endforeach
Assuming you have the belongs to relations set up in each model to act as the reverse of your has many relations:
class Article extends Model
{
public function issue()
{
return $this->belongsTo(Issue::class);
}
}
class Issue extends Model
{
public function volume()
{
return $this->belongsTo(Volume::class);
}
}
You should be able to eager load the issue relation and the issue's volume relation when retrieving your articles, like this:
$articles = Article::with(['issue', 'issue.volume'])->get();
and in your view (Assuming you use the name attribute):
{{ $article->issue->volume->name }}
For more information regarding this, look up "Nested Eager Loading" in the documentation

LARAVEL 5.3, Get User Name and Role Name (Using Laravel Model Relationships)

Testgrammatical or spelling errors
► clarify meaning without changing it
► correct minor mistakes
► add related resources or links
► always respect the original author
Get the data using eager loading:
$user = User::where('id', $userId)->with('roles')->first();
Then display the data:
{{-- Display full name --}}
{{ $user->first_name.' '.$user->last_name }}
{{-- Display all role names of the user --}}
#foreach ($user->roles as $role)
{{ $role->name }}
#endforeach
I guess relation here is many to many, so you need to change roles() relation to belongsToMany()
If you're using some package with many to many relationship, but you're only attach one role for user, you can display role name with this:
{{ $user->roles->first()->name }}
Also, you could use accessor to get full name:
public function getFullNameAttribute($value)
{
return $this->first_name.' '.$this->last_name;
}
To display full name using accessor:
{{ $user->full_name }}
#foreach($users as $user)
{{$user->name}} {{$user->roles->first()->name}}
#endforeach

Categories