Something you need to know in Laravel

Laravel_learn
2 min readOct 3, 2022

--

Let’s start with validation.
Validation is necessary when you want some specific information from the user as per your requirement. Validating forms is an important part of any web application, as it ensures that the user input is clean and complete before performing any actions on it.

Here’s how you can add validation to your user data.

$validatedData = $request->validate([
‘name’ => ‘required|max:255’,
‘price’ => ‘required’,
]);

Find data

To search data from the database in laravel application

$example = Exapmle::findOrFail($id);

$example = database::find($id);

Save data into the database

$example->email = $data['email'];
$example->save();

How to delete Two different ways

example::where(‘id’,$id)->delete();

use DB;

DB::delete(‘delete from example where id = ?’,[$id]);

Two different ways to select all data from a database

use DB;

$users = DB::select(‘select * from example’);

$user = user::all();

Putting data into a session in a Laravel application

There are many ways to put data into a session in a Laravel application.

But here we will use simple.

$req->session()->put(‘user’,$user);

Here we are calling the function that after login the username will enter the session.

Destroying Sessions

If the user doesn’t log out, then the Session object remains active. To destroy the existing Session object, use the following code:

\Session::flush(); // Empty the Session Cache

// \Session::forget(‘user_id’); // Clear just one specific Session variable

Result

Hope you learned something if you got something new keep it in mind and always learn something new in Laravel.

--

--

No responses yet