Viewed   82 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?



This is how you can do it:

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

// then
$permissions; // collection of unique permissions of the user with id = $id
Wednesday, September 14, 2022

This isn't the complete answer but it's close. No matter how I work the data though, I can't figure out how you are coming up with cash_has. Also I'm keeping it raw SQL because I feel it would be more helpful and shouldn't be difficult to convert back to query builder. I also don't know the exact column names so you may have to fix some of those.

    COALESCE(, AS outlet, 
    COALESCE(, AS delivery_boy,
    SUM(IF(cm.source_type = 'admin', amount, 0)) AS cash_taken,
    SUM(IF(cm.source_type = 'deliveryBoy', amount, 0)) AS cash_returned,
    SUM(IF(cm.source_type = 'admin', amount, 0)) - SUM(IF(cm.source_type = 'deliveryBoy', amount, 0)) AS cash_has
FROM delivery_cash_manages cm
LEFT JOIN outlets ON = cm.destination_id AND cm.source_type = 'deliveryBoy'
LEFT JOIN delivery_boys boys ON = cm.source_id AND cm.source_type = 'deliveryBoy'
LEFT JOIN outlets outlets_admin ON = cm.source_id AND cm.source_type = 'admin'
LEFT JOIN delivery_boys boys_admin ON = cm.destination_id AND cm.source_type = 'admin'
WHERE COALESCE(, = '2'  #  This is where you plug in your $outlet_id
GROUP BY outlet, delivery_boy

The reason you are getting an error with your query though is if you group by anything, you need to group by everything you select which are aren't aggregate columns (functions like sum, max, avg).

Saturday, November 12, 2022

My issue was that the id in the services table was an unique VARCHAR instead of the standard auto-incrementing INT. Fixing that and adding a name field cleared up the issue.

Monday, August 15, 2022
  1. Start by using composer require illuminate/database vlucas/phpdotenv

  2. create a bootstrap file to bootstrap Eloquent's connection string:

        require 'vendor/autoload.php';
        use IlluminateDatabaseCapsuleManager as Capsule;
        $dotenv = new DotenvDotenv(__DIR__);
        $capsule = new Capsule;
            'driver'    => env('DB_CONNECTION'),
            'host'      => env('DB_HOST'),
            'port'      => env('DB_PORT'),
            'database'  => env('DB_DATABASE'),
            'username'  => env('DB_USERNAME'),
            'password'  => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
  3. Add environment variables!! (.env file)

  4. Create a model file, you can put this anywhere you want

        namespace Models;
        use IlluminateDatabaseEloquentModel;
        class User extends Model
  5. Use them!

        use ModelsUser;
        use IlluminateDatabaseConnection as DB;
        $user = User::find(1);
        $user2 = User::where('name', 'somename')->first();
Friday, December 16, 2022

Finally got it working... Basically it's polymorphism + many to many relationship combined. I thought it don't require tags table. Taggables table acts as pivot table and tags is the table which contains Tag objects that connect models based on pivot table (taggables table)

Tuesday, October 18, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :