【问题标题】:Laravel - How to join table from posts to categoriesLaravel - 如何将表格从帖子连接到类别
【发布时间】:2012-12-27 00:53:27
【问题描述】:

这是我的表格数据。

  • 类别 [id] [category_name]

  • 帖子 [id] [category_id] [post_name]

我想做的是:

我想为 echo category_name 列出帖子并加入类别表。


这是我的控制器

class Form_data_Controller extends Base_Controller {


    function action_index() {

        $posts = new Post();
        $list_posts = $posts->list_posts();
        $view['list_posts'] = $list_posts;

        echo '<pre>';
        print_r( $list_posts );
        echo '</pre>';

        $view['pagination'] = $list_posts->links();

        // page title
        $view['page_title'] = 'Test list data';

        // create view and end.
        return View::make( 'form-data.index_v', $view );

    }// action_index


}

这是后期模型

class Post extends Eloquent {


    //public static $table = 'posts';


    public function categories() {
        return $this->belongs_to( 'Category' );
    }// categories


    function list_posts() {
        $query = $this
                ->order_by( 'post_name', 'asc' )
                ->paginate( '10' );

        return $query;
    }// list_posts


}

这是类别模型

class Category extends Eloquent {


    //public static $table = 'categories';


    public function posts() {
        return $this->has_many( 'Post' );
    }// categories


}

我想列出来自帖子模型的帖子 -> list_posts() 方法,因为我希望它在模型而不是控制器中完成,但我无法加入类别表以获取 category_name .

如何加入分类表得到category_name

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    多个控制器方法可能需要以不同的方式“列出帖子”,所以我不会把这个逻辑放在模型中。这就是我认为你应该完成你的问题的方式。否则,您可以将其设为静态方法并将其称为 $posts = Post::list_posts();

    class Form_data_Controller extends Base_Controller {
    
        function action_index() {
    
            $posts = Post::order_by( 'post_name', 'asc' )->paginate( '10' );
    
            return View::make( 'form-data.index_v')
                      ->with('title', 'Test list data')
                      ->with('posts', $posts);
        }
    }
    

    //application/models/Post.php

    class Post extends Eloquent {
    
        //public static $table = 'posts';
    
        public function category() {
            return $this->belongs_to( 'Category', 'category_id' );
        }
    }
    

    //application/models/Category.php

    class Category extends Eloquent {
    
        //public static $table = 'categories';
    
        public function posts() {
            return $this->has_many( 'Post' );
        }// categories
    }
    

    //application/views/form-data/index_v.blade.php

    <html>
    <head>
    <title>{{ title }}</title>
    </head>
    <body>
     {{ $posts->links() }}
    @foreach($posts as $post)
     {{ $post->category->name }}
    @endforeach
    </body>
    </html>
    

    【讨论】:

    • 如果表字段被称为 category_name 而不是 name$post-&gt;category-&gt;name 是否有效,或者是拼写错误?
    • 尝试购买错误... 未处理的异常消息:尝试获取非对象位置的属性:C:\test-laravel\laravel\view.php(386) : eval()'d code在第 37 行
    猜你喜欢
    • 2023-03-22
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多