Outsourcing SEO Services / Company
Why Choose Blazingcoders for SEO Outsourcing? Your go-to source for outsourcing software and digital marketing services is Blazingcoders. Our products serve IT firms looking to grow their offerings
Read More
In this Post PHP CodeIgniter 4 instructional exercise, I will tell you the fundamental CRUD usefulness with MySQL Database.
Muck is an abbreviation for Create, Read, Update, and Delete information base records.
This is quite possibly the most well-known errand in the improvement to make something in the table, update some data, read information from a table, and erase data.
In this model, I will utilize the bootstrap library to list posts and render the structures to make and alter posts.
Download and Configure Codeigniter 4 Application
In the initial step, I will download the new form of Codeigniter 4, Go to this connection constantly Codeigniter 4, and unfasten this application in your working registry and change the application name to muck.
Run the writer order to download the conditions.
Now I will configure the base URL in app/Config/App.php.
public $baseURL = 'http://localhost:8080';
To
public $baseURL = 'http://localhost/crud/public';
Configure Database and Create Table
In this progression, I will make an information base originally called muck and make another table post in this muck data set.
You can run the underneath SQL inquiry to make posts table in your information base.
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`description` text NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Presently I need to refresh the information base qualification in the data set config document.
I need to go application/Config/Database.php and open the Database.php document in a proofreader.
public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'crud',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'cacheOn' => false,
'cacheDir' => '',
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
In this step, I will create a model file PostModel.php to communicate with the posts table in the app/Models/ directory.
namespace App\Models;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
class PostModel extends Model
{
protected $table = 'posts';
protected $allowedFields = ['title', 'description'];
}
?>
In this step, I will create a controller record Posts. Hypertext Preprocessor in the app/controllers directory. In this controller record, I will create a few approaches to carry out crud operation.
index() - this method is used to list all the posts.
create() - this method is used to render the create form.
store() - this method is used to insert the new post into the table.
edit() - this method is used to render the edit form.
update() - this method is used to update the post information.
delete() - this method is used to delete the post information from a database table.
use CodeIgniter\Controller;
use App\Models\PostModel;
class Posts extends Controller
{
public function index()
{
$model = new PostModel();
$data['posts'] = $model->orderBy('id', 'DESC')->findAll();
return view('posts', $data);
}
public function create()
{
return view('create-post');
}
public function store()
{
helper(['form', 'url']);
$model = new PostModel();
$data = [
'title' => $this->request->getVar('title'),
'description' => $this->request->getVar('description'),
];
$save = $model->insert($data);
return redirect()->to( base_url('posts') );
}
public function edit($id = null)
{
$model = new PostModel();
$data['post'] = $model->find($id);
return view('edit-post', $data);
}
public function update()
{
helper(['form', 'url']);
$model = new PostModel();
$id = $this->request->getVar('id');
$data = [
'title' => $this->request->getVar('title'),
'description' => $this->request->getVar('description'),
];
$save = $model->update($id,$data);
return redirect()->to( base_url('posts') );
}
public function delete($id = null)
{
$model = new PostModel();
$data['post'] = $model->where('id', $id)->delete();
return redirect()->to( base_url('posts') );
}
}
?>
In this step, I will create the respective HTML files.
posts.php
create-post.php
edit-post.php
posts.php
Codeigniter 4 CRUD Tutorial - posts List Example - Blazingcoders
Id | Title | Description | Action |
---|---|---|---|
|
|
| Edit |
Why Choose Blazingcoders for SEO Outsourcing? Your go-to source for outsourcing software and digital marketing services is Blazingcoders. Our products serve IT firms looking to grow their offerings
Read MoreA heartfelt approach to honoring and remembering a loved one is to create a memorial website, which gives family and friends a digital platform to do the same. This comprehensive tutorial will show yo
Read MoreDesign Services Blazingcoders Offer:- At Blazingcoders, we add creativity and innovation to your dream. Our designing services are logo design, website design, and newsletter designed for aggressiv
Read More