Laravel previous and next records(Laravel 上一个和下一个记录)
问题描述
我正在尝试创建一个页面,我可以在其中查看数据库中的所有人并对其进行编辑.我制作了一个表格,在其中填写某些字段的数据库中的数据.
我想通过下一个和上一个按钮浏览它们.
为了生成下一步,我必须使用比当前更大的 ID 来加载下一个配置文件.
为了生成上一步,我必须使用小于当前 ID 的 ID 来加载先前的配置文件.
我的路线:
Route::get('users/{id}','UserController@show');控制器:
public function show($id){$input = User::find($id);//如果用户单击下一步,则应执行此操作.$input = User::where('id', '>', $id)->firstOrFail();echo '';dd($输入);echo '</pre>';return View::make('hello')->with('input', $input);}查看:按钮:
<a href="{{ URL::to( 'users/' . $input->id ) }}">Next</a>获取当前 ID 并增加它的最佳方法是什么?
以下是从@ridecar2 链接派生的更新的控制器和视图文件,
控制器:
public function show($id){//获取当前用户$user = User::find($id);//获取之前的用户ID$previous = User::where('id', '<', $user->id)->max('id');//获取下一个用户ID$next = User::where('id', '>', $user->id)->min('id');return View::make('users.show')->with('previous', $previous)->with('next', $next);}查看:
<a href="{{ URL::to( 'users/' . $previous ) }}">Previous</a><a href="{{ URL::to( 'users/' . $next ) }}">Next</a>I am trying to create a page where I can see all the people in my database and create edits on them. I made a form where I fill in the data from the database of certain fields.
I would like to navigate trough them by a Next and Previous button.
For generating the next step I have to take the ID larger than the current one to load the next profile.
For generating the previous step I have to take the ID smaller than the current one to load the previous profile.
My route:
Route::get('users/{id}','UserController@show');
Controller:
public function show($id)
    {
        $input = User::find($id);
        // If a user clicks next this one should be executed.
        $input = User::where('id', '>', $id)->firstOrFail();
        echo '<pre>';
        dd($input);
        echo '</pre>';
        return View::make('hello')->with('input', $input);
    }
View: The buttons:
<a href="{{ URL::to( 'users/' . $input->id ) }}">Next</a>
What is the best approach to get the current ID and increment it?
Below are your updated controller and view files derived from @ridecar2 link,
Controller:
public function show($id)
{
    // get the current user
    $user = User::find($id);
    // get previous user id
    $previous = User::where('id', '<', $user->id)->max('id');
    // get next user id
    $next = User::where('id', '>', $user->id)->min('id');
    return View::make('users.show')->with('previous', $previous)->with('next', $next);
}
View:
<a href="{{ URL::to( 'users/' . $previous ) }}">Previous</a>
<a href="{{ URL::to( 'users/' . $next ) }}">Next</a>
这篇关于Laravel 上一个和下一个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 上一个和下一个记录
				
        
 
            
        - 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
 - 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
 - 覆盖 Magento 社区模块控制器的问题 2022-01-01
 - 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
 - Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
 - PHP foreach() 与数组中的数组? 2022-01-01
 - 如何在 Symfony2 中正确使用 webSockets 2021-01-01
 - openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
 - PHP - if 语句中的倒序 2021-01-01
 - Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
 
