php - How to access all table data in many many relationship laravel -


i have 3 tables.

1.posts

2.categories

3.category_post.

my posts tables here :

+------------+------------------+------+-----+---------------------+----------------+ | field      | type             | null | key | default             |          | +------------+------------------+------+-----+---------------------+----------------+ | id         | int(10) unsigned | no   | pri | null                | auto_increment | | title      | varchar(255)     | no   |     | null                |                | | slug       | varchar(255)     | no   |     | null                |                | | reporter   | varchar(255)     | no   |     | null                |                | | meta       | varchar(255)     | no   |     | 0                   |                | | body       | text             | no   |     | null                |                | | image      | varchar(255)     | no   |     | 0                   |                | | top        | tinyint(1)       | no   |     | 0                   |                | | post_count | int(11)          | no   |     | 0                   |                | | created_at | timestamp        | no   |     | 0000-00-00 00:00:00 |                | | updated_at | timestamp        | no   |     | 0000-00-00 00:00:00 |                | +------------+------------------+------+-----+---------------------+----------------+ 

my categories table here:

+---------------+------------------+------+-----+---------------------+----------------+ | field         | type             | null | key | default             |          | +---------------+------------------+------+-----+---------------------+----------------+ | id            | int(10) unsigned | no   | pri | null                | auto_increment | | name          | varchar(255)     | no   |     | null                |                | | category_slug | varchar(255)     | no   |     | null                |                | | parrent_id    | int(11)          | no   |     | 0                   |                | | created_at    | timestamp        | no   |     | 0000-00-00 00:00:00 |                | | updated_at    | timestamp        | no   |     | 0000-00-00 00:00:00 |                | +---------------+------------------+------+-----+---------------------+----------------+ 

my category_post table:

+-------------+------------------+------+-----+---------+----------------+ | field       | type             | null | key | default |          | +-------------+------------------+------+-----+---------+----------------+ | id          | int(10) unsigned | no   | pri | null    | auto_increment | | category_id | int(10) unsigned | no   | mul | null    |                | | post_id     | int(10) unsigned | no   | mul | null    |                | +-------------+------------------+------+-----+---------+----------------+ 

i can create post multiple category selected . when want showing post category, can't access category table . means, want view category name each post .

here models:

in category model:

public function posts() {     return $this->belongstomany('post'); } 

in post model:

public function categories() {     return $this->belongstomany('category'); } 

my query helper function post category:

public static function cat_post($category, $limit, $top) {     $posts = post::wherehas('categories', function($q) use ($category, $top)         {             $q->where('name', 'like', $category);             $q->where('top', 'like', $top);          })->with('categories')->take($limit)->orderby('created_at', 'desc')->get();     return $posts; } 

i can view post data . category name not .

my post loops:

<?php $headline = helper::head_post(10, 1); ?>    @foreach ($headline $post)     <li><a href="">{{ $post->title }}</a></li>   @endforeach 

when try category name not working:

  @foreach ($headline $post)     <li><a href="">{{ $post->categories->name }}</a></li>   @endforeach 

please me.

because it's many many relation $post->categories->name not work. doesn't know category (of many) want name of.

so example use $post->categories()->first()->name name of first category or loop on them name of categories.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -