I have db table images with primary key ID and auto increment. Am adding new images with ajax > controller > model.
My model for create has method create
public function create($data = array())
{
$this->db->insert($this->_table, $data);
return $this->db->insert_id();
}
public function getLastInserted() {
return $this->db->insert_id();
}
And in controller i create test metod to check what is last id inserted:
public function lastID()
{
var_dump($this->photo->getLastInserted());
}
// int(0)
But record is succeffull inserted in database. What can be problem ?
From the official docs: mysqli::$insert_id - Returns the auto generated id used in the LAST query
Make sure you are not doing any other queries AFTER your create() method.
I would suggest to place a last inserted ID into class variable, to avoid problems like these in the future:
private $lastInsertId = null;
public function create($data = array())
{
$this->db->insert($this->_table, $data);
$this->lastInsertId = $this->db->insert_id();
return $this->lastInsertId;
}
public function getLastInserted() {
return $this->lastInsertId;
}
Your create function return last insert id. To see your last insert id just use this code in controller function
public function lastID()
{
var_dump($this->photo->create('table_name',$data));
}
photo model function
function create($tableName, $data)
{
$this->db->insert($tableName, $post);
return $this->db->insert_id();
}
lastInsertid only works when you use it on your db connection variable like
dbconnection->lastInsertId()
Related
Please tell me the solution if exists. This is my code:
public function handleRequest()
{
if (request()->filled('location_id')) {
$this->notification = $this->notification
->whereHas('location', function ($query)
{
$query->where('location_id',
request('location_id'));
});
}
return $this->notification;
}
I want to get the data of the "notification" table only, but this query returns "Location" table's data too. I have to use the $query scoped solution.
Why don't you try select function select(['Notifiaction table column name'])
public function handleRequest()
{
if (request()->filled('location_id')) {
$this->notification = $this->notification
->whereHas('location', function ($query)
{
$query->where('location_id',
request('location_id'));
})->select(['Notifiaction table column name']);
}
return $this->notification;
}
I try to get my categories base on their status and i have scope below in my category model:
public function scopeOfStatus($query, $status)
{
return $query->where('status_id', $status);
}
And my controller is like:
public function finder() {
$findercategories = Category::OfStatus('Active')->get();
return response()->json($findercategories);
}
My route is like:
Route::get('/finder','frontend\SearchController#finder');
But i get blank page as result. Any idea?
update
if i use $findercategories = Category::ofStatus(1)->get(); i get the result that i want but it's static not dynamic :\
Get the Id's from the statuses table with respect to the searched value and use the Id's to get the Category
public function scopeOfStatus($query, $status)
{
$statuses = Status::where('title', $status)->pluck('id'); // Or relevant column name
return $query->whereIn('status_id', $statuses);
}
and in your Controller you could do so
public function finder() {
$findercategories = Category::ofStatus('Active')->get();
return response()->json($findercategories);
}
I'm have got a problem about showing data with multi relation table,
I just want show data from jadwal table with specified value from kelas.nama_kelas and sort it by jam.waktu_mulai,
for example : Showing data from jadwal where has nama_kelas=3C
this my model.php
public function dosen(){
return $this->hasMany('App\Dosen','id');
}
public function jurusan(){
return $this->hasMany('App\Jurusan','id');
}
public function ruang(){
return $this->hasMany('App\Ruang','id');
}
public function kelas(){
return $this->hasMany('App\Kelas','id');
}
public function makul(){
return $this->hasMany('App\Matakuliah','id');
}
public function hari(){
return $this->hasMany('App\Hari','id');
}
public function jam(){
return $this->hasMany('App\Jam','id');
}
This my controller.php
public function show($id)
{
$dec = $id;
$sort = \App\Kelas::find($dec);
$jadwals = Jadwal::join('dosen','dosen.nip','=','jadwal.nip')
->join('jurusan','jurusan.kode_jurusan','=','jadwal.kode_jurusan')
->join('ruang','ruang.kode_ruang','=','jadwal.kode_ruang')
->join('kelas','kelas.kode_kelas','=','jadwal.kode_kelas')
->join('mata_kuliah','mata_kuliah.kode_mk','=','jadwal.kode_mk')
->join('hari','hari.kode_hari','=','jadwal.kode_hari')
->join('jam','jam.kode_jam','=','jadwal.kode_jam')
->select('*','jadwal.id as jadwal_id','kelas.id as kelas_id')
->where('kelas.nama_kelas','LIKE','%'.$sort->nama_kelas.'%')
->orderBy('jam.waktu_mulai','Asc')
->get();
return view('Akademik.Jadwal.jadwalDetail',compact('jadwals'));
}
And this my database entiti reltion diagram :
sorry for my bad english
EDIT 1:
I have been test and do a research it works but when i try to print $jadwal->kelas_id it return nul...but not for $jadwal->jadwal_id,,,
I am working on notifications I have 2 tables: one is notify and the other is notify_status. Through notify I am showing data like title and description and in notify_status I have field read_status which is by default 0. After I show it I want to change it to 1. I also have notify_id in it as a foreign key. This is my show method:
public function show($id)
{
$notify = Notify::find($id);
$notify_status = NotifyStatus::where('notify_id', $id)->get();
$user_data['read_status'] = 1;
$user = NotifyStatus::create($user_data);
return view('notify.desr')->with(compact('notify'));
}
But it isn't creating against notify_id. What should I do?
Your models should define the following relationships:
class Notify extends Model
{
public function setAsRead()
{
$this->status->read_status = 1
$this->status->save();
}
public function wasRead()
{
return (bool) $this->status->read_status;
}
public function status()
{
return $this->hasOne(NotifyStatus::class);
}
}
class NotifyStatus extends Model
{
public function notify()
{
return $this->belongsTo(Notify::class);
}
}
Take a look at Laravel Eloquent Relationships for further reading.
In your controller you can use it like:
$notify = Notify::find($id);
$notify->status->read_status = 1;
$notify->status->save();
return view('notify.desr')->with(compact('notify'));
Or you can simply create a new method to set the new status (take a look at first method of Notify class):
$notify = Notify::find($id);
$notify->setAsRead();
return view('notify.desr')->with(compact('notify'));
public function show($id)
{
$notify = Notify::find($id);
$notify_status = NotifyStatus::where('notify_id', $id)->first();
$notify_status->read_status = 1;
$notify_status->save()
return view('notify.desr')->with(compact('notify'));
}
I want to insert multiple rows in the database on one click if row is checked using multiple checkbox
Here is my code=>
1)Controller: guard.php
(Here I take a array of list of student id's and pass them one by one to the another function get_leave_data which take id of a student and return more information about the student from another table leave_application).
public function students_out(){
$request=$this->input->post('approve_leave_status');
if($request=="OUT"){
$check_list_out[]=$_POST['check_list_out'];
if(!empty($check_list_out)){
foreach ($check_list_out as $check_list_id) {
$student_data=$this->get_leave_data($check_list_id);
$this->insert_student_outside($student_data);
}
$this->load->helper('url');
$this->load->view('view_guard');
}
}else{
$this->load->helper('url');
$this->load->view('view_guard');
}
} `
public function get_leave_data($id){
$this->load->model('model_guard');
$data=$this->model_guard->get_data($id);
return $data;
}
public function insert_student_outside($std_data){
$this->load->model('model_guard');
$data=$this->model_guard->insert_student_out($std_data);
return $data;
}
2)Model: model_guard.php
(The functions get_data() and get_data2() return more informations about student and function insert_student_out() insert the student to the student_outside table)
public function get_data($id){
$this->db->select('leave_id,leave_from_roll_no,leave_student_name,leave_going_to,leave_from_date,leave_till_date,leave_hostel_no,leave_status');
$this->db->from('leave_application');
$this->db->where('leave_id',$id[0]);
$query=$this->db->get();
$data1=$query->result();
$data2=$this->get_data2($data1);
$final_array=array_merge($data1,$data2);
return $final_array;
}
public function get_data2($array){
foreach ($array as $key) {
$roll_no=$key->leave_from_roll_no;
}
$this->db->select('student_year,student_semester,student_parent_email');
$this->db->from('students');
$this->db->where('student_roll_no',$roll_no);
$query=$this->db->get();
$data=$query->result();
return $data;
}
public function insert_student_out($std_data){
$roll_no=$std_data[0]->leave_from_roll_no;
$id=$std_data[0]->leave_id;
$date_out=date('Y-m-d');
$inside_date=NULL;
$date_allowed=$std_data[0]->leave_till_date;
$array=array(
'outside_id'=>$id,
'outside_roll_no'=>$roll_no,
'outside_date_out'=>$date_out,
'outside_date_in'=>$inside_date,
'outside_date_allowed'=>$date_allowed
);
if($this->db->insert('students_outside',$array)){
return true;
}else{
false;
}
}
you are trying to insert batch_array using codeignator
the right syntax to using batch_array is:-
$array=array(
'coloum_name'=>$id,
'coloum_name'=>$roll_no,
'coloum_name'=>$date_out,
'coloum_name'=>$inside_date,
'coloum_name'=>$date_allowed
);
if($this->db->insert_batch('students_outside',$array)){
return true;
}else{
false;
}
you can read manual insert_batch here
ok, i am assuming that in $std_data you have more than one record.then in your model's insert_student_out() function try this-
public function insert_student_out($std_data){
foreach($std_data as $row){
$roll_no=$row->leave_from_roll_no;
$id=$row->leave_id;
$date_out=date('Y-m-d');
$inside_date=NULL;
$date_allowed=$row->leave_till_date;
$array=array(
'outside_id'=>$id,
'outside_roll_no'=>$roll_no,
'outside_date_out'=>$date_out,
'outside_date_in'=>$inside_date,
'outside_date_allowed'=>$date_allowed);
$this->db->insert('students_outside',$array);
}
}