我如何在Laravel Model类中编写这样的语句
SELECT * from video where view=(select max(view) from video)
$query = DB::table('video')->select('*');
$alldetails = $query->addSelect('max(view)')->get();
我打算选择具有最大观看次数的视频的所有属性
我的模特
class Video extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'video';
public static function feature(){
$query = DB::table('video')->select('*');
$alldetails = $query->addSelect('max(view)')->get();
return $alldetails;
}
}
我的控制器
class HomeController extends BaseController {
public function home(){
$feature=View::feature();
return View::make('frontend/index')
->with("title","Kromatik Multimedia")
->with('feature'.$feature);
}
}
我的观点
@foreach($feature as $fet)
$fet->title;
@endforeach
答案 0 :(得分:2)
创建一个这样的模型:
class Video extends Eloquent {
// Laravel expects the table name "videos"
// for Video model so need to specify here
// because you didn't follow that convention
protected $table = 'video';
}
从你的控制器调用这样的东西:
class HomeController extends BaseController {
public function home()
{
// Get the Video model with maximum views; assumed that,
// view field contains numeric number, i.e. 2 (two views)
$featured = Video::orderBy('view', 'desc')->first();
// Load the "index.blade.php" view from "views/frontend" folder
return View::make('frontend.index')->with('video', $featured);
}
}
在view
中(不需要循环,因为只传递了一个模型):
{{ $video->title }}
{{ $video->genre }}