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.
Related
I have one to many relationships and need to show using where conditions.
When I use findOrFail() it's working as well.
$foo = Model::findOrFail(1);
on my template blade
#foreach($foo->bars as $index=>$bar)
{{ $bar->name }}
#endforeach
on my code above, it's working. but the reference to an id, that's not what I need.
I need it using where conditions. like this:
$foo = Model::where('conditon', 1)->get();
then I call it on my blade template with
#foreach($foo->bars as $index=>$bar)
{{ $bar->name }}
#endforeach
then I get an error:
ErrorException (E_ERROR) Property [bars] does not exist on this collection instance.
It seems after get() I cannot call child with $foo->bars
How do you get this to work?
The findOrFail() method returns an instance of the "Model".
The get() method returns a collection of instances of the "Model" even if there is only one result.
if you want just one result, use first() instead of get().
$foo = Model::where('conditon', 1)->first();
then in the blade template do
#if($foo)
#foreach($foo->bars as $index=>$bar)
{{ $bar->name }}
#endforeach
#endif
if you need multiple results, do another foreach().
#foreach($foo as $oneFoo)
#foreach($oneFoo->bars as $index=>$bar)
{{ $bar->name }}
#endforeach
#endforeach
if you are going with the "multiple" solution, i suggest you name your variable "foos".
$foos = Model::where('conditon', 1)->get();
and so
#foreach($foos as $foo)
#foreach($foo->bars as $index=>$bar)
{{ $bar->name }}
#endforeach
#endforeach
Try to use ->first() instead of ->get().
Because ->get() retrieve multiple results which fit the query criteria.
You can either loop through $foo results or use ->first() to retreive the first query match.
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
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
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
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