how can I pull all the posts of a certain user? should i foreach all the posts of a certain user? Like if user1 posted something and I want to pull out whatever he posted and only shows when he's logged in.
In Laravel, if you are using Eloquent relationships things would look something like this.
First, define your relationships within your models. If you're following the example below, I'm going to assume the following.
Your users table in database is named users. Your posts table in
database is named posts, and has an integer column named user_id
corresponding to the id of the user the post belongs to. If your
table and column names are different, make sure you read the Laravel
docs to learn how to set custom table names and column names for
Eloquent models.
app/models/User.php
<?php
class User extends Eloquent {
// All the default User.php stuff....
public function posts()
{
return $this->hasMany('Post');
}
}
app/models/Post.php
class Post extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}
After we've defined those relationships, you can get a particular user using something like this.
$user = User::find(1); // This will get the user with an ID of one.
Then, you retrieve that user's posts, we can use the Eloquent relationship we defined earlier.
$posts = $user->posts()->all();
If the user is already logged in, you can user the Auth class to do this for the logged in user.
$posts = Auth::user()->posts()->all();
You'll now realize that ->posts() actually returns a Collection, and you can use all of the Query and Eloquent methods on that collection as normal.
Related
I have two migration tables and two models
1)User.
2)Subscription.
in subscriptions Model i write one method called user() which has hasMany relationship and in Users model it will contain subscriptions() method which have belongs() relationship.
in user table id acts as an primary key and in subscription table sub_id acts as a foriegn key,for example if the user subscribed to any subscription the user id will store in the sub_id in the subscription table upto this it's working fine.
$is_subscription=Subscription::where('sub_id',$user->id)->value('sub_id');
from the above code i am able to get the value but now what i want is is there any better way to write the query based on models ,please help me to write this one..
According to your descriptions, I believe this is what you have:
class User extends Model
{
public function subscriptions()
{
return $this->hasMany(Subscription::class);
}
}
class Subscription extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
So you want to check whether an user has any subscriptions. You can do one of the followings:
$user->subscriptions()->exists();
// Or
$user->subscriptions->isNotEmpty();
You can use pluck() method:
$is_subscription=Subscription::where('sub_id',$user->id)->pluck('sub_id');
You can find details on the methods that are available for collection using the Laravel Documentation
I'm working on a small project using Laravel, and I would like to create a like and dislike system, so a user can like one to many more users.
I already have a table called Users (default by Laravel), now I have created another table called favorite_user with id, user_id and liked_the_user.
How can I get all the users that a user already liked? Is my table correct or should I change something?
All what I want to do is add like and show the people I liked. I don't know how to make this relationship since I am new to Laravel.
Thank you
Looks like you have a pivot table there so a Many to Many relationship is what you need to setup. You will need to pass some additional arguments since the naming won't fit with convention.
User Model:
// users the user has liked
public function liked()
{
return $this->belongsToMany(self::class, 'favorite_user', 'user_id', 'liked_the_user');
}
// users who like the user
public function likes()
{
return $this->belongsToMany(self::class, 'favorite_user', 'liked_the_user', 'user_id');
}
Controller:
public function liked(Request $request)
{
// get liked users for current user
$liked = $request->user()->liked;
// get liked users for an arbitrary user
$liked = User::findOrFail(...)->liked;
...
}
Laravel 8.x Docs - Eloquent - Relationships - Many to Many - belongsToMany
I'm facing an issue with model extension. Here's an example of my problem:
I have a User model and an Admin model that extends my User model. I use a github repo called Bouncer for permissions. When I save my roles for an Admin model, it saves as /App/Admin and for Users, it saves as /App/User for the model reference.
So when I call my roles for a Admin or for a User, no problem. But my issue is when I want to query all my users with their roles. I obviously get all my users AND my admin, but the Admins can't get their roles because the are "/App/Admin" in the database.
How can I get all the roles of my "extended" models when I call the parent?
You will create a relationship in your model using belongs to and use a method called "with ()".
First step:
In your model, create belongs to ex:
public function post()
{
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}
Second step:
You will use it in your controller,example:
$users = User::with('podcasts')->get();
Eloquent: Relationships
https://laravel.com/docs/7.x/eloquent-relationships#updating-belongs-to-relationships
Another example
Get Specific Columns Using “With()” Function in Laravel Eloquent
I have a giveaway table, it has a winner_id column in which the user ID on the site is entered. It is necessary that the ID from the winner_id column looks in the users table and displays the user login that has found on the site. How can this be done correctly?
You are looking for a One to Many (inversed) relationship from the "Giveway" model side. First, you need to create a "Giveaway" model which represents your "giveaway" database table, in case you don't have that already. You should have the "user" model, as this exists by default. Your "giveaway" model could look like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Giveaway extends Model
{
/**
* Get the user that is related to the giveaway.
*/
public function user()
{
return $this->belongsTo('App\User', 'id', 'winner_id');
}
}
Now, get the giveaway instance and you can do something like this:
// this will print the user instance which is associated to the giveaway row with id #1
dd(Giveaway::find(1)->user);
For further details, please check the Laravel Docs: https://laravel.com/docs/6.x/eloquent-relationships#one-to-many-inverse
Trying to figure what table I need to make this relationship to work...
I'd like the user to mark a post as favorite and save it so when he log back in, he can view all his favorite posts.
So I have User and Post models and their relationships of course.
User hasMany Post, Post belongTo User
Do I need to add Favorite model and add fav_id to both User and Post models ?
Or maybe adding pivot table?
If a favorite is defined as a simple relationship between users and posts, you don't need a model for it. However, you do want a pivot table, call it something like favorites. It would contain id, post_id, user_id, created_at, updated_at.
Then in your user model:
public function favoritePosts()
{
return $this->belongsToMany('Post', 'favorites');
}
And in the post model:
public function favoritedBy()
{
return $this->belongsToMany('User', 'favorites');
}
So now, for any given user, you can do $user->favoritePosts to get an array of posts that user has "favorited".
And from a post object, you can do $post->favoritedBy, to get an array of users that have favorited that post.
I haven't tested this but that seems like that would work off the top of my head.