Buenos días quería saber como hacer la siguiente relación sql:
Tabla clientes:
id PRIMARY KEY auto increment,
company varchar(45) not null,
holding varchar (45) not null,
id_holding int not null
Cabe aclarar que esta tabla no la cree en mysql ni sqlserver ni otro, solamente la realicé acá para que se pueda entender la pregunta, asi que puede que la tabla tenga algún error.
Mi idea es que la tabla contenga jerarquía en si misma, es decir
---------------------------------------------------
| id | company | id_holding| holding |
--------------------------------------------------
| 1 | Disney | 0 |Disney |
| 2 | Espn | 1 |Disney |
| 3 | Fox | 1 |Disney |
| 4 | Fox sports| 3 |Fox |
----------------------------------------------------
Esta tabla muestra que disney no tiene una compañia padra o jefe si no que el holding es la compañia misma. Pero en el caso de Espn y Fox que pertenecen a Disney llevan como holding a Disney y por ende el id del holding es el id de disney. Y por ultimo tenemos a Fox sports que por mas que Fox pertenezca a Disney, tambien puede tener una compañia como subordinada. Espero que se pueda entender el ejemplo de manera correcta
Ahora yo tengo mi modelo Customer donde quiero realizar esta relación pero no logro entender como hacerlo:
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $fillable = [
'holding',
'company',
'legal_entity',
'mobile_no',
'email',
'country',
'state',
'city',
'address',
'postal',
'cuit',
'website',
'status'
];
Y este es mi metodo store de la migration create_customers_table:
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->id();
$table->string('holding');
$table->string('company')->nullable();
$table->string('legal_entity')->nullable();
/*$table->string('mobile_no');
$table->string('mobile_two')->nullable();
$table->string('mobile_three')->nullable();*/
$table->string('email');
/*$table->string('position')->nullable();*/
$table->string('country')->nullable();
$table->string('state')->nullable();
$table->string('city')->nullable();
$table->string('address')->nullable();
$table->string('postal')->nullable();
$table->string('cuit');
$table->string('website')->nullable();
$table->tinyInteger('status')->default('1');
$table->integer('id_parentCompany')->nullable();
$table->integer('created_by')->nullable();
$table->integer('updated_by')->nullable();
$table->timestamps();
});
}
Pero como pueden ver al id_parentCompany que sería el id del holding le puse nullable porque no sabía como hacer para que me cargue en el registro el id del holding.