error for inserting data in database in view in joomla - joomla

I want to insert a data in database but it show error
Fatal error: Call to a member function getform() on a non-object in
\components\com_enquiry\views\form\view.html.php
on line 9
my code is:
enqiry.php:
public function getform($data)
{
$db=JFactory::getDbo();
$db=$this->getDbo();
$query=$db->getQuery(true);
$reg=new stdClass();
$reg->name=$data['name'];
$reg->email=$data['email'];
$reg->phone=$data['phone'];
$reg->comments=$data['comments'];
$reg=$db->insertObject('#__enquiry',$reg);
}
and view.html.php:
public function display()
{
$this->msg = 'enquiry form';
$model=$this->getModel();
$data =$model->getform();
$this->assignRef('data', $data );
parent::display();
parent::display();
}
controller:
class enquiryController extends JControllerLegacy
{
public function display()
{
$vname=JRequest::getCmd('view','form');
JRequest::setVar('view',$vname);
JRequest::setVar('layout','default');
parent::display();
}
public function show()
{
$data['name']=$_POST['name'];
$data['email']=$_POST['email'];
$data['phone']=$_POST['phone'];
$data['comments']=$_POST['comments'];
$sname=JRequest::getCmd('view','thanx');
JRequest::setVar('view',$sname);
print_r($data);
}
}
could someone help me solve this error?

The problem is that your model is not loaded by the line:
$model=$this->getModel();
This either means that your model in enquiry.php is not named correctly
ComponentNameModelEnquiry
or that your view is not named enquiry.
You can try to specify which model you want, like
$model = $this->getModel('enquiry');

Related

call another controller from dashboard using template pages

I working on Codeigniter project, I create page template to load header left menu and footer, everything working good, when I try the open link in the menu I want to open another controller. I do it but when the controller view open the variable inside to load database table for each row not working.. but the controller who load the database when I open it without my template its working fine
The Dashboard controller
class Dashboard extends CI_Controller{
protected $data = array();
function __construct()
{
parent::__construct();
$this->data['pagetitle'] = 'Invoices Manager';
}
protected function render($the_view)
{
$this->data['the_view'] = (is_null($the_view)) ? '' : $this->load->view($the_view,$this->data, TRUE);
$this->load->view('templates/master_page', $this->data);
}
public function home() {
// $this->load->view('templates/master_page', $this->data);
$this->render( 'templates/homepage_view');
}
public function dashboard() {
// $this->load->view('templates/master_page', $this->data);
$this->render( 'dashboard/home');
}
public function purchaselist(){
$this->render('purchase/index');
}
}
The purchase controller that working good alone
class Purchase extends CI_Controller{
protected $data = array();
protected $mydata = array();
function __Construct()
{
parent::__Construct ();
$this->load->database(); // load database
$this->load->model('Purchase_model'); // load model
$this->mydata['purchase']=null;
}
public function index()
{
$query = $this->Purchase_model->getPurchaselist();
if($query)
{
$mydata['purchase'] = $query;
}
$this->load->view('purchase/index', $mydata);
// $this->render( 'purchase/index');
}
}
when I call dashboard/purchaselist they say the
Message: Undefined variable: purchase
Filename: purchase/index.php
Line Number: 17
its should load database table inside the template
try using dashboard/index.php/purchaselist or load your modal $this->load->model('Purchase_model'); as global
Please pass null or empty array in purchase if $query is empty
public function index()
{
$query = $this->Purchase_model->getPurchaselist();
if(!empty($query)){
$mydata['purchase'] = $query;
}else{
$mydata['purchase'] = array();
}
$this->load->view('purchase/index', $mydata);
// $this->render( 'purchase/index');
}

accessing custom method in model

Im practicing laravel and im making a custom method for my user
In my user model i have build a function like this
public function employee(){
return $this->where('user_type','employee');
}
and then in my controller I'm accessing the function like this
public function index(){
$users = User::latest()->employee();
return UserResource::collection($users);
}
but it return an error Method Illuminate\Database\Query\Builder::employee does not exist.how to fix this?
Use local scope instand
public function scopeEmployee($query)
{
return $query->where('user_type', 'employee');
}
Your controller can be as it was !
public function index(){
$users = User::latest()->employee()->get();
return ProductsResource::collection($users);
}

Call to a member function diffForHumans() on null

i am getting outputs of my posts in the system but i am getting this errror Call to a member function diffForHumans() on null... Can u please help me? Thanks...
Article model is that:
protected $fillable=[
'user_id','content','live','post_on'
];
public function setLiveAttribute($value)
{
$this->attributes['live']=(boolean)($value);
}
And the articlescontroller is that
public function index()
{
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function create()
{
return view('articles.create');
}
public function store(Request $request)
{
Article::create($request->all());
}
public function show()
{
}
public function update()
{
}
public function destroy()
{
}
The problem is for sure not here. You probably try to run diffForHumans() in your view for example like this:
$user->post_on->diffForHumans()
but post_on is set to null instead of date.
So you should verify where exactly you run this method, set this column as date and in your view add something like this:
Posted: {{ $date ? $date->diffForHumans: '-' }}

Codeigniter model can not connect to db

My controller can connect to the DB but my model can not. I have autoloaded the DB in the autoload.php file, but no luck in the model.
so if I do something like
$this->db->insert('table', $data);
I receive this Call to a member function insert() on a non-object I have used Codeigniter before but have never had this issue, on my other project I did not even use parent::__construct()
class Bucketlist extends CI_Model {
private $data = array();
public function __construct(){
parent::__construct();
}
// Setter Function
public function __set ($var, $val) {
$this->data[$var] = $val;
}
// Getter Function
public function __get($var) {
return (isset($this->data[$var])) ? $this->data[$var] : null;
}
// Create WishList
function createBucketList($bucketlist) {
$this->db->insert('_bucketlist', $bucketlist->data);
}
}
thanks.
You may need to Autoload the Database connection (http://ellislab.com/codeigniter/user-guide/database/connecting.html), as it appears that the db variable is not instantiated before you are trying to use it.

Joomla Component data from model to view

I am creating a component in Joomla for the first time, now i am having a hard time calling data from my model in to my view. I did this as follows:
//In my model
class InternetModelDefault extends JModel {
public function test(){
$this->test='test';
}
}
//In my view
class InternetViewInternet extends JView {
$model = $this->getModel();
$test = $model->test();
var_dump($test);
// Display the view
parent::display($tpl);
}
}
The output gives the following error:
Fatal error: Call to a member function test() on a non-object in
/var/websites/www.infrait.nl/content/components/com_internet/views/internet/view.html.php
on line 66
Where does it went wrong? Please some help..
Current mapstructure:
http://imgdump.nl/hosted/ad9e57de83060b3240f8fc6bba99237b.png
As i am new i can only share you this direct link.
your model.php like this
jimport ('joomla.application.component.modellist');
class InternetModelInternet extends JModelList
{
public function test(){
$this->test='test';
}
}
and in view.html.php get model
jimport ('joomla.application.component.view');
class InternetViewInternet extends JView
{
public function display ($tpl = null)
{
$model = &$this->getModel ();
$test = $model->test();
var_dump($test);
// Display the view
parent::display($tpl);
}
}
You are getting the error because object is not initiated properly.Change class name to InternetModelInternet. And model file name will be internet.php.
Let me know if it does not work.

Resources