PHP Laravel Course: 23 Laravel Model (Working With Database)
PHP Laravel Course: 23 Laravel Model (Working With Database)
Connecting to Database
Laravel has made processing with database very easy. Laravel currently supports
following 4 databases:
- MySQL
- Postgress
- SQLite
- SQL Server
The query to the database can be fired using raw SQL, the fluent query builder, and
the Eloquent ORM. To understand the all CRUD (Create, Read, Update, Delete)
operations with Laravel, we will use simple student management system. Configure
the database in config/database.php file and create the college database with
structure in MySQL as shown in the following table.
Database: College
Table: student
name varchar(25)
We will see how to add, delete, update and retrieve records from database using
Laravel in student table.
Insert Records
We can insert the record using the DB facade with insert method. The syntax of
insert method is as shown in the following table.
Returns bool
Example
use DB;
<html>
<head><title>Student Management | Add</title></head>
<body>
<form action="/student/store" method="post">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<table>
<tr>
<td>Name</td>
<td><input type='text' name='name' /></td>
</tr>
<tr>
<td colspan='2'><input type='submit' value="Add student" /></td>
</tr>
</table>
</form>
</body>
</html>
Route::get('student/create',
array('as'=>'student/create','uses'=>'StudentController@create'));
Route::post('student/store',
array('as'=>'student/store','uses'=>'StudentController@store'));
Retrieve Records
We can insert the record using the DB facade with insert method. The syntax of
insert method is as shown in the following table.
Returns array
<html>
<head><title>View Student Records</title></head>
<body>
<a href='/student/create'>New Student</a><br/>
<table border=1>
<tr>
<td>ID</td>
<td>Name</td>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
</tr>
@endforeach
</table>
</body>
</html>
Step 3: Edit the “Store” method/action at StudentController
Route::get('student',
array('as'=>'student','uses'=>'StudentController@index'));
Update Records
We can update the records using the DB facade with update method. The syntax of
update method is as shown in the following table.
Returns int
Example
<html>
<head><title>View Student Records</title></head>
<body>
<a href='student/create'>New Student</a><br/>
<table border=1>
<tr>
<td>ID</td>
<td>Name</td>
<td>Action</td>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td><a href='student/edit/{{ $user->id }}'>Edit</a></td>
</tr>
@endforeach
</table>
</body>
</html>
<html>
<head><title>Student Management | Edit</title></head>
<body>
<form action="/student/update/<?php echo $users[0]->id; ?>" method="post">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<table>
<tr>
<td>Name</td>
<td><input type='text' name='stud_name' value='<?php echo
$users[0]->name; ?>' /></td>
</tr>
<tr>
<td colspan='2'><input type='submit' value="Update student" /></td>
</tr>
</table>
</form>
</body>
</html>
Route::get('student/edit/{id}',
array('as'=>'student/edit','uses'=>'StudentController@edit'));
Route::post('student/update/{id}',
array('as'=>'student/update','uses'=>'StudentController@update'));
Delete Records
We can delete the record using the DB facade with the delete method. The syntax of
delete method is shown in the following table.
Returns int
<html>
<head><title>View Student Records</title></head>
<body>
<a href='student/create'>New Student</a><br/>
<table border=1>
<tr>
<td>ID</td>
<td>Name</td>
<td>Action</td>
<td>Action</td>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td><a href='student/edit/{{ $user->id }}'>Edit</a></td>
<td><a href='student/delete/{{ $user->id }}'>Delete</a></td>
</tr>
@endforeach
</table>
</body>
</html>
Step 3: Add the following lines in routes/web.php.
Route::get('student/delete/{id}',
array('as'=>'student/delete','uses'=>'StudentController@destroy'));
Models
To create a new model in laravel from artisan is and framework will create a new
model under app/ .
After run the creating model command, framework will create one model class file
under app/student.php folder by the following
<?php
namespace laravel_5_3;
use Illuminate\Database\Eloquent\Model;
protected $fillable = [
'id',
'name'
];
}
return view('student.edit',['users'=>$users]);
}
public function update(Request $request, $id)
{
$name = $request->input('stud_name');
//DB::update('update student set name = ? where id = ?',[$name,$id]);
// Delete Student
$student = Student::find(1);
$student->delete();
echo "Record deleted successfully.<br/>";
echo '<a href="/student">Click Here</a> to go back.';
}