samedi 19 mai 2018

How to define dynamic model and relation in Laravel for database tables

I've 2 tables: courses: id, name, category_id and categories: id, name I wanted to define dynamic models for them along with dynamic relation. For this I've defined 3rd table that stores the meta(fields, foreign key) of above 2 tables. So far I've written following code:

    $entity = Entity::with(['fields'])->find(1);
    $model = new CurrentEntity();
    $model->setTable($entity->db_key);

    foreach($entity->fields as $field)
    {
        if($field->data_source == 'DATABASE')
        {
            $relatedModel = new RelatedEntity();
            $relatedModel->setTable($field->reference);
            $model->setRelation('categories', $relatedModel);
        }
    }

I could set table for CurrentEntity and get data for it. But I'm unable to set relation and get the data from related table.

How could I achieve the solution?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2IQwr2t
via IFTTT

Aucun commentaire:

Enregistrer un commentaire