Getting Error Of Undefined Variable "data" php Codeigniter - php

I am updating table taking id in url which is auto increment.. i am also defiend a variable data in controller that is $data['data']=$this->articlesmodel->find_data($art_id); and this varible passing to view that is $this->load->view('admin/edit',$data);
and in view at form open i called the variable id**}",['class'=>'form-horizontal']);?>
but the error occure that the $data is undefiend.
this is view code
<?php echo form_open("admin/update_data/{$data->id}",['class'=>'form-horizontal']);?>
This is controller code
public function edit_data($art_id)
{
$this->load->helper('form');
$this->load->model('articlesmodel');
$data['data']=$this->articlesmodel->find_data($art_id);
$this->load->view('admin/edit',$data);
}
This is Model Code
public function update_data($art_id, Array $data )
{
return $this->db
->where('id',$art_id)
->update('data',$data);
}

Related

Shows only 3 most recent data

So I want to display the three most recent data from the 'berita' table.
i have created a function in controller, and an error appears Call to undefined method stdClass::take()
public function index()
{
$data = [
'berita' => $this->BeritaModel->allData()->last()->take(3)->get(),
];
return view('user.v_home_smk', $data);
}

Retrieve data from database in codeigniter

I am new to Codeigniter so I'm having some difficulties I want to retrieve data from the database and load it in the view but I couldn't figure out how.
Here is my model:
public function viewClientWaterwell(){
$userid = get_client()->userid;
$waterwells = $this->db->select('*')
->from($this->table)
->where('ww_client_id', $userid)
->get()->result_array();
return $waterwells;
}
Here is my clientController:
public function viewClient()
{
$data['waterwells'] = $this->waterwell_model->viewClientWaterwell();
$this->data($data);
$this->view('waterwells/clients/viewClient');
$this->layout();
}
I want to pass some data in a view to the client side but I can't figure out how. I think there is some problem with my model but I cannot figure out what. I'm new to this and will appreciate your feedback. Thanks!
You just need to use second argument to pass data to your template or view file
Here is example to start
Inside your controller function
public function viewClient()
{
$data = array( 'myvar' => 'I love stackoverflow' );
$this->load->view('waterwells/clients/viewClient', $data);
}
And inside your template or view file you can access them by their name.
<?php echo $myvar; ?>

Undefined variable: alert Laravel 5.6

Hey guys I am new to laravel and I have been trying to store all records of table 'alert' to a variable and then pass that variable to a view so that I can display them.
I have a controller - AlertController and inside that a function:
public function showalert(){
$alert = Alert::all()->toArray();
return view('content')->with(compact('alert'));
}
In my view I have this code
#foreach($alert as $title)
{{$title->text}}
#endforeach
I am receiving this error :Undefined variable: alert (View: \views\content.blade.php)
You can try below
// controller file
public function showalert(){
$alert = Alert::all();
return view('content', [ 'alert' => $alert ]);
}
// view file
#foreach($alert as $title)
{{$title->text}}
#endforeach
Use this approach:
public function showalert(){
$alert = Alert::all();
return view('content', compact('alert'));
}

How to update a record in lumen

I am simply trying to update a record but it says undefined variable in my view.
My html form view is like:
<form name="add_page" data-toggle="validator" role="form" id="add-pageform" action='{{url("update/page/$page->page_id")}}'>
My Controller:
public function updatePage(Request $request,$id)
{
$page = $request->all();
$plan = PageList::find($id);
$plan->update($page);
return view('edit-list');
}
and my route.php
$router->post('update/page/{id}','AjaxController#updatePage');
when i trying to access my view it says
Undefined variable: page (View: D:\xampp\htdocs\dev\app\resources\views\edit-list.blade.php)
its displaying error from in my form tag
any help would be appreciated:
You have not passed the $page variable to the view. Update your controller action like such:
public function updatePage(Request $request,$id)
{
$page = $request->all();
$plan = PageList::find($id);
$plan->update($page);
return view('edit-list')->with('page', $page);
}
For more information on passing data to views check this link
You should change your routes method to a patch if you want to update and then insert this into your form #method{'PATCH'}

Laravel 5 Passing data from Controller to View with AJAX

I'm having an issue passing data from controller to a view loaded via ajax. Below is my code:
JS:
function editass(id, name, title) {
var modal_title = document.getElementById("modal-title");
var modal_name = document.getElementById("modal-name");
var modal_body = document.getElementById("modal-body");
modal_title.innerHTML=title;
modal_name.innerHTML=name;
modal_body.innerHTML = '<div class = "text-info" align="center">Just a moment...</div>';
$("#modal-body").load('/SchoolSmart/public/assessments/edit/'+id);
//alert("modal");
}
Routes:
Route::get('/assessments/edit/{id}', 'AssessmentsController#edit');
Controller function
public function edit($id)
{
$editassessments = Assessment::findOrFail($id);
return view('assessments.edit')->with('$editassessments', $editassessments);
}
View
Assessment name: {!! $editassessments->name !!}
The error returned from the header is:
Undefined variable: editassessments
The view loads fine if I remove the call to variable, which is the whole essence of the page. Also, this function works fine if I the view was not loaded via AJAX.
Please, help on how I pass the variable from controller to view loaded via AJAX.
Thank you
Remove the '$':
return view('assessments.edit')->with('editassessments', $editassessments);

Categories