app/Http/Controllers/Home/HomeController.php
/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
Display a listing of the resource.
@return Response
Display a listing of the resource . @return Response
public function index($id='')
	{
		$Category = Category::all();
		$c = DB::table('categories')->select('id')->get();
		$arr = array();
		foreach ($c as $key => $value) {
			$arr[] = $value->id;
		}
		if(!in_array($id,$arr)){
			$index = DB::table('messages as m')
			->leftJoin('users as u','m.user_id','=','u.id')
			->leftJoin('categories as c','m.category','=','c.id')
			->select('m.*','u.face','c.name','c.color','c.id as cid')
			->orderBy('m.created_at','desc')
			->paginate(30);
		}else{
			$index = DB::table('messages as m')
			->leftJoin('users as u','m.user_id','=','u.id')
			->leftJoin('categories as c','m.category','=','c.id')
			->select('m.*','u.face','c.name','c.color','c.id as cid')
			->where('m.category','=',$id)
			->orderBy('m.created_at','desc')
			->paginate(30);
		}
		$data = [
			'index' => $index,
			'category' => $Category
		];
		return view('home.index',$data);
	}
public function index ( $ id = '' ) { $ Category = Category :: all ( ) ; $ c = DB :: table ( 'categories' ) -> select ( 'id' ) -> get ( ) ; $ arr = array ( ) ; foreach ( $ c as $ key => $ value ) { $ arr [ ] = $ value -> id ; } if ( ! in_array ( $ id , $ arr ) ) { $ index = DB :: table ( 'messages as m' ) -> leftJoin ( 'users as u' , 'm.user_id' , '=' , 'u.id' ) -> leftJoin ( 'categories as c' , 'm.category' , '=' , 'c.id' ) -> select ( 'm.*' , 'u.face' , 'c.name' , 'c.color' , 'c.id as cid' ) -> orderBy ( 'm.created_at' , 'desc' ) -> paginate ( 30 ) ; } else { $ index = DB :: table ( 'messages as m' ) -> leftJoin ( 'users as u' , 'm.user_id' , '=' , 'u.id' ) -> leftJoin ( 'categories as c' , 'm.category' , '=' , 'c.id' ) -> select ( 'm.*' , 'u.face' , 'c.name' , 'c.color' , 'c.id as cid' ) -> where ( 'm.category' , '=' , $ id ) -> orderBy ( 'm.created_at' , 'desc' ) -> paginate ( 30 ) ; } $ data = [ 'index' => $ index , 'category' => $ Category ] ; return view ( 'home.index' , $ data ) ; }
Display a listing of the resource.
Display a listing of the resource .
// 取出分类 //指定了分类 // return view('home.index',['index' => $index,'category' => $Category]);