Answer the question
In order to leave comments, you need to log in
How to link models?
There is a table with users:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('servers', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('ip')->unique();
$table->string('login');
$table->string('pass');
$table->string('country')->nullable();
$table->string('image_url')->nullable();
$table->double('ping')->nullable();
$table->integer('personalPrice');
$table->integer('sharedPrice');
$table->integer('uptime')->default(100);
$table->timestamps();
});
Schema::create('server_user', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('user_id')->unsigned()->onDelete('cascade');
$table->integer('server_id')->unsigned()->onDelete('cascade');
$table->timestamps();
});
Schema::create('purchased_servers', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->date('purchase_date')->nullable();
$table->date('payment_date')->nullable();
$table->boolean('status');
$table->integer('server_id')->unsigned();
$table->foreign('server_id')->references('id')->on('server_user')->onDelete('cascade');
$table->timestamps();
});
Answer the question
In order to leave comments, you need to log in
public function server()
{
return $this->belongsToMany('App\ServerModel', 'server_user', 'user_id', 'server_id');
}
In the user model, try adding this method, only specify your ServerModel
If server_user and purhcased_servers are not related to each other, then add user_id to purchased_servers and that's it, hasMany comes out.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question