Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

What is the value of $p in the welcome.blade.php.

I think the paginate makes a array of objects.

0

$p has a value of 3. Actually I want to retrieve the category name from the categories table using the category id stored in posts table.

0

Ok, I think i understand,

The db call you are making is bypassing the Eloquent table, so you will not have access to any functions in the model class.

This is an alternative to fetch the data,

$posts = Post::with('category')->orderBy('id', 'desc')->paginate(5);

The model class with a relation to the category

class Post extends Model implements SluggableInterface {

	protected $table = 'posts';

	public function category () {
  		return $this->hasOne('Full\Name\Space\To\Model\Category', 'id', 'category');
	} 
}

In the blade template, I'm assuming it is something like this,

@foreach ($posts as $p)

   // $p is now a model class object

	{{ $p->category->category }} // assuming the column in the category table is named category
@endforeach

There are some other ways to do what your asking, but this was easiest i think to accomplish it.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

dumbo22 dumbo22 Joined 6 Feb 2016

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.