Search code examples
savelaravel-5.1limit

Save data with limit to 3 only?


Accessing database data and limit to 3 rows only.

I am new to laravel and i dont know how to limit access the rows.Anyone has idea how to get the result the ids 1-3 so that i will save it to my other table?

Sample range of grade to save. Only grades within this range should be save but limiting to 3 data only.

84 to 90

Scholar
id          grade
1            90
2            85
3            85
4            82
5            86
6            84
7            83
8            87
9            88
10           89


public function AddOrgaRequest(Request $request){

$test = new List;
some code   
$test->save();

return redirect()->back();
}

Solution

  • You can use whereBetween from Laravel's querybuilder to get the data of a column that is between 2 values(eaxmple taken from Laravel docs):

    $users = DB::table('users')->whereBetween('votes', [1, 100])->get();
    

    https://laravel.com/docs/5.4/queries#where-clauses

    To limit your query results you can use(example taken from Laravel docs):

    $users = DB::table('users')
                    ->offset(10)
                    ->limit(5)
                    ->get();
    

    https://laravel.com/docs/5.4/queries#ordering-grouping-limit-and-offset

    A combination of these methods should get your what you want. After retrieving your results you can save them to another table.