Asked  7 Months ago    Answers:  5   Viewed   38 times

I have 3 tables; users, groups and permissions

In models I have the relationships set as belongsToMany in user model:

public function groups() {
    return $this->belongsToMany('Group');
}

in group model:

public function users() {
    return $this->belongsToMany('User');
}

public function permissions() {
    return $this->belongsToMany('Permission');
}

in permissions model:

public function groups() {
    return $this->belongsToMany('Group', 'id');
}

many users - to - many groups many groups - to - many permissions

I'm trying to get all the permissions a user has, and have no clue what the code for it should look like. Can anyone help?

 Answers

24

This is how you can do it:

User::where('id', $id)->with(['groups.permissions' => function ($q) use (&$permissions) {
     $permissions = $q->get()->unique();
}])->first();

// then
$permissions; // collection of unique permissions of the user with id = $id
Wednesday, March 31, 2021
 
McAn
answered 7 Months ago
33

You may try this:

$recipes = Recipe::with(array('user', 'categories' => function($q) use ($cat_id) {
    $q->where('id', $cat_id);
}))->get();

Changes following:

public function category() {
    return $this->belongsToMany('Category','category_recipe');
}

To (category should be categories in Recipe.php):

public function categories() {
    return $this->belongsToMany('Category','category_recipe');
}

Btw, you may also use join like this (Using Eloquent model, if you need in any case):

Recipe::join('category_recipe', 'recipes.id', '=', 'category_recipe.recipe_id')
      ->join('category', 'category.id', '=', 'category_recipe.category_id')
      ->join('users', 'users.id', '=', 'recipes.user_id')
      ->where('category.id', '=', $cat_id)->get();

Update: You may also try this:

$recipes = Recipe::whereHas('categories', function($q) use ($cat_id) {
    $q->where('id', $cat_id);
})->with('user')->get();
Wednesday, March 31, 2021
 
Farnabaz
answered 7 Months ago
63

You can create getter methods to handle these:

In your Post model, create new methods something along the lines of:

public function getCategories()
{
    return $this->hasMany()->where('taxonomy', 'post_category');
}

public function getTags()
{
    return $this->hasMany()->where('taxonomy', 'post_tag');
}
Saturday, May 29, 2021
 
keyBeatz
answered 5 Months ago
79

It's quite hard to understand what you want to achieve but I'll try. First of all as @particus mentioned the best way is to create pivot table when you don't need to worry about such things.

But the solution if you have list of ids in a columns separated by coma is not storing values like

1,2,3

but always adding , at the beginning and at the end, so it should be in this case:

,1,2,3,

This way, if you have in your table ,19,2,3, and you want to search for value 9, you should use look for ,9, string, for example:

$id = 9; 
$items = Items::where('column', LIKE '%,'.$id.',%')->get();

Now for above string no record will be found, but if you have ,9,2,3, or just ,9, the desired record will be found.

Thursday, September 2, 2021
 
Pradip
answered 2 Months ago
50

I'm adding another answer since it has nothing to do with my original answer. Using the new info you posted, I'm calling the table/entity you posted "Follower". The original entity, "User".

What happens if you create the following associations:


namespace AcmeUserBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * AcmeUserBundleEntityUser
 *
 * @ORMTable()
 * @ORMEntity
 */
class User
{
    /**
     * @var integer $id
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORMOneToMany(targetEntity="AcmeFollowerBundleEntityFollower", mappedBy="followeduser")
     */
    protected $followers;

    /**
     * @ORMOneToMany(targetEntity="AcmeFollowerBundleEntityFollower", mappedBy="followeeuser")
     */
    protected $followees;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    public function __construct()
    {
        $this->followers = new DoctrineCommonCollectionsArrayCollection();
    $this->followees = new DoctrineCommonCollectionsArrayCollection();
    }

    /**
     * Add followers
     *
     * @param AcmeFollowerBundleEntityFollower $follower
     */
    public function addFollower(AcmeFollowerBundleEntityFollower $follower)
    {
        $this->followers[] = $follower;
    }

    /**
     * Add followees
     *
     * @param AcmeFollowerBundleEntityFollower $followee
     */
    public function addFollowee(AcmeFollowerBundleEntityFollower $followee)
    {
        $this->followees[] = $followee;
    }    

    /**
     * Get followers
     *
     * @return DoctrineCommonCollectionsCollection 
     */
    public function getFollowers()
    {
        return $this->followers;
    }

    /**
     * Get followees
     *
     * @return DoctrineCommonCollectionsCollection 
     */
    public function getFollowees()
    {
        return $this->followees;
    }
}


namespace AcmeFollowerBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * AcmeFollowerBundleEntityFollower
 *
 * @ORMTable()
 * @ORMEntity
 */
class Follower
{
    /**
     * @var integer $id
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="AcmeUserBundleEntityUser", inversedBy="followers")
     * @ORMJoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $followeduser;

    /**
     * @ORMManyToOne(targetEntity="AcmeUserBundleEntityUser", inversedBy="followees")
     * @ORMJoinColumn(name="followee_id", referencedColumnName="id")
     */
    protected $followeeuser;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set followeduser
     *
     * @param AcmeUserBundleEntityUser $followeduser
     */
    public function setFolloweduser(AcmeUserBundleEntityUser $followeduser)
    {
        $this->followeduser = $followeduser;
    }

    /**
     * Get followeduser
     *
     * @return AcmeUserBundleEntityUser 
     */
    public function getFolloweduser()
    {
        return $this->followeduser;
    }

    /**
     * Set followeeuser
     *
     * @param AcmeUserBundleEntityUser $followeeuser
     */
    public function setFolloweeuser(AcmeUserBundleEntityUser $followeeuser)
    {
        $this->followeeuser = $followeeuser;
    }

    /**
     * Get followeeuser
     *
     * @return AcmeUserBundleEntityUser 
     */
    public function getFolloweeuser()
    {
        return $this->followeeuser;
    }
}

I'm not sure if this would do the trick, I really don't have much time to test it, but if it doesn't, I thnk that it's on it's way. I'm using two relations, because you don't need a many to many. You need to reference that a user can have a lot of followers, and a follower can follow a lot of users, but since the "user" table is the same one, I did two relations, they have nothing to do with eachother, they just reference the same entity but for different things.

Try that and experiment what happens. You should be able to do things like:


$user->getFollowers();

$follower->getFollowedUser();

and you could then check if a user is being followed by a follower whose user_id equals $userThatIwantToCheck

and you could search in Followers for a Follower whose user = $user and followeduser=$possibleFriend

Thursday, September 2, 2021
 
user1024047
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :