I need the following code in codeingiter.
mysql_query("DELETE `father_name` FROM `table` WHERE `id`='$id'");
So that on father name delete from the row not the whole row. Thanks in advance.
Done in codeigniter like this.
$data = array('father_name' => 'NULL');
$this->db->where('id', $id);
$this->db->update('table', $data);
Related
Here I am using laravel 5.5. My code is
$result = DB::insert('INSERT INTO .......');
Here it returns true. But how can I get inserted id? In my table id is the primary key. Thanks in advance
you can use insertGetId().
If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:
$id = DB::table('users')->insertGetId(
['email' => 'john#example.com', 'votes' => 0]
);
doc can be found here.
Instead of using DB method you can simply use Laravel eloquent:
$result = <YOUR_MODEL_NAME>::create(<YOUR_DATA>)->id();
it return the last inserted record id.
And make sure if you use this method you need to add $fillable in your MODEL like:
class <YOUR_MODEL> extends Model
{
protected $fillable = [ 'column_name_1', 'column_name_2', .., 'column_name_n' ];
}
Why don't you order your rows and get last id?
select <primary key> from <table> order by desc limit 1;
$result = MODEL_NAME::create(data);
return $result->id;
try this may be it's working
I want to join another table with customer grid condition will be custom attribute value and table column value.
I have added one custom attributes for customers, attribute name is affiliate_id, and my other table name is aff_accounts, I want to show the aff_accounts table's column in the customer grid.
Please help me.
Thanks
If you are facing any issue you can ask me.Try the below code:
protected function _prepareCollection() {
$collection = Mage::getResourceModel('customer/customer_collection')->addNameToSelect();
$collection->getSelect()->join(
array('e' => 'event'), 'e.customer_id=main_table.entity_id', array('e.status') // added 'e.status' in stead of 'status'
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
Hope it will help you.
I have a page model. It has following columns in database table:
id
title
image
body
I want to update only "image" column value.
Here's my code:
public function delImage($path, $id) {
$page = Page::find($id);
$page->where('image', $path)->update(array('image' => 'asdasd'));
\File::delete($path);
}
it throws me an error, that i am trying to use where() on a non-object. How can i correctly update my "image" column value?
You may try this:
Page::where('id', $id)->update(array('image' => 'asdasd'));
There are other ways too but no need to use Page::find($id); in this case. But if you use find() then you may try it like this:
$page = Page::find($id);
// Make sure you've got the Page model
if($page) {
$page->image = 'imagepath';
$page->save();
}
Also you may use:
$page = Page::findOrFail($id);
So, it'll throw an exception if the model with that id was not found.
I tried to update a field with
$table->update(['field' => 'val']);
But it wasn't working, i had to modify my table Model to authorize this field to be edited : add 'field' in the array "protected $fillable"
Hope it will help someone :)
Version 1:
// Update data of question values with $data from formulay
$Q1 = Question::find($id);
$Q1->fill($data);
$Q1->push();
Version 2:
$Q1 = Question::find($id);
$Q1->field = 'YOUR TEXT OR VALUE';
$Q1->save();
In case of answered question you can use them:
$page = Page::find($id);
$page2update = $page->where('image', $path);
$page2update->image = 'IMGVALUE';
$page2update->save();
Try this method short and clean :
Page::where('id', $id)
->update(['image' => $path]);
An example from Laravel doc
Flight::where('active', 1)
->where('destination', 'San Diego')
->update(['delayed' => 1]);
DB::table('agents')
->where('id', $agentid)
->update(['status' => '0']);
I want to do the following:
//set up insert....
$this->db->insert('property');
$id = $this->db->insert_id();
//some stuff
//set up get
$this->db->where('id', $id);
$id = $this->db->get();
This does not work. It would appear the insert is not being executed before the get - the get is returning zero rows. The insert does (eventually) work. Any suggestions?
You need to give insert some data to insert.
With either the set method:
$this->db->set('name', 'Eric');
$this->db->insert('property');
or by passing an array as the 2nd parameter:
$this->db->insert('property', array(
'name' => 'Eric'
));
As, for your select, you need to tell it what table to select from.
Use either the from method:
$this->db->from('property');
$this->db->where('id', $id);
$id = $this->db->get();
or pass get a table as a parameter:
$this->db->where('id', $id);
$id = $this->db->get('property');
Also, note that get() returns a query object. You need to use ->row (or ->result) to get the data.
$this->db->from('property');
$this->db->where('id', $id);
$query = $this->db->get();
$id = $query->row()->id;
You're missing an argument - insert() takes two:
A table name
An array or object containing columns and values
From the documentation:
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
So, you need to supply insert() with the necessary data by including an array or object as the second argument.
Edit:
Alternatively, you can use the $this->db->set() method for setting values, as explained in Rocket's more comprehensive answer, which also points out you need to specify a table when selecting data.
It says , you must use the “set” method to update an entry. Pls help
My model is
$this->db->where('id', $this->uri->segment(3));
$this->db->update('mytable', $data);
My controller is
$data = $this->db->select('mytable', $_POST);
$this->contact_model->model_update_function($data);
Your $data variable doesn't contain a valid array. This is because $this->db->select(); doesn't actually run the query, you need $this->db->get(); or $this->db->get_where(); in order to do so. Even then, you also need to call $query->result(); to retrieve the data from the result.
Your controller should be
$query = $this->db->get_where('mytable', $_POST);
$data = $query->result();
$this->contact_model->model_update_function($data);