To create a crud is an basic thing in programming. Today, I'll talk about creating a CRUD using Laravel framework. First of all, why to use Laravel, instead of pure PHP? Because Laravel is very pratice and useful framework, that allows us to manage our database in a vary simple way. So, let's go step by step:
1 - Configuring .env
the first thing we must do is configure a connection to our database. In our project's root we have an file named .env, and this file contains some important informations about our project and system. If you're forking a project fro git, probably you find the .end.example, you change it's name to .env end set all infomarmations it requires, incluing database:
DB_CONNECTION=your_connection_(mysql_example)
DB_HOST=your_host_(localhost_example)
DB_PORT=your_port(3307_example)
DB_DATABASE=your_databese_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
2 - Creating a Model
Configured .env, we can create a table. To create a table, in Laravel we can create ane migration:
php artisan make:migration create_mytable_table
it will create a migration file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRecordsTable extends Migration
{
public function up()
{
Schema::create('mytable', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('mytable');
}
}
you can see that the columns are defineds in up function, and we define the type of each column:
to create a integer field: $table->integer('age');
to create a text field: $table->text('description');
to create a float field: $table->float('rating');
to create a datetime field: $table->dateTime('birthdate');
after creating your fields, run migration
php artisan migrate
before, to create a model:
php artisan make:model Record
This command will generate a model file:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class MyTable extends Model
{
protected $fillable = ['your', 'atributes'];
}
So, it's everything ok to create our CRUD!
Creating an insert:
To create a insert, you can create one array where the key is our column's name:
$dataAll = [
"Name" => "Jhon",
"Email" => "jhon@emailexample.com"
];
The values can be defined from different sources, as an POST from some form in front end. And remembering: it must be defined in controller, following MVC patterns.
Now, we just must to call our Model and create in the table, using our array:
MyTable::create($dataAll);
This method is native of Eloquent ORM - Laravel Eloquent doc
Creatig a Select From:
1 - Select without where:
to get all registers in our database, just:
$data = MyTable::all();
it put's all infomations in $data, and if we what to send this data to on view, to render this information to an user Laravel will automatically convert it to JSON. The same happens when we want to do a Select with some condition:
1 - Select where:
$data = MyTable::where('age', 23)->get();
it returns all registers where age equals 23 and put it in $data;
Doing one update:
To do an update in Laravel, is very simple:
$data = MyTable::where('age', 23)->update(['email', 'new@email.mail']);
we just must say where we want to update, and past in an array the key - column name and value - new value.
Deleting data:
to delete data in Laravel is very simple too:
MyTable::where('age', 23)->delete();
this code will delete from database all registes where age equals 23.
Top comments (0)