简体   繁体   English

laravel 4如何通过雄辩订购和加入

[英]laravel 4 how to order and join by eloquent

Im new in Laravel 4, and right now im coding for small project, i use laravel as framework to build my website, but my code i always wonder it's optimize or not because in my model i just wrote: 我是Laravel 4的新手,现在我正在为小型项目编码,我使用laravel作为框架来构建我的网站,但是我的代码我总是想知道它是否是优化的,因为在我的模型中我刚刚写过:

Category Model 分类模型

     public function parents()
    {
        return $this->belongsTo('Category', 'cat_father');
    }

     public function children()
    {
        return $this->hasMany('Category', 'cat_father');
    }

}

Post Model: 帖子模型:

<?php
class Post extends BaseModel{
    public $table = "post";
    protected $primaryKey = 'idpost';

     public function Category()
    {
        return $this->belongsTo('Category', 'cat_id');
    }



}

because i didn't know how to join 2 tables in laravel 4, i have a condition is find all post from my categories, which it hadn't belong to category name "Reunion", but i didn't know how to do that, therefore i wrote 2 lines code for that purpose (im not sure wrote code in controller is best way but i didn't know how to call method from Model to controller and get return value) 因为我不知道如何在laravel 4中联接2个表,所以我的条件是从我的类别中查找所有帖子,该帖子不属于类别名称“团圆”,但是我不知道该怎么做,因此我为此编写了两行代码(我不确定在控制器中编写的代码是最好的方法,但我不知道如何从Model调用方法到控制器并获取返回值)

My method from controller for select all post, it hasn't belong to category name "Reunion" 我从控制器选择所有帖子的方法,它不属于类别名称“ Reunion”

public function getAllPostView()
    {       
        $getCat = Category::where('cat_name','=', 'Reunion')->firstOrFail();                            
        $post = Post::where('cat_id', '!=', $getCat->idcategory)->get();
        return View::make('layouts.post')->with('post',$post);
    }   

My question, my code is optimize when i wrote it in controller? 我的问题是,当我在控制器中编写代码时,我的代码是否处于优化状态? and how to wrote it in model and get parameter for passing it to controller and use it to view. 以及如何在模型中编写它并获取将其传递给控制器​​并用于查看的参数。 second question is how to order "POST" because some cases post need to be ordered from new to old 第二个问题是如何订购“ POST”,因为某些情况下需要从新到旧订购

could just use simple joins 可以只使用简单的联接

public function getAllPostView()
{       
    $getCat = Category::where('cat_name','=', 'Reunion')  
            ->join('post','post.cat_id', '!=','Category.idcategory')->get();
    return View::make('layouts.post')->with('post',$post);
}   

Look out for same field names in both the tables if so can use select 查找出相同的字段名称都在表中,如果这样可以使用select

$getCat = Category::select('Category.idcategory as cat_id','Category.cat_id as pos_id','many other fields')
               // 'as cat_id' not required for unique field names
            ->join('post','post.cat_id', '!=','Category.idcategory')
            ->where('cat_name','=', 'Reunion')  
            ->get();

This is how you do it: 这是您的操作方式:

$exclude = 'Reunion';

$posts = Post::select('posts.*')->join('categories', function ($j) use ($exclude) {
  $j->on('posts.cat_id', '=', 'categories.idcategory')
    ->where('categories.name', '<>', $exclude);
})->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM