I am trying to group users by date that is in the relationship accesses and page this result.
Model User
public function acessos()
{
return $this->hasMany(Acessos::class,'id_usuario');
}
Model Acessos
public function user()
{
return $this->belongsToMany(User::class,'id_usuario');
}
Livewire component
$data = User::query()
->search($this->search)
->with('acessos')
->has('acessos')
->paginate($this->perPage);
return view('livewire.relatorios.acessos.acessos-component',[
'data' => $data
]);
Remove the user() method from Acessos Model and change the hasMany() method to belongsToMany in User model's acessos method. This Will solve your problem.
Remove this:
public function user()
{
return $this->belongsToMany(User::class,'id_usuario');
}
And change this:
public function acessos()
{
return $this->belongsToMany(Acessos::class,'id_usuario');
}
Related
I've added a custom method called customMethod to a model, like so:
class Prize extends Model {
customMethod(){
return 'test'
}
}
When I use find to get a prize by primary key I can call this method no problem
const prize = await Prize.find(1);
return prize.customMethod();
//returns 'test'
but when if I get a prize any other way, through a relationship or by querying by a field, I can't access this method.
const countrysPrizes = await country.prizes().fetch();
for (const key in countrysPrizes) {
if (countrysPrizes.hasOwnProperty(key)) {
const prize = countrysPrizes[key];
return prize.customMethod();
//returns 500 - prize.customMethod is not a function
}
}
How can I access this method while iterating through multiple of the object?
You need to use <fetched_object>.rows because of VanillaSerializer
Example code :
const user = await User.find(1);
const posts = await user.posts().fetch();
posts.rows.forEach(post=> { // use .rows
console.info(post.customMethod()); // Your custom method
});
I've had trouble using the basic foreach loop. So I used .foreach()
An output example with fetch() :
I am trying to write a seed file for users and profiles with a one to one relationship and currently getting an "error: relation 'user_profiles' does not exist". From digging around, it seems like Adonis will assume this as a pivot table in the case of a many to many relationship. What I (think or intend to) have is a one to one relationship between users and profiles. Thanks in advance! Newbie to SQL and Adonis. As a side note, the user persists to the db, but there is no corresponding profile.
// My User Schema
class UserSchema extends Schema {
up () {
this.create('users', (table) => {
table.increments('id')
table.string('username', 80).notNullable().unique()
table.string('email', 254).notNullable().unique()
table.string('password', 60).notNullable()
// table.integer('profile_id').unsigned().references('id').inTable('userprofiles')
table.timestamps()
})
}
down () {
this.drop('users')
}
}
// My Profile Schema
class UserprofileSchema extends Schema {
up () {
this.create('userprofiles', (table) => {
table.increments()
table.string('first_name')
table.string('last_name')
// table.integer('user_id')
// .unsigned().references('id').inTable('users')
table.integer('user_id')
.unsigned()
.index('user_id')
table.foreign('user_id')
.references('users.id')
table.timestamps()
})
}
down () {
this.drop('userprofiles')
}
}
My User model includes the following relationship definition:
profile () {
return this.hasOne('App/Models/UserProfile')
}
// Seed script
class UserSeeder {
async run () {
try {
const user = await Factory.model('App/Models/User').create()
const userProfile = await Factory.model('App/Models/UserProfile').make()
userProfile.user_id = user.id
await user.profile().save(userProfile)
} catch (e) {
console.log('Error From Seeder: ', e);
}
}
}
Error code '42P01' and can post whole body if needed. Thanks!
On your Model userProfile, set table name as follows.
class User extends Model {
static get table () {
return 'userprofiles'
}
}
My Post Model like:
namespace App;
use Illuminate\Database\Eloquent\Model;
class **Post** extends Model
{
protected $table = 'posts';
protected $guarded = ['id'];
public function user(){
return $this->belongsTo('App\User','user_id','id');
}
}
And User Model like:
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $table = 'users';
protected $guarded = ['id'];
public function userType(){
return $this->belongsTo('App\User','user_type_id','id');
}
}
Now if I call the following in my controller:
$posts = \App\Post::with('user.userType')->where('status', 1)->get();
It returns all the posts for posts.status = 1, with users table data and also user_type.
What should I do??, if I want to set a where condition for users table or for user_types table like: where('user.id', 5) or where('user_types.id', 1).
Is it possible?? if yes please answer.
Thanks in advance.
I have a very weird problem. When I'm submitting the form, it throws an error with server-side validation.
BadMethodCallException
Method username does not exist.
LoginController.php
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $username = 'username';
protected $redirectTo = '/dashboard';
protected $guard = 'web';
public function getLogin()
{
if (Auth::guard('web')->check())
{
return redirect()->intended('dashboard');
}
return view('login');
}
public function postLogin(Request $request)
{
$auth = Auth::guard('web')->attempt(['username' => $request->username(),
'password' => $request->password(), 'active' => 1]);
if ($auth)
{
return redirect()->route('dashboard');
}
return redirect()->route('/');
}
public function getLogout()
{
Auth::guard('web')->logout();
return redirect()->route('/');
}
}
The problem is that you trying to access the input property as method, I mean $request->username() and same with password, there is no username method, that's why you are getting error. you can access input with input() method like $request->input('username') or via dynamic property $request->username
read more at docs
In Symfony2 RC3, I am trying to create a related entity on a User object (FOSUserBundle) at the point of user creation so that I can display the appropriate fields on an edit profile form. I am doing the following in the RegistrationFormHandler.
class RegistrationFormHandler
{
protected $request;
protected $userManager;
protected $form;
public function __construct(Form $form, Request $request, UserManagerInterface $userManager)
{
$this->form = $form;
$this->request = $request;
$this->userManager = $userManager;
}
public function process($confirmation = null)
{
$user = $this->userManager->createUser();
$this->form->setData($user);
if ('POST' == $this->request->getMethod()) {
$this->form->bindRequest($this->request);
if ($this->form->isValid()) {
if (true === $confirmation) {
$user->setEnabled(false);
} else if (false === $confirmation) {
$user->setConfirmationToken(null);
$user->setEnabled(true);
}
$prog = new \MyBundle\CoreBundle\Entity\Programme();
$prog->setStartDate(date_create());
$prog->setEndDate(date_create());
$prog->setWeeklyTarget(4);
$prog->setGoal('');
$user->addProgrammes($prog);
$this->userManager->updateUser($user);
return true;
}
}
return false;
}
}
The programme record does get created in the database but with a null user_id so it seems the association isn't working correctly. Anyone know what might be causing this?
The solution was to do $programmes->setUser($this); in the addProgrammes method of my User entity