Hello I am new in CI,
How I can put 2 filters to delete using funtions of codeigniter?
I have this
$this->db->where('id_casa', $id_casa);
$this->db->delete("casas");
can I do this?
$this->db->where('id_casa', $id_casa);
$this->db->and('id_usuario', $id_usuario);
$this->db->delete("casas");
Thanks !!!
just use
//Associative array method:
$this->db->where(array('id_casa' => $id_casa,'id_usuario'=>$id_usuario));
$this->db->delete("casas");
(or) If you use multiple function calls they will be chained together with AND between them:
$this->db->where('id_casa' => $id_casa);
$this->db->where('id_usuario'=>$id_usuario);
$this->db->delete("casas");
//both produce : where id_casa=$id_casa and id_usuario=$id_usuario
You can create an array for the conditions to give in where clause
$condition = array('id_casa' => $id_casa,'id_usuario'=>$id_usuario);
$this->db->where($condition);
$this->db->delete("casas");
Related
I'm trying to insert data into MYSQL and update if there is already a value there. Can't seem to get the insert statement right. This is what I wrote
DB::insert('insert INTO dataentries (MAth) values(?)',[$total])->where('ID_number', $id_numbers);
You can use an updateOrCreate statement.
Something like this (not tested):
$updateOrCreate = App\Dataentries::updateOrCreate(
['total' => $value],
['id' => $id]
);
https://laravel.com/docs/5.3/eloquent
Use updateOrCreate():
$dataentry = App\Dataentry::updateOrCreate(
['ID_number' => $id_number],
['MAth' => $total]
);
I guess I am breaking all the rules by deliberately making a duplicate question...
The other question has an accepted answer. It obviously solved the askers problem, but it did not answer the title question.
Let's start from the beginning - the first() method is implemented approximately like this:
foreach ($collection as $item)
return $item;
It is obviously more robust than taking $collection[0] or using other suggested methods.
There might be no item with index 0 or index 15 even if there are 20 items in the collection. To illustrate the problem, let's take this collection out of the docs:
$collection = collect([
['product_id' => 'prod-100', 'name' => 'desk'],
['product_id' => 'prod-200', 'name' => 'chair'],
]);
$keyed = $collection->keyBy('product_id');
Now, do we have any reliable (and preferably concise) way to access nth item of $keyed?
My own suggestion would be to do:
$nth = $keyed->take($n)->last();
But this will give the wrong item ($keyed->last()) whenever $n > $keyed->count(). How can we get the nth item if it exists and null if it doesn't just like first() behaves?
Edit
To clarify, let's consider this collection:
$col = collect([
2 => 'a',
5 => 'b',
6 => 'c',
7 => 'd']);
First item is $col->first(). How to get the second?
$col->nth(3) should return 'c' (or 'c' if 0-based, but that would be inconsistent with first()). $col[3] wouldn't work, it would just return an error.
$col->nth(7) should return null because there is no seventh item, there are only four of them. $col[7] wouldn't work, it would just return 'd'.
You could rephrase the question as "How to get nth item in the foreach order?" if it's more clear for some.
I guess faster and more memory-efficient way is to use slice() method:
$collection->slice($n, 1);
You can try it using values() function as:
$collection->values()->get($n);
Based on Alexey's answer, you can create a macro in AppServiceProvider (add it inside register method):
use Illuminate\Support\Collection;
Collection::macro('getNth', function ($n) {
return $this->slice($n, 1)->first();
});
and then, you can use this throughout your application:
$collection = ['apple', 'orange'];
$collection->getNth(0) // returns 'apple'
$collection->getNth(1) // returns 'orange'
$collection->getNth(2) // returns null
$collection->getNth(3) // returns null
you may use offsetGet since Collection class implements ArrayAccess
$lines->offsetGet($nth);
Maybe not the best option, but, you can get item from array inside collection
$collection->all()[0]
result_array() for a query gives the following :
Array ( [0] => Array ( [id] => 1 ) [1] => Array ( [id] => 4 ) )
I want to use id=1 and id=4 in the where clause in the OR condition like the following :
$this->db->where_in('id',$query->result_array());
But the above causes error. Is there any direct way of doing the same?
You can simply use array_column
array_column($query->result_array(), 'id')
here is reference to it.
$result = $roleIdQuery->result_array();
$column = array_map(function($sub)
{
return $sub['id'];
}, $result);
I used this. Sadly.
because it has arrays within that array.
$ids=array();
$resultArray=$query->result_array();
foreach($resultArray as $result){
$ids[]=$result['id'];
}
$ids // this is what you need to pass now..
If you change your original query to use GROUP_CONCAT. Not an option in Active Record so would need to write your own SQL. something like
$query= $this->db->query('SELECT GROUP_CONCACT(id) AS ids FROM table WHERE condition=true');
Then you should be able to do
$this->db->where_in('id',$query->result_array()[0]['ids']);
Just noticed your on 5.3 so unfortunately that means this won't work you will have to do.
$result = $query->result_array();
Then pass in $result[0]['ids']
I have a URL www.mydomain.com/jobs?applications=2|4|6
I am trying to get the values to work with my Input::get but failing. I have tried using array but this doesn't work. Can anyone advise? I'm unfamiliar with using Laravel with this structure of querystring.
$applicationIDs = Input::get('applications');
$applications = Job::with('users');
if(!empty($applicationIDs)){
$applications->whereIn('id', $applicationIDs);
}
$applications = $applications->get();
Your applications parameter is just a string. Use explode to turn it into an array of ids:
$applicationIDs = explode('|', Input::get('applications'));
I wonder, if someone could help me...
Is there a LINQ query that will return a bool, if any item from one IList<> is contained int another IList<>.
These IList<>'s are object and I need to compare one a single property of the object, the "Name" property in this case?
Is there a LINQ query that can do this? If so could someone show me the correct implementation?
Thank you
Well you could project both lists:
if (list1.Select(x => x.Name)
.Intersect(list2.Select(x => x.Name))
.Any())
Is that what you're after?
I think this should do it:
bool matchExists = list1.Any(a1 => list2.Any(a2 => a1.Name == a2.Name));
Another one for your options:
List1.Where(l => List2.Select(s => s.Name).Contains(l.Name)).Any();
I usually use it like below:
List<UserInfo> userUpd = nd.Where(x => !rd.Any(y => y.Identifier.Equals(x.Identifier))).ToList();