Hey filoveg, im sorry if this is completely off topic to you, but im really struggling implementing a category system that looks a lot like yours, would it in any way be possible for you to share me some more of your code regarding this?
Im thinking your models structure + DB schema? (if possible), and maybe the part of your controller that controls the various categories etc.
Hope its ok :)
ok here it is. although i still have things to do, some of which i dont know how xD
Schema::create('categories', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->timestamps();
});
Schema::create('subcategories', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->text('description');
$table->integer('category_id');
$table->timestamps();
});
Schema::create('threads', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->integer('user_id');
$table->integer('subcategory_id');
$table->timestamps();
});
Schema::create('posts', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->text('body');
$table->integer('user_id');
$table->integer('thread_id');
$table->timestamps();
});
class Category extends Eloquent {
public function subcategories()
{
return $this->hasMany('Subcategory');
}
}
class Subcategory extends Eloquent {
public function category()
{
return $this->belongsTo('Category');
}
public function threads()
{
return $this->hasMany('Thread');
}
}
class Thread extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
public function subcategory()
{
return $this->belongsTo('Subcategory');
}
public function posts()
{
return $this->hasMany('Post');
}
}
class Post extends Eloquent {
protected $fillable = array('title', 'body');
public function thread()
{
return $this->belongsTo('Thread');
}
public function user()
{
return $this->belongsTo('User');
}
}
// list of categories
$data = Category::with('subcategories')->get();
// list of threads in subcategory
$threads = Thread::where('subcategory_id', $id)
->with('user')
->paginate(15);
As to my problem, for fetching latest post for each subcategory, i think i'm close to a solution. Buuut.. this wont work because - Integrity constraint violation I can see where is the problem, but how do I specify desired table in subquery?
$data = Category::with(array('subcategories' => function($query){
$query->with(array('posts' => function($q){
$q->orderBy('id', 'desc')->first();
}))
->get();
}))
->get();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community