This is a Noob question , but i have searched a lot with no luck.
I am returning array of objects from model to controller and i have to pass it to another model. I will have to convert it but how ?
This is my controller :
$data['customer_phoneno'] = $this->session->userdata('customer_phoneno');
$data['cid']=$this->Stylish_wizard->getCid($this->session->userdata('customer_phoneno'));
$data['bookings']=$this->Myaccount_customer->getBookings($cid);
Model :
public function getBookings($cid)
{
$this->db->where('cid', $cid);
$this->db->from('bookings');
$query = $this->db->get();
return $query->result();
}
I want to get the data from one model and use it to get some other data from another model.
I had tried passing $data['cid'] but got this error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_query_builder.php
Line Number: 662
Backtrace:
File: C:\xampp\htdocs\style\application\models\Myaccount_customer.php
Line: 24
Function: where
File: C:\xampp\htdocs\style\application\controllers\Myaccount.php
Line: 95
Function: getBookings
File: C:\xampp\htdocs\style\index.php
Line: 292
Function: require_once
A Database Error Occurred
Error Number: 1054
Unknown column 'Array' in 'where clause'
SELECT * FROM `bookings` WHERE `cid` = `Array`
Filename: C:/xampp/htdocs/style/application/models/Myaccount_customer.php
Line Number: 26
I tried passing
$data['cid']->cid
But its not working
When i printed $data['cid'] i got
Array ( [0] => stdClass Object ( [cid] => 8 ) )
So how do i convert it in string ?
Then you can achieve it using as
Controller
public function your_function(){
$this->load->model('folder_name/my_calling_model');//initialize that model
//your rest code
$data['my_data'] = $this->my_calling_model->my_calling_function();//your function to be called
}
You can call another model within your code as like above code,but it seems you might be having a typo within your code while passing $cid it seems to be $data['cid'] instead. So your code looks like
$data['bookings']=$this->Myaccount_customer->getBookings($data['cid']);//<----- changed from $cid to $data['cid']
Correct me if I am wrong, but I think you want to achieve this:
$data['customer_phoneno'] = $this->session->userdata('customer_phoneno');
$data['cid']=$this->Stylish_wizard->getCid($this->session->userdata('customer_phoneno'));
$data['bookings']=$this->Myaccount_customer->getBookings($data['cid']);
Make sure to pass the right array / object element.
edit:
Like I said: Make sure to pass the right array or object element. $data['cid'] contains an array. But you have to pass a String(the id). So you have to look at this array and pass the right element with your id.
Related
I have a model called EmploymentApplication that uses a JSON field to hold all of the submitted form data when someone submits their application. This field is simply called "data". I'm working on the resource controller's 'index' method. I only want to return the id, and the first name and last name (that's inside the 'data' JSON field).
I'm trying to figure it out in Tinker (there is currently only one application in the DB):
$result = \App\Models\EmploymentApplications\EmploymentApplication::all(['id', 'data->fname', 'data->lname'])
returns:
Illuminate\Database\Eloquent\Collection {#888
all: [
App\Models\EmploymentApplications\EmploymentApplication {#881
id: 1,
`data`->'$."fname"': ""Betty"",
`data`->'$."lname"': ""Sue"",
},
],
}
So, It's returning the correct data. The part that has me stumped is how to access that data->fname and data->lname from here.
$result[0]->id works just fine.
$result[0]->(backtick)data(backtick)->'$."lname"'
throws:
PHP Parse error: Syntax error, unexpected '`', expecting T_STRING or T_VARIABLE or '{' or '$' on line 1
What is the correct way to access these properties?
I know I could just use array_map to clean up the result before returning it from the controller, but what is the best practice here?
Thank you.
Use column aliases:
$result = EmploymentApplication::all(['id', 'data->fname as fname', 'data->lname as lname']);
$result[0]->fname
$result[0]->lname
I am not sure if this is what you mean, but if I am using tinker and want to get information from my database, this is what I use.
DB::select("SELECT id, fname, lname FROM EmploymentApplication")
or if you are looking for a specific id.
DB::select("SELECT id, fname, lname FROM EmploymentApplication WHERE id=1")
I want to collect multiple checkbox values to process, but I got this
error = Fatal error: Cannot use object of type stdClass as array in this
line = foreach ($keluhan as $gejala) {
array_push($cf_array, $this->Maturan->get_nilai_cf($gejala, $penyakit['kode_penyakit']));
}
Is $panyakit an actual array that your trying to access or an object?
If it is an object try accessing it like this:
$penyakit->kode_penyakit
I have passed $id from the controller as:
$data['getId'] = $id; // say value = 12
return view('Administrator.notification.index',$data);
However, in the view file when I used {{getId}} , it show me the error:
Use of undefined constant getId - assumed 'getId'
The $data variable is acessible from the view, not getId. $data is an array, getId is a key of the $data array and 12 your value according to the key value.
Try printing out something like: {{$data['getId']}}
If you want to see the output of the variable, for debugging purposes, use {{dd($data)}}
https://laravel.com/docs/5.6/views
update this line from
return view('Administrator.notification.index',$data);
To
return view('Administrator.notification.index',compact('data'));
and then in view u can access it like this,
{{ $data['getId'] }}
The error is you are missing the $ sign. It's {{$getId}} not {{getId}}.
I am using rtconner/laravel-tagging package to get tags functionality to my app.
I can count attached tags by $o->tags->count()
I can loop the tags by a foreach: #foreach($o->tags as $t).
print attached tags by
the problem
Now I want to get a collection of random 10 Quotation with no tags attached.
While I can print a random 10 pieces with a given attribute:
$object = Quotation::where('deepness', null)->get()->random(10);
(Note: I have a random scope defined in the model, irrelevant for my issue)
... but this code, cloned from another model doesn't work:
$object = Quotation::whereHas('tags','>',0)->get()->random(10);
It produces this error message:
FatalThrowableError in Builder.php line 880:
Type error: Argument 2 passed to Illuminate\Database\Eloquent\Builder::whereHas() must be an instance of Closure, string given
I have also tried to execute this query
$object = Quotation::has('tags')->get()->random(10);
but I got this:
```
BadMethodCallException in Builder.php line 2431:
Call to undefined method Illuminate\Database\Query\Builder::tags()
```
Note 2: In the source model (the one I cloned from) the relation was counting a hasMany relation.
to do
Please help me to create the collection of Quotations with no tags assigned
Had the same problem and solved it this way:
$objects = Quotation::all();
$objects = $objects->filter(
function ($object, $key) {
return $object->tags->count() > 0;
}
)->random(10);
Hope issues still relevant :)
In my application controller, I have a method transaction_reimburse() that receives ids as POST string values. The method's function is to get all transactions that match the ids from the database, and update them accordingly.
Controller method:
public function transaction_reimburse() {
$reimburse = array('reimburse' => 1);
$transactions = $this->Transaction_model->get_these_ids($this->input->post('ids'));
$this->Transaction_model->reimburse_these($transactions, $reimburse);
}
Model method:
function get_these_ids($ids) {
$this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
$this->db->from('tbl_transactions');
$this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
$this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
$this->db->where("tbl_transactions.id IN (". $ids .")");
return $this->db->get();
}
Model method:
function reimburse_these($transactions, $reimburse) {
$this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
$this->db->from('tbl_transactions');
$this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
$this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
$this->db->where("tbl_transactions.id IN (". $transactions .")");
$this->db->update($this->tbl_transaction, $reimburse);
}
However, I am getting the following error:
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: models/transaction_model.php
Line Number: 450
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
UPDATE `tbl_transactions` SET `reimburse` = 1 WHERE `tbl_transactions`.`id` IN ()
Filename: C:\xampp\htdocs\ipa\system\database\DB_driver.php
Line Number: 330
What I need to know is why do I get these errors?
The errors mean that your $ids variable is not in a string format (meaning: 1,2,3,4. Its probably an array). To fix it, first find out what format it is on by using print_r($ids) or var_dump($ids). Once you know the format, use PHP functions(such as implode()) to transform it into strings.
IN is looking for a set in the format e.g. (1,2,3,4).
Either make your post values adopt the format 1,2,3,4 so that when the value is included into the query it's valid or if you're sending an array use a method in PHP such as implode(',',$array) to create the desired format for binding into the query..
I have reviewed your both function and found that in both function you put tbl_transactions.id in where clause and ids is passed from post and hence there is no need to call get_these_ids to get ids, you already have ids in post array and hence instead of transactions variable, just use $this->input->post('ids') for reimburse_these functions.
if you surely want to get tbl_transactions from get_these_ids function then you need to use group_concat with group by in query of get_these_ids function and just return that concated id in get_these_ids function then it will work.