// In a blog application, you might have a "posts" table in your database // that contains columns for the post ID, title, content, author, etc. // To model this table using Eloquent, you would create a Post class: class Post extends \Illuminate\Database\Eloquent\Model { protected $table = 'posts'; // maps the class to the "posts" table in the database } // Then you could use methods on the Post class to interact with the database: $posts = Post::all(); // retrieves all posts from the database foreach ($posts as $post) { echo $post->title; }
// In an e-commerce application, you might have a "users" table and an "orders" // table in your database, with a one-to-many relationship between them. // To model these tables using Doctrine, you would create User and Order classes: /** @Entity **/ class User { /** @Id @GeneratedValue @Column(type="integer") **/ protected $id; /** @Column(type="string") **/ protected $name; /** @OneToMany(targetEntity="Order", mappedBy="user") **/ protected $orders; // ... } /** @Entity **/ class Order { /** @Id @GeneratedValue @Column(type="integer") **/ protected $id; /** @ManyToOne(targetEntity="User", inversedBy="orders") **/ protected $user; /** @Column(type="string") **/ protected $status; // ... } // Then you could use the EntityManager object to interact with the database: $em = EntityManager::create($conn, $config); $users = $em->getRepository(User::class)->findAll(); // retrieves all users from the database foreach ($users as $user) { echo $user->getName() . " has " . count($user->getOrders()) . " orders."; }