Show Pivot Table

Hello, I have two tables users_group & companies which are linked to a pivot table company_user_group and I can't display my data in my UsersGroupResource. Thanks for your help
10 Replies
Becker Maxime
Becker Maxime14mo ago
Model UserGroup
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
* @property integer $id
* @property integer $company_id
* @property string $name
* @property string $created_at
* @property string $updated_at
* @property Company $company
*/
class UserGroup extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users_group';

/**
* @var array
*/
protected $fillable = ['name', 'created_at', 'updated_at'];

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/

public function companies()
{
return $this->belongsToMany(Company::class)->withPivot(['company_id']);
}

}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
* @property integer $id
* @property integer $company_id
* @property string $name
* @property string $created_at
* @property string $updated_at
* @property Company $company
*/
class UserGroup extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users_group';

/**
* @var array
*/
protected $fillable = ['name', 'created_at', 'updated_at'];

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/

public function companies()
{
return $this->belongsToMany(Company::class)->withPivot(['company_id']);
}

}
Dan Harrin
Dan Harrin14mo ago
also what is happening when you try errors?
Becker Maxime
Becker Maxime14mo ago
I have no error but nothing is displayed form :
protected function getFormSchema(): array
{

return [
Card::make()
->schema([
TextInput::make('name')
->label('Nom')
->disabled(function () {
$result = true;
if (Gate::check('update_users::group') === true) {
$result = false;
}
return $result;
})
->required()
->maxLength(255),
TextInput::make('company_id'),
Select::make('company_id')
->label('Entreprises autorisées')
->multiple()
->options(Company::pluck('name', 'id'))
// ->disabled(function () {
// $result = true;
// if (Gate::check('update_users::group') === true) {
// $result = false;
// }
// return $result;
// })
// ->options(Company::all()->pluck('name', 'id'))->searchable()
->required(),
])->columns(2),
];
}
protected function getFormSchema(): array
{

return [
Card::make()
->schema([
TextInput::make('name')
->label('Nom')
->disabled(function () {
$result = true;
if (Gate::check('update_users::group') === true) {
$result = false;
}
return $result;
})
->required()
->maxLength(255),
TextInput::make('company_id'),
Select::make('company_id')
->label('Entreprises autorisées')
->multiple()
->options(Company::pluck('name', 'id'))
// ->disabled(function () {
// $result = true;
// if (Gate::check('update_users::group') === true) {
// $result = false;
// }
// return $result;
// })
// ->options(Company::all()->pluck('name', 'id'))->searchable()
->required(),
])->columns(2),
];
}
Dan Harrin
Dan Harrin14mo ago
dont you need a CompanyUserGroup resource
Becker Maxime
Becker Maxime14mo ago
I know, I haven't created any CompanyUserGroup models but I would like to display in my multiple select the companies corresponding to the userGroup
Dan Harrin
Dan Harrin14mo ago
thats the job of select relationship() method right?
Becker Maxime
Becker Maxime14mo ago
Select::make('company_id')
->relationship('company_user_group', 'company_id')
->label('Entreprises autorisées')
->multiple()
->options(Company::pluck('name', 'id'))
Select::make('company_id')
->relationship('company_user_group', 'company_id')
->label('Entreprises autorisées')
->multiple()
->options(Company::pluck('name', 'id'))
I have the impression that does not know my pivot table
Dan Harrin
Dan Harrin14mo ago
wrong relationship name? should be companies
Becker Maxime
Becker Maxime14mo ago
I have already tried with companies and I still have nothing in my select
Dan Harrin
Dan Harrin14mo ago
wrong column name? should be companies, name maybe