Pages

Saturday, 2 July 2022

Get Data through Model in Codeigniter 4

Table Structure



 Model

<?php

namespace App\Models;
use CodeIgniter\Model;
class ContactModel extends Model
{
      protected $table='holywebcontact';
       protected $primaryKey = 'ID';
       protected $allowedFields = ['ID', 'Name', 'Phone', 'Message', 'CreatedDate'];     

}

Controller

<?php 

namespace App\Controllers;
use App\Models\ContactModel;
class Home extends BaseController
{

     public function dataread()
    {

        $data['title'] = 'DB Get Demo';
       $crudModel = new ContactModel();
        $data['user_data'] = $crudModel->findAll(); 
        return view('tmp2', $data);

    }

}

Note: Below is a list of some different types of conditions/filters for data access.
 
$data['user_data'] = $crudModel->where('id>=','5')->orderBy('id', 'DESC')->paginate(5);

 $data['user_data'] = $crudModel->where('id>=','5','phone','22')->orderBy('id', 'DESC')->paginate(5);

$data["model"]=$album->where('AlbumID', $aid)->findAll();


Get All record with ouder by clause

$data=$GalleryModel->orderBy('ID', 'DESC')->findAll();

View

<?php

foreach ($model as $item)
{
       echo $item["ID"] . "," . $item['Name'] . "<br>";
}

?>

Next Topic

No comments:

Post a Comment