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

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?

Last updated 2 years ago.
0

Sure, what do you need?

Last updated 2 years ago.
0

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 :)

Last updated 2 years ago.
0

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);	
Last updated 2 years ago.
0

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();
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

filoveg filoveg Joined 10 Sep 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.