IN自定义php代码
$select_query = "select * from admin where Stutes = '1' order by ID asc";
我们如何将此代码嵌入我的这个laravel 5代码
public function index()
{
$books=Book::all();
return view('books.index', compact('books'));
}
您可以使用查询生成器
$admin= DB::tabe('admin')
->select("*")
->where("Stautes",1)
->orderBy("ID", "asc")
->get();
优化查询:
由于您正在尝试检索所有行,因此您的查询可以是类似的:
$admin= DB::table('admin')->where("Status",1) ->orderBy("ID", "asc") ->get();
如果要检索特定列:
$admin_ids = DB::table('admin')->select('id')->where("Status",1) ->orderBy("ID", "asc") ->get();
Laravel Select docs
希望这能有所帮助。
在控制器中
public function index()
{
//$books=Book::all();
$books= DB::table('books')
->select('*')
->where('status', '1')
->orderBy('id', 'asc')
->get();
return view('books.index', compact('books'));
}