Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent

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

Last updated 3 years ago.
0

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.

Last updated 3 years ago.
0

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?

Last updated 3 years ago.
0

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.

Last updated 3 years ago.
0

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

Last updated 3 years ago.
0

Sounds like a good idea, but I actually solved it like this:

$user->roles()->attach(1, array('expires' => $expires));

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

kay899 kay899 Joined 7 Jul 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.

© 2025 Laravel.io - All rights reserved.