laravel 5雄辩的关系让所有记录都无效

时间:2016-05-05 19:11:41

标签: laravel eloquent laravel-5.2

Tables reletions

Category.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{

  public function getVideo(){

        return $this->hasMany('App\Video','category_id','id')->where('categories.kat_name', '=', 'comedy');


    }

}

PagesController.php(控制器)

namespace App\Http\Controllers;

use App\User;

use App\Category;

use App\Video;

use App\Http\Controllers\Controller;

class PagesController extends Controller
{

function getCategory($kategori_ad){

    $myvideo = Category::all()->getVideo();

    }


}

我想要这样做:SELECT * FROM categories加入视频ON categories.id = videos.category_id AND categories.kat_name =' animals'与orm

2 个答案:

答案 0 :(得分:2)

class Category extends Model {

  public function getVideo(){

    return $this->hasMany('App\Video','category_id','id');

  }
}
class PagesController extends Controller {

   function getCategory($kategori_ad){
       $myvideo = Category::where('kat_name', '=', 'comedy')->with('getVideo')->get();
   }
}

答案 1 :(得分:0)

$myvideo = Category::with('getVideo')->get();