1. Go to this page and download the library: Download shopapps/has-table-relation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shopapps / has-table-relation example snippets
use ShopApps\HasTableRelation\Illuminate\Database\Eloquent\Concerns\HasOtherRelationships;
use App\Models\Number;
class Customer extends Model
{
use HasOtherRelationships;
public function numbers()
{
return $this->hasTable(Number::class);
}
}
use ShopApps\HasTableRelation\Illuminate\Database\Eloquent\Concerns\HasOtherRelationships;
use App\Models\Customer;
class Number extends Model
{
use HasOtherRelationships;
public function customer()
{
return $this->belongsToTable(Customer::class);
}
// equivelent to ...
public function customer()
{
return $this->belongsToTable(Customer::class, 'first'); // calls $query->first() on the parent model
}
// another example...
public function customer()
{
return $this->belongsToTable(Customer::class, 'last'); // calls $query->last() on the parent model
}
// another example...
public function customers() // plural since we will be returning more than one ;-)
{
return $this->belongsToTable(Customer::class, 'all'); // calls $query->get() on the parent model
}
}
use App\Models\Customer;
use App\Models\Number;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public $table = 'customer_number'; // standard is to keep the table names in alphabetical order
public $columnOne = 'customer';
public $columnTwo = 'number';
/**
* Run the migrations.
*/
public function up(): void
{
if (!Schema::hasTable($this->table))
{
Schema::create($this->table, function (Blueprint $table)
{
$table->unsignedInteger("{$this->columnOne}_id")->index();
$table->unsignedInteger("{$this->columnTwo}_id")->index();
/* setup index across the two */
$table->primary(["{$this->columnOne}_id", "{$this->columnTwo}_id"]);
});
$this->buildData(); // populate the pivot table
}
}
public function buildData() {
/*
* find the first record in $columnOne and populate the pivot table with all records from $columnTwo
*/
$connection = Schema::getConnection();
/** @var Customer $customer */
$customer = new Customer();
$customer->setConnection($connection->getName());
$customer = $customer->first(); // my table only has one record, yours may be different, so collect and loop accordingly.
if($customer) {
/** @var Number $customer */
$numbers = new Number();
$numbers->setConnection($connection->getName());
$numbers = $numbers->get(); // get all the rows
if(count($numbers) > 0) {
$numbers = $numbers->pluck('id')->toArray(); // put id's into an array
$customer->numbers()->syncWithoutDetaching($numbers); // attach the id's to the pivot table
}
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists($this->table);
}
};
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.