douludi8413 2018-10-25 18:56
浏览 75
已采纳

了解Laravel:请解释“ - > with('i',($ request-> input('page',1) - 1)* 5); ”

Please forgive me for asking a probably beginners-question. It's only that I really searched the net without finding answers! SOF seems like my last option before turning crazy.

My problem is, that I simply can't understand what this line of code does:

->with('i', ($request->input('page', 1) - 1) * 5);

It lays in my Controller index(request $Request) method.

The whole looks like this:

public function index(Request $request)
{
    $books = Book::indexBooks()->paginate(20);
    return view('bookCRUD.index', compact('books'))
        ->with('i', ($request->input('page', 1) - 1) * 5);
}

Another User wrote this, but I cant make sense of it.

That code will get the top 5 of all products, ordered by the id of products in descending order. Then the products data are passed into the view named index.blade.php inside ProductCRUD directory. You could find that directory on yourproject/resources/views. It also flashes a session variable named i (on the view you could access the variable using $i), which have the value of the form input / query string named page, if it exists. Otherwise, the $request->input('page', 1) = 1. From the usage of that variable, the $i will act as starting row number of each page on the grid.

I would be the happiest to receive constructive answers!

  • 写回答

2条回答 默认 最新

  • doubeizhong5178 2018-10-25 19:06
    关注

    The with() method is used to send data to the view.

    The documentation makes it clearer:

    Passing Data To Views

    As you saw in the previous examples, you may pass an array of data to views:

    return view('greetings', ['name' => 'Victoria']);
    

    When passing information in this manner, the data should be an array with key / value pairs. Inside your view, you can then access each value using its corresponding key, such as <?php echo $key; ?>. As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view:

    return view('greeting')->with('name', 'Victoria');
    

    As you can see, the with() accept two parameters:

    • The first one (a string) defines the name of the variable that will be returned to the view
    • The second parameter specifies the actual value that this variable will get.

    So, in the code you provided:

    return view('bookCRUD.index', compact('books'))
             ->with('i', ($request->input('page', 1) - 1) * 5);
    

    This means that in the view bookCRUD.index.blade.php, the $i variable will be available and that its value will be the result of $request->input('page', 1) - 1) * 5.

    So, you could do something with it, like:

    <p> The interesting value is: {{ $i }} </p>
    

    Additional:

    The following statements are equivalent:

    return view('a_nice_view')->with('manager', $user);
    

    with this other one:

    return view('a_nice_view')->withManager($user); // sugared.
    

    This both statements will make available the variable, in this case, $manager to be used in the view.


    Update

    Related to the second part of your question, what he/she is saying is that that line will return 5 products. For the way it looks, he/she is referring to the compact('books') part. This will return to the view a variable (that I can assume is a collection of Book objects).

    The rest of what the use said is just an explanation of what you asked. The only detail is that the user is explaining tha value that the $i variable will get. He/she is using the $request->input('field', 'default_variable') to retrieve an input. Check the docs.

    Retrieving An Input Value

    Using a few simple methods, you may access all of the user input from your Illuminate\Http\Request instance without worrying about which HTTP verb was used for the request. Regardless of the HTTP verb, the input method may be used to retrieve user input:

    $name = $request->input('name');
    

    You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request:

    $name = $request->input('name', 'Sally');
    

    So, as you can see.. doing this $request->input('page', '1') means that if the page field is defined in the request, it will get that value, if not present, the default value will be 1. Just that.

    Look it in this way:

    public function index(Request $request)
    {
        $books = Book::indexBooks()->paginate(20);
        $value = ($request->input('page', 1) - 1) * 5; // this resolves the value to be retuned
        // so, if 'page' is defined in the request it will get the value.
        // if not, it will be '1', so doing the math: $value = 0.
    
        return view('bookCRUD.index', compact('books'))
            ->with('i', $value);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教