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"> @csrf <input type="file" name="image"> <input type="submit" value="Submit"> </form>
2.2 File Upload Controller Code
public function fileupload(Request $request) { $request->file('image')->store('foldername'); }
3.1 How to Add Validation in Controller Code
public function save(Request $req) { $request->validate( [ 'pname'=>'required' ] ); $pname = $req->pname; $pdesc = $req->pdesc; $p = new product; $p->name = $pname; $p->desc = $pdesc; $p->save(); }
3.2 Add Validation Error Message in View
<form action="/product" method="post"> @csrf <div class="container"> <label for="pname"><b>Name</b></label> <input type="text" placeholder="Enter Product Name" name="pname" required> @error('name') {{$message}} @enderror <br/><br/> <label for="pdesc"><b>Desc</b></label> <input type="text" placeholder="Enter Desc" name="pdesc" required> <br/> <button type="submit">Add</button> </div>