Primary codes in Laravel

0

You see, I have a language table with the following values:

Schema::create('idiomas', function (Blueprint $table) {
        $table->increments('id');
        $table->string('idioma');
        $table->string('nivel_hablado');
        $table->string('nivel_escrito');
        $table->string('titulo_oficial');
        $table->unsignedInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });

The variable 'language' is what the language is (English, German, Japanese, etc) while 'user_id' is the user of the database. I want both variables to be primary, which will allow the user_id or the language to match in 2 rows, but there can never be 2 rows with exactly the same language and user_id. How do I achieve that?

Edit: I have already seen that my table already has a primary key, the id. The story is that I want both language and user_id to be unique, but not individually, but there can not be the same language for the same user, but a user can have several languages and one language can be in several users.

In MySQL it would be achieved like this:

CREATE TABLE idiomas(
id int PRIMARY KEY NOT NULL,
idioma varchar(25) NOT NULL,
user_id varchar(2) NOT NULL,
UNIQUE 'idioma_usuario_uidx' ('idioma','user_id'));
    
asked by Miguel Alparez 06.05.2018 в 12:35
source

2 answers

1

For this new problem that you pose this is my answer.
In the documentation you have all the different types of indexes and how you can generate them. You are in need of a single composite index and that is like the composite index that I put in the previous answer only that instead of being a primary index in this case it is a unique index. In this way you will be able to have as many languages for each user as you wish.

Schema::create('idiomas', function (Blueprint $table) {
    $table->increments('id');
    $table->string('idioma');
    $table->unsignedInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users');

    $table->unique(['user_id', 'idioma']);
});

I recommend you read the documentation Creating Indexes to understand a little better.
Greetings and I hope I have been helpful

    
answered by 07.05.2018 / 14:04
source
0

You should have a table users , a table idiomas and another table idioma_user . The latter to describe the languages available to users.

I propose the following:

Migrations

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

Schema::create('idiomas', function (Blueprint $table) {
    $table->increments('id');
    $table->string('nombre');
    $table->timestamps();
});

Schema::create('idioma_user', function (Blueprint $table) { // tiene que ir en orden alfabetico y en singular al igual que los nombres de los modelos correspondientes
    $table->increments('id');
    $table->unsignedInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users');
    $table->unsignedInteger('idioma_id');
    $table->foreign('idioma_id')->references('id')->on('idiomas');
    $table->string('nivel_hablado');
    $table->string('nivel_escrito');
    $table->string('titulo_oficial');
    $table->timestamps();

    $table->index(['user_id', 'idioma_id']); // con esto no podrá haber un usuario con 2 idiomas. solo puede existir esta combinación.
});

User Model

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function idioma()
    {
        return $this->hasOne(\App\IdiomaUser::class);
    }
}

Model Language

namespace App;

use Illuminate\Database\Eloquent\Model;

class Idioma extends Model
{
    protected $fillable = [
        'name',
    ];

    public function users()
    {
        return $this->hasManyThrough(\App\IdiomaUser::class);
    }
}

IdiomaUser Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class IdiomaUser extends Model
{
    protected $fillable = [
        'user_id', 'idioma_id', 'nivel_hablado', 'nivel_escrito', 'titulo_oficial'
    ];

    public function user()
    {
        return $this->belongsTo(\App\User::class, 'user_id');
    }

    public function idioma()
    {
        return $this->belongsTo(\App\Idioma::class, 'user_id');
    }
}

With this you can for example do:

$user = auth()->user();
dd($user->idioma, $user->idioma->nivel_hablado);

You could also get the list of users by language

$idioma = Idioma::find(1);
dd($idioma->users);
    
answered by 06.05.2018 в 18:52