0

I trying to make a CRUD called Usuarios(Users) with a foreing key from the table Perfiles(profiles) , the foreing key is the id from Profiles, but I donot know how to make it with a select

tHIS IS MY FORM.BLADE.PHP

<select name="idPerfil" id="idPerfil" class="form-control">
        <option value="">Eliga un nombre de perfil </option>
    @foreach ($perfiles as $perfil)
    
    <option value="{{ $perfil['id'] }}" 
    
 @if ($perfil->id === $usuario->idPerfil)
    selected
        
    @endif
    
    > {{ $perfil['NombrePerfil'] }}
        
    </option>      
        @endforeach
        </select>

PerfilesContoller.php

 public function edit($id)
{
    
    $perfil= Perfiles::findOrFail($id);

    return view('perfiles.edit',compact('perfil'));
}

public function create() {

    return view('perfiles.create');

}

THIS IS MY USUARIOSCONTROLLER.PHP

public function create()
    {
        //
        $perfiles = Perfiles::all();

        return view('usuarios.create',compact('perfiles'));
    }

public function edit($id) { //

        $usuario= Usuarios::findOrFail($id);
        $perfiles = Perfiles::all();

        return view('usuarios.edit',compact('usuario','perfiles'));
    }

Table Pefiles

public function up()
{
    Schema::create('perfiles', function (Blueprint $table) {

        $table->increments('id');
        $table->string('NombrePerfil')->unique();
        $table->timestamps();
        
    });
}

table usuarios

 public function up()
{
    
    
    Schema::create('usuarios', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('idPerfil');
        $table->string('Nombre');
        $table->string('UserName')->unique();
        $table->string('Email')->unique();
        $table->string('Password');
        $table->foreign('idPerfil')->references('id')->on('perfiles');
        $table->rememberToken();
        $table->timestamps();
    });
}
  • Welcome to SO ... where is this code where you try to create a new user and cannot? the only view you are showing seems to be for editing a user, not creating – lagbox Sep 03 '20 at 03:26
  • I have a Form.blade.php, create.blade.php and edit.blade.php , the Form.blade is shared with create and edit. – Alejandro Huerta Pérez Sep 03 '20 at 14:43

1 Answers1

0

For starters i think your accessing the primary key as if the perfil is an array at some point and then treat it like an object at another, your doing this: $perfil['id'] instead of this: $perfil->id. Try structuring your blade like so:

<select name="idPerfil" id="idPerfil" class="form-control">
    <option value="">Eliga un nombre de perfil </option>
    @foreach ($perfiles as $perfil)
    
    <option value="{{ $perfil->id }}" 
    
        @if ($perfil->id === $usuario->idPerfil) selected @endif> 
       
       {{ $perfil->NombrePerfil }}
        
    </option>      
    @endforeach
</select>
tsommie
  • 470
  • 4
  • 13
  • 1
    you can access the attributes of a model either way, they should probably stick to one, but it makes no difference functionally here – lagbox Sep 03 '20 at 02:15
  • True...there are so many things wrong with his question, first am not sure he specified a primary key column while declaring the relationship in the model cos if he didn't laravel will default to convention `perfil_id` so therefore `idPerfil` wont work. Second, `profile` should be a child of `user` and not the other way around not that it wont work eventually...I was only eliminating the immediate obvious. – tsommie Sep 03 '20 at 02:35
  • Do you want to see my tables? – Alejandro Huerta Pérez Sep 03 '20 at 14:44
  • I have edidted my Question – Alejandro Huerta Pérez Sep 03 '20 at 18:41