MongoDB isn't a relational database, and from the looks of it, you are trying to treat it like one, I suggest using a relational database instead, since you clearly store relational data.
Jul 19, 2024
1
Level 1
Laravel NOVA mongodb issue.
Hello guys. I am using Mongodb and Nova to build my super-admin.
if I don't add this function in any of my models,
my model will not load it hasMany relationships. Still, if I add it, the model will return an object as its id when it is displayed via belongsTo or hasmany on other resources.
Could you tell me how I fix this issue? I have tried to cast to a string, but hasMany relationships will refuse to work. I am out of ideas.
public function getIdAttribute($value = null)
{
return $value;
}
http://127.0.0.1:8000/nova/resources/properties/6697c74ae5e7d53c581178bf
http://127.0.0.1:8000/nova/resources/properties/[Object object]
namespace App\Models;
use MongoDB;
use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Relations\BelongsTo;
class CustomerModel extends Model
{
protected string $collection = 'customers';
protected $connection = 'mongodb';
protected $casts = ['createdAt'=>'datetime',
'deletedAt'=>'datetime',
'updatedAt'=>'datetime',
];
public function getBusiness(): BelongsTo
{
return $this->belongsTo(BusinessesModel::class, 'business', '_id');
}
public function getTenancy(): \Illuminate\Database\Eloquent\Relations\HasMany|MongoDB\Laravel\Relations\HasMany
{
return $this->hasMany(TenancyModel::class, 'tenant', '_id');
}
public function getTransactions(): \Illuminate\Database\Eloquent\Relations\HasMany|MongoDB\Laravel\Relations\HasMany
{
return $this->hasMany(TransactionsModel::class, 'customer', '_id');
}
public function getInvoices(): \Illuminate\Database\Eloquent\Relations\HasMany|MongoDB\Laravel\Relations\HasMany
{
return $this->hasMany(InvoiceModel::class, 'customer', '_id');
}
}
Please or to participate in this conversation.