Lecture 7 ORM and DB Access
Lecture 7 ORM and DB Access
Lecture 7 ORM and DB Access
Course Code: CSC 4182 Course Title: Advanced Programming In Web Technologies
• $users = DB::table('users')->get();
returns a fluent
query builder
instance for the
given table
Configuring models for
Eloquent
• To use eloquent we need to follow some rules while building models.
• Models typically live in the app directory, but you are free to place them
anywhere. (need some change in composer.json)
• All Eloquent models extend Illuminate\Database\Eloquent\Model class.
• Simply the model names should be singular and the table names should be
plural. Model Table
User users
Student students
Course courses
Namespace should be
App\Foldername
Including our
model
allcourses.blade.php is residing in
resources/views/courses folder that’s why
courses.allcourses
In view use as
object.
See the properties
id, name we did
not declared in Output
model but we can
use it because
those are database
columns
CRUD Operations
• Insert Operation
• Already we know how to submit form data. After submission data to controller
function insert_course(Request $request){
$course = new Course();
$course->name= $request->name;
$course->save();
return view('courses.allcourses');
}
• Update Operation
function update_course(Request $request){
$course = Course::where('id' , $request->course_id)->first();
$course->name= $request->name;
$course->save();
return view('courses.allcourses');
}
• Delete operation
function delete_course(Request $request){
$course = Course::where('id' , $request->course_id)->first();
$course->delete();
return view('courses.allcourses');
}
SQL Operation using Query Builder
Retrieving Data