Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

Have you played around with Laravel's migrations and seeding? That might be your best solution. If you are unsure about you want to design your database, Migrations makes creating and dropping DB objects very easy until you have a design you are pleased with.

URL: This is a link

Here is an example users table. You would still need to refer to the link I posted if you want to go this route.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

	public function up()
    {
        Schema::create('users', function($table)
        {
            $table->increments('id');
            $table->string('email')->unique();
            $table->string('favorite_pizza');
            $table->string('username');
            $table->string('password');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('users');
    }
}
?>
Last updated 1 year ago.
0

Yes, I'm aware of migrations, but that's not the issue here. It's not that I want to add properties to all objects, I want all objects (users, to continue the example) to have different properties. Ie, user #1 has the property "Age = 5", while user #2 has the property "Middle name = Gary". The properties is different for each object, and there can be anything from 0 to 1000 properties per object (ie, per row in the database, not per type of object).

One real-life example is that users can create custom html forms. I have pre-defined fields they can add to their form (for example, single line text, multiline text, checkbox and so on). Each field has different properties - text fields might have a "placeholder" property, while checkboxes has a "checked" property.. Another field that accept dates might have a "mindate" and "maxdate" property.

The question is more - how do I design such properties in the database, and connect this through Eloquent so that I can do something like

$fields = Field::whereForm($myFormId)->with("properties.values")->get();

And get the linked properties. The properties would be a collection of property objects, containing something like:

echo $myField->properties[0]->name; // output: "placeholder"
echo $myField->properties[0]->value; // output: "Type Here" (string)
echo $myField->properties[1]->name; // output: "max_length"
echo $myField->properties[1]->value; // output: 30 (int)
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ErikEklund erikeklund Joined 14 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.