When I create a new User, I store in session the new e-mail like this:
$this->session->set_userdata('new_user', ['new_email', $data['email']]);
If I use var_dump on $this->session->userdata('new_user'), the result is the following:
array(2) { [0]=> string(9) "new_email" [1]=> string(24) "email#email.com" }
But when I try to access it to print the value, by echoing $this->session->userdata('new_user')['new_email'], an error saying the index new_email does not exist. I've used this way of printing values in my whole website, but specifically in this page, it does not work.
Use $this->session->set_userdata('new_user',$data['email'])
Related
I am facing the data from the database as an array and now I want to access the data in the same Controller method. Is it possible?
suppose my MODEL is (method name attendance)
$query = $this->db->query("SELECT emp_name FROM attendance WHERE id= '1' ");
$result = $query->getResult('array');
My controller:
$data["emp_info"]=$this->userdata->attendance();
return view("addleave_v",$data);
So in the controller, I have fetched the data and stored it on $data['emp_info'] it is a single element array. So I want to use the emp_name in the controller. How can I use it? I tried emp_info['emp_name], $data['emp_info']['emp_name']
I have used an image attachment. Thanks
I recommend you to see the library Result Arrays and Result Rows.
Based on the data you provided (var_dump)
array(1) { [0]=> array(10) { ["uiid"]=> string(2) "30"
["attendance_date"]=> string(10) "2021-03-09" ["emp_id"]=> string(1)
"9" ["time_in"]=> string(19) "0000-00-00 00:00:00" ["time_out"]=>
string(19) "0000-00-00 00:00:00" ["comments"]=> string(14) "APPROVED
LEAVE" ["overtime"]=> string(1) "0" ["status1"]=> string(1) "1"
["created_by"]=> string(0) "" ["total_time"]=> string(0) "" } }
Your array is structured as a multi-element array. You can view the current data using a loop (foreach) or refer to the first element of an array (in controller $data["emp_info"][0]['emp_name'] or in view $emp_info[0]['emp_name']).
If you need a single-element array, use getRowArray() or getRow('array'). Then you will be able to use the variables as you first wanted.
in controller $data["emp_info"]['emp_name']
in view $emp_info['emp_name]
I recommend that you try the print_r($emp_info); command in your view(addleave_v). You will see what data structure has been assigned to the variable.
I have varchar field in my table and I want to using unique validate but want to remove the whitespace in my table before validate it.
Example :
I have value in database "My Value". And if I send "myvalue" to request data, the validation must be work in this case. How to do it in laravel validation?
I don't know exactly what do you want to do.. DO you mean to do like this?
Go to your model then add this code
public function getNameAttribute($value) {
return str_replace(" ", "", $value);
}
I wanted to have the result with only the values not with the table column name in Laravel 4.2. For example,
$recs = DB::table('table_name')
->select('id', 'title')
->get();
the result is
array(2) {
[0]=>
object(stdClass)#863 (2) {
["id"]=>
int(2)
["title"]=>
string(8) "my title"
}
[1]=>
object(stdClass)#862 (2) {
["id"]=>
int(3)
["title"]=>
string(10) "my title 2"
}
}
but I want to have only the result NOT the column name, like
[
2,
"my title"
],
[
3,
"my title 2"
]
yes, I can make it by looping the result and make new array without column name. But is there are any way to get the result in this fashion using Laravel ?
Try
$recs = DB::table('table_name')->pluck('id', 'title');
dd($recs->all());
You can use the map() and the array_values() methods:
$recs->map(function($i) {
return array_values((array)$i);
})
I'm not sure about Laravel 4.2, but I've just tested it and it works perfectly in Laravel 5.3. If it doesn't work in 4.2, use the native array_map() function instead.
Reference
Try $recs->flatten()->all()
Update
Since your $recs looks like an array from your error. Try converting to collection. I hope this will work on v4.2
$recsCollection = new \Illuminate\Database\Eloquent\Collection($recs);
$recsCollection->flatten()->all();
DB::table('table_name')->all()->lists('id', 'title')->toArray()
Referance: Retrieving A List Of Column Values
i want to check username if taken, but eloquent is running weird for me...
In my database, only one user is registered, username is yigitabi
when i try to get user where username is yigitabi it returns the row but
when i try to get user where username is yiğitabi it returns same row...
i have tried querylog and my two query logs are here...
array(1) { [0]=> array(3) { ["query"]=> string(42) "select * from `users` where `username` = ?" ["bindings"]=> array(1) { [0]=> string(9) "yiğitabi" } ["time"]=> float(0.24) } }
array(1) { [0]=> array(3) { ["query"]=> string(42) "select * from `users` where `username` = ?" ["bindings"]=> array(1) { [0]=> string(9) "yigitabi" } ["time"]=> float(0.24) } }
How can i fix this situation?
There is an excellent discussion of the issue at MySQL matching unicode characters with ascii version.
In order to get this to work as you expect, you need to change the collation from utf8_unicode_ci to utf8_bin. However, you need to keep in mind that there are three collation settings: one for the database, one for each table, and then one for each field.
Changing one of these collations does not affect the others. Changing the collation on the database does not affect the collation set on any existing table or field; it changes the default collation for future tables. Likewise, changing the collation on a table does not affect the collation set on any existing fields in that table; it changes the default collation for future fields.
So, to directly address your issue, you need to change the collation on the username field to be utf8_bin. It is up to you to determine what other fields, tables, or database for which you may want to change the collation.
I try to get the last ID of an attached model ?
My code is the following to attach the model :
$menu->pages()->attach($element['page_id'], array('name' => $element['name'], 'order' => $i, 'menu_page_id' => NULL));
But after that, I want to know the ID of the attached model because I have a foreign key "menu_page_id" which is related to the same model.
Any idea how to do that or another way ?
Thanks
This is untested, but I don't see any reason why it wouldn't work.
$menu_page_id = $menu->pages()->wherePivot('page_id', $element['page_id'])->first()->pivot->menu_page_id;
Edit:
I just verified and this does indeed work for you, but only as long as you have your pages relationship setup like so...
public function pages()
{
return $this->belongsToMany('Page')->withPivot('menu_page_id');
}
Or if you don't want to do that, this should work too...
$menu_page_id = $menu->pages()->withPivot('menu_page_id')->wherePivot('page_id', $element['page_id'])->first()->pivot->menu_page_id;