Laravel - Query to mysql DB - php

I am new to Laravel. I have a existing and working query that allow me to get all the item present in my Mysql Table.
File --> ProductRepostitory.php
Public function getAll(){
$data = Product::paginate(10);
if(empty($data)){
return false;
}else{
return $data;
}
}
In that table I have a column named PR_ACTIF --> 1 or 0.
I would like to display in the query only PR_ACTIF = 1
Thanks for you help!

Try this
...
Public function getAll(){
$data = Product::where('PR_ACTIF', 1)->paginate(10);
if(empty($data)){
return false;
}else{
return $data;
}
}
...

Related

Using where condition getting blank page in codeigniter mysql

Hi i have used where condition in mysql after using that if i run the website getting the blank page.
Actually i am three department like QA Department,Development,Management.
IN Mysql i have used where condition to get only QA department list but getting blank page.
Here is my code:
Controller:
public function index()
{
$data['records2'] = $this->index_model->get_ourteammembers();
$data['mainpage'] = "our-team";
$this->load->view('templates/template',$data);
}
Model
function get_ourteammembers()
{
$this->db->select('T.*');
$this->db->from('ourteam AS T');
$this->db->where('T.department'='QA Department');
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
Try This
function get_ourteammembers()
{
$this->db->select('T.*');
$this->db->from('ourteam AS T');
$this->db->where('T.department','QA Department');
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
As Per CodeIgniter Document you have to use ',' instead of '='
$this->db->where('T.department'='QA Department');
To
$this->db->where('T.department','QA Department');

Get last insert id return null

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()

Displaying Flash data in php codeigniter

I am struggling with flash data in codeigniter. My edit function is working properly. But problem is with delete function.My controller is as follows
public function rmRole()
{
$data = array();
$roleId = $this->uri->segment(4,0);
if ($this->_delete($roleId)) {
$this->session->set_flashdata('flash_message' ,'data_deleted');
redirect('adminroles');
}
}
My model includes
public function _delete($id) {
$table = $this->get_table();
$this->db->where('id', $id);
$this->db->delete($table);
}
My data is got deleted. But it is redirecting to a blank page with the url. What shall i do to display a flash data like data deleted successfully and redirect to adminroles module?
try this in model
function delete($id) {
if ($id)
{
if ($this->db->where("id", $id)->delete("table name")){
return TRUE;
}
else{
return FALSE;
}
}else{
return FALSE;
}
}

Inserting multiple values in database codeigniter?

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);
}
}

laravel4 select all data from other table by relation(one to many)

i have two tables users its model (User) ... and servs it model (servs) .... the relation is one to many .... when i try to select all sevices with belong to one user .... it select first service only and ignore others ... this is code i used it
public function getserv(){
return View::make('infos.serv');
}
public function postserv(){
$user = User::find(Auth::user()->id);
$user_id = $user->id;
$serv = servs::where('user_id','=',$user_id);
if($serv->count()){
$serv = $serv->get();
//return $serv->user_id;
foreach ($serv as $servs) {
return $servs->serv_id;
}
}
}
Instead of returning the data at the first loop, you should better do something like this:
$result = array();
foreach ($serv as $servs) {
$result[] = $servs->serv_id;
}
return $result;
Try this.
public function postserv(){
$user = User::find(Auth::user()->id);
$user_id = $user->id;
$serv = servs::where('user_id','=',$user_id)->get()->first;
if($serv)
return $serv->serv_id;
else
return null;
}
You only see the first one because when you return something the function ends and the rest of $serv doesn't get processed.
I recommend you first set up Eloquent relations properly
class User extends Eloquent {
public function servs(){
return $this->hasMany('servs');
}
}
After that you can retrieve all servs for a user like this:
$user = Auth::user();
$servs = $user->servs;
foreach ($servs as $serv) {
echo $serv->serv_id;
}

Categories