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);
}
Related
This is used to export excel
My controllers:
public function reportExport(Request $request)
{
return Excel::download(new ReportExport($request), 'LAPORAN ABSENSI.xlsx');
}
Export folder : ReportExport
return [
"att_cross" => $att_cross,
"belum_absen" => $belum_absen,
"absen" => $absen,
];
but, when I return one of the data is successful, for example :
return $absen;
I want to display the contents of the variable $absent, $belum_absen, $att_cross but I get an error message "Call to a member function all() on array "
how can I display the data for these three parameters?
Read the docs for Excel package, you need to map() the output.
For example, I have two tables. One called "PedidoReservaQuarto" and another "PedidoReserva". What I want to do is this, I want to save the data in both tables when I click the button. But first I need to enter data in the table "PedidoReservaQuarto" because of the id.
My action controller
public function actionCreate($id)
{
$modelPedidoReservaQuarto = new PedidoReservaQuarto();
$modelPedidoReserva = new PedidoReserva();
$modelPedidoReservaQuarto->quartoId = $id;
$modelPedidoReservaQuarto->save();
if ($modelPedidoReserva->load(Yii::$app->request->post()) && $modelPedidoReserva->save()) {
return $this->redirect(['create', 'id' => $modelPedidoReserva->id]);
}
return $this->render('../pedido-reserva/create', [
'model' => $modelPedidoReserva,
]);
}
Model PedidoReservaQuarto
public function getPedidoReservas()
{
return $this->hasMany(PedidoReserva::className(), ['reservaQuartoId' => 'id']);
}
/**
* Gets query for [[Quarto]].
*
* #return \yii\db\ActiveQuery
*/
public function getQuarto()
{
return $this->hasOne(Quarto::className(), ['id' => 'quartoId']);
}
Model PedidoReserva
public function getReservaQuarto()
{
return $this->hasOne(PedidoReservaQuarto::className(), ['id' => 'reservaQuartoId']);
}
Assuming in you form you have the data you need for both the model and send these data using method post (click submit button)
and you must obtain the id from modelPedidoReserva for assign to model modelPedidoReservaQuarto you could try
public function actionCreate()
{
$modelPedidoReservaQuarto = new PedidoReservaQuarto();
$modelPedidoReserva = new PedidoReserva();
$modelPedidoReservaQuarto->quartoId = $id;
$modelPedidoReservaQuarto->save();
if ($modelPedidoReserva->load(Yii::$app->request->post()) && $modelPedidoReserva->save()) {
$modelPedidoReservaQuarto->load(Yii::$app->request->post());
$modelPedidoReservaQuarto->quartoId = $modelPedidoReserva->id;
$modelPedidoReservaQuarto-save();
}
return $this->render('../pedido-reserva/create', [
'model' => $modelPedidoReserva,
]);
}
once you have saved the $modelPedidoReserva the related id is already available ..
I created a filter for db table, but filtration just work for first page
I try this code in controller
public function index(Request $request)
{
$user=User::orderBy('created_at', 'ASC');
if($request->get('search'))
{
if ($request->get('type')) {
$user->where('type', $request->get('type'));
}
if ($request->get('id')) {
$user->where('id', (integer)$request->get('id'));
}
if ($request->get('first_name')) {
$user->where('first_name', $request->get('first_name'));
}
if ($request->get('email')) {
$user->where('email', $request->get('email'));
}
}
$users = $user->paginate(20);
$user->appends(['search' => $request->get('search')]);
return view('admin.user.index', compact('users'));
}
But I get this error
Call to undefined method Illuminate\Database\Query\Builder::appends()
How I can fix this error or how I can make filter works with all pages ?
As I can see you have used following line and paginate the user data in users
$users = $user->paginate(20);
So you need to use users variable instead of user
$users->appends(['search' => $request->get('search')]);
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);
}
Currently, editing profile works for me. However, my problem is, only one element is successfully edited. I can echo previously saved data, and I can even type and edit each of the textboxes. Problem is, only the revision made on "profile" field is properly reflected. All the other fields remain the same.
here's what I have so far:
in controller page:
public function edit_profile()
{
//loads client profile and allows editing to be done
$this->validateRole('client');
$this->load->model('job_model');
$id = $this->auth_model->get_user_id();
$data['client'] = $this->auth_model->get_client($id);
$this->load->view('client/edit_profile', $data);
}
public function edit_profile_submit()
{
$this->validateRole('client');
$this->load->model('auth_model');
//$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $_POST['tagline']);
$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $_POST['profile']);
//$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $_POST['billing_mode']);
redirect('client/view_profile?message=Profile updated.');
}
in model page:
public function edit_client_profile($id, $profile)
{
// allows client to edit his or her profile
$data = array(
//'tagline' => $tagline,
'profile' => $profile
//'billing_mode' => $billing_mode
);
$this->db->where('id', $id);
$this->db->update('client', $data);
}
I tried editing my controller and model page only to get errors so I commented the added lines for now instead.
I am looking forward to any possible help.
Thanks!
The error is probably because you have two undefined variables in your $data array each time you call the model. Instead of creating the array in your model, create it in your controller:
$data = array(
'tagline' => $_POST['tagline'],
'profile' => $_POST['profile'],
'billing_mode' => $_POST['billing_mode']
);
Then call the model
$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $data);
Remove the array creation from your model, change the second parameter being passed to $data:
public function edit_client_profile($id, $data)
{
$this->db->where('id', $id);
$this->db->update('client', $data);
}