Laravel Model Query Introduction and Usage
Model Laravel is an Eloquent ORM (Object-Relational Mapping) representation of a database table. It provides a simple and comprihensive way to interact with database by allowing user to work with database records as objects. Each model related to a table in database, and each instance of a model corresponds to a row in that table.
Laravel model works between php and database as a wrapper. He below is a laravel model
// app/Models/User.php namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $fillable = ['name', 'email', 'password','contact_no']; }
To get data we can call User model like below
$users = User::all(); //this will fetch all users from users table
Get data from users table for id : 5
$users = User::where('id',5)->first(); //this will fetch user from users table where id is 5
Get data from users table for id: 1, 2, 10
$users = User::whereIn('id',[1,2,10])->get(); //this will fetch users from users table where id is 1, 2, 10
Get all users from users table where id not 20
$users = User::whereNoIn('id',[20])->get(); //this will fetch all users except id 20