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
答案 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();