Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

The error doesn't come from the code you showed, it's probably in the view and the reason for that is that I believe you try to do something like this:

$category->types->tipo();

That being said, you try to call Types method on the Collection of Types models not on a single model.

Last updated 2 years ago.
0

OK. I hope to explain better.

I have two tables

mysql> select * from eventi_tipo;

+----+-----------+--------------+
| id | Type     | category_id |
+----+-----------+--------------+
|  2 | Opera     |            1 |      
|  3 | Film       |            2  |      
|  4 | Balletto   |            1  |
|  6 | Horror     |            2 |
+----+-----------+--------------+
4 rows in set (0.00 sec)

mysql> select * from categories;

 +----+-----------+
 | id | category | 
+----+-----------+
 |  1 |  Teatro     |
 |  2 | Film      |
+----+-----------+
2 rows in set (0.00 sec)

Now in my controller Types I have this

public function index()
{
	$types = Types::with('categories')->get();
	return View::make('eventi_tipo.index', $type);
}

And in my models:

<?php

class Category extends \Eloquent {

protected $fillable = [];

protected $table = 'categories';

public function types()
{
	return $this->hasMany('Types','category_id');
}
}
<?php class EventiTipo extends \Eloquent { protected $fillable = []; protected $table ='types'; public function categories() { return $this->belongsTo('Categories'); } } The problem is the in my result I have the ID of th categories and not their name (Teatro, Film)
Last updated 2 years ago.
0

Solved

<?php

class Type extends \Eloquent {

protected $fillable = [];

protected $table ='types';

public function categories()
{
	return $this->belongsTo('Categories','category_id');
}

}

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

gnardella gnardella Joined 27 Apr 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.