Laravel Tutorial Day 8

Learning Objectives: 1) Laravel Session Management. Store value to Session, Fetch value from the session. 2) Another Important Point we will discuss is File Upload. 3) Validation Controller

1.1 Store Value to Session

Route::get('/setsession', function () {

        session(['loginusername'=>'Adarsh Patel']);
        echo "done";

1.2 Fetch Value from Session

  Route::get('/getsession', function () {

        echo session('loginusername');

2.1 File Upload View Code

<form action="/fileupload" method="post" enctype="multipart/form-data">
    <input type="file" name="image"> 

    <input type="submit" value="Submit">

2.2 File Upload Controller Code

public function fileupload(Request $request)

3.1 How to Add Validation in Controller Code

 public function save(Request $req)


        $pname = $req->pname;
        $pdesc = $req->pdesc;

        $p = new product;
        $p->name = $pname;
        $p->desc = $pdesc;

3.2 Add Validation Error Message in View

<form action="/product" method="post">
  <div class="container">
    <label for="pname"><b>Name</b></label>
    <input type="text" placeholder="Enter Product Name" name="pname" required>
    <label for="pdesc"><b>Desc</b></label>
    <input type="text" placeholder="Enter Desc" name="pdesc" required>
    <button type="submit">Add</button>