What is the value of $p in the welcome.blade.php.
I think the paginate makes a array of objects.
$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.
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community