Hello,
is there a way in Laravel to store Data for a Model in two different tables? Or to build relations for that.
I code for a game where I have a Model "Devplan" which relates to a Model "Resource".
Now I need to store some values for each Devplan and then values for an ID of Devplan for each Resource, as the User has to pay for each Devplan with different amounts of Resources. How can I handle that?
So my tables would look like:
Schema::create('devplans', function(Blueprint $table)
{
$table->increments('id');
$table->integer('time');
$table->integer('points');
$table->integer('building_id');
$table->integer('level_id');
$table->timestamps();
$table->softDeletes();
});
Schema::create('devplan_resource', function(Blueprint $table)
{
$table->increments('id');
$table->integer('devplan_id');
$table->integer('resource_id');
$table->integer('value');
$table->timestamps();
$table->softDeletes();
});
So I will have multiple entries in devplan_resource for each row in devplans, which hold the values for each Resource (Silver, Gold etc) that the Player has to pay to get the next "Devplan" => Level in the Game.
Hope someone could help me.
Thanks Kay
Yeah, relationships are insanity easy with Laravel. Take a look at the docs: http://laravel.com/docs/4.2/eloquent#relationships
If you keep scrolling down, after eager loading, you will learn how to query and insert data for related models.
Once set up, you can pull out related data really easy. It's one of Laravels sling points.
Hope it helps.
ok, I now can save these relationship.
But how can I set the value for $table->integer('value') which I also need in this table?
It sends on the relationship type you have set up. One to one, one to many etc?
The docs for inserting are here: http://laravel.com/docs/4.2/eloquent#inserting-related-models
You have a few different options depending on the type of relationship.
Let me know if you get stuck and I'll try to help.
Another thing maybe to look into is perhaps using repositories? That way you can make a meta object that on the frontend is simply accessing both tables but as if they were in a single table
Sounds like a good idea, but I actually solved it like this:
$user->roles()->attach(1, array('expires' => $expires));
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community