Im trying to get the 'diversities()' from items with a pivot table (belongToMany).
What am I missing?:
** Client works fine and Item and Client are identical (almost). The error I get is
Base table or view not found: 1146 Table 'restigo_spm.diversity_item' doesn't exist
and I dont have a diversity_item anywhere in the code, why is it searching for it?
Client:
{
protected $fillable = ['name', 'diversity_id', 'type', 'enable'];
public function diversities()
{
return $this->belongsToMany('App\Models\Diversity');
}
}
ClientSchema:
Schema::create('clients', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('client_diversity_id')->nullable();
$table->string('name')->unique();
$table->enum('type', ['restaurant', 'coffee', 'bar']);
$table->boolean('enable');
$table->timestamps();
});
ClientDiversity (pivot):
class ClientDiversity extends Model
{
protected $table = 'client_diversity';
protected $fillable = ['diversity_id', 'client_id'];
}
ClientDiversitySchema:
Schema::create('client_diversity', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('client_id')->nullable();
$table->unsignedInteger('diversity_id')->nullable();
$table->timestamps();
});
Item:
class Item extends Model
{
protected $fillable = ['name', 'diversity_id', 'catalog_number', 'price', 'has_vat', 'enable'];
public function diversities()
{
return $this->belongsToMany('App\Models\Diversity');
}
}
ItemSchema:
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('item_diversity_id')->nullable();
$table->string('name');
$table->string('catalog_number')->unique();
$table->integer('price');
$table->boolean('has_vat');
$table->boolean('enable');
$table->timestamps();
});
Diversity:
class Diversity extends Model
{
protected $fillable = ['name', 'item_id', 'client_id', 'enable'];
public function clients()
{
return $this->belongsToMany('App\Models\Client');
}
public function items()
{
return $this->belongsToMany('App\Models\Item');
}
}
DiversitySchmea:
Schema::create('diversities', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->boolean('enable');
$table->timestamps();
});
Here is where I call it. The ClientControl code is exactly the same and works, but Item doesnt. ItemController:
class ItemController extends Controller
{
public static function index()
{
$items = Item::with('diversities')->get();
return new ItemCollection($items);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire