Giter Site home page Giter Site logo

Comments (8)

rappasoft avatar rappasoft commented on May 17, 2024

If you have a specific role you can grab the users with the relationship, as specified here:

https://github.com/rappasoft/laravel-5-boilerplate/blob/master/app/Role.php

$role = Role::find(1)->users

from laravel-boilerplate.

xnhinzkyx avatar xnhinzkyx commented on May 17, 2024

Thanks for this but I'm new to laravel can you show me how to display it using this function?

public function getUsersPaginated($per_page, $status = 1, $order_by = 'id', $sort = 'asc') {
return User::where('status', $status)->orderBy($order_by, $sort)->paginate($per_page);
}

Thanks.

from laravel-boilerplate.

rappasoft avatar rappasoft commented on May 17, 2024

I'm confused, exactly what are you trying to display with a function?

from laravel-boilerplate.

xnhinzkyx avatar xnhinzkyx commented on May 17, 2024

I'm trying to display users with a role moderator for example using that function getUsersPaginated().

Like return User::where('status', $status)->orderBy($order_by, $sort)->paginate($per_page)->role('moderator');

from laravel-boilerplate.

rappasoft avatar rappasoft commented on May 17, 2024

Try something like:

User::whereHas('role', function($q) {
    $q->where('name', '=', 'moderator');
})->where('status', $status)->orderBy($order_by, $sort)->paginate($per_page);

That may work, untested.

from laravel-boilerplate.

xnhinzkyx avatar xnhinzkyx commented on May 17, 2024

Thanks but it gives me an error:

BadMethodCallException in Builder.php line 2003:
Call to undefined method Illuminate\Database\Query\Builder::role()

Do i need to add something?

from laravel-boilerplate.

rappasoft avatar rappasoft commented on May 17, 2024

It might be roles instead. Or it might not work at all because it's a pivot table.

from laravel-boilerplate.

xnhinzkyx avatar xnhinzkyx commented on May 17, 2024

Yeah it doesn't work for me but still thank you for your support.

from laravel-boilerplate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.