Laravel - Undefined property: stdClass::$text - laravel-5

Why would $text be providing an Undefined Property error?
If I run dd($mentions) prior to the function it definitely exists and contains the text property.
If I run dd($mention->text) before return and in the function I also get what is expected.
However, the function will not return a value for $text and instead errors out.
$text = $mentions->map(function ($mention) {
return $mention->text;
});

I'd bet that you have 1 object in the $mentions collection that doesn't have the ->text property ?
Try:
if(!isset($mention->text)){
dd($mention))
}
to find out which one.

Related

Error Undefined error:addition in laravel

I am creating some code using laravel in controller and i want send array from controller to blade file i face problem 1/1) ErrorException
Undefined variable: addition It runs but now face this error
$get_details=DB::select('SELECT deliver_date,GROUP_CONCAT(orders_qty) as orders_qty FROM `orders` WHERE order_status=? and deliver_date between ? AND ? GROUP BY deliver_date',[6,'2019-02-01','2020-02-05']);
foreach($get_details as $date_wise_details)
{
$array=explode(',',$date_wise_details->orders_qty);
$addition[]=array_sum($array);
}
return view('dashboard',['get_data'=>$get_details])->with('addition',$addition);
You need to define addition array to resolve this issue
$addition = []; // define your array here
foreach($get_details as $date_wise_details)
{
$array=explode(',',$date_wise_details->orders_qty);
$addition[]=array_sum($array);
}

Is there any way to pass variable in rememberForever callback?

I have following code,
\Cache::rememberForever('Roles', function() {
return RoleModel
::where('ParentRoleID' >= $CurrenctUserRoleID)
->get();
});
Issue is: I am getting Error
Undefined variable: CurrenctUserRoleID
Question: Is there any way to pass variable in callback?
You may try this (Notice the use of use keyword):
$CurrenctUserRoleID = 1; // Some id
\Cache::rememberForever('Roles', function() use($CurrenctUserRoleID) {
return RoleModel
::where('ParentRoleID' >= $CurrenctUserRoleID)
->get();
});
Check the PHP manual: Inheriting variables from the parent scope.
PHP.net - anonymous functions - Example #3
You aren't passing anything with the callback as you are not the caller of that callback. You are telling PHP to use a variable from the parent scope.
function (...) use (...) { ... }

Laravel: Call to undefined method save()

I want to update all my records where the name is like the one im referencing it.
However im getting an error saying Call to undefined method save()
Here's my code
$section = Section1::where('name', 'like', 'ss123')->get();
$section->name = Input::get('name');
Please help :(
$section = Section1::where('name', 'like', 'ss123')->get() returns a collection (think of an array).
Therefore you should loop through the collection using foreach and apply the save to each object in the collection, as such:
foreach($section as $s)
{
$s->name = Input::get('name');
$s->update();
}
Then again this ain't very DB-friendly.
Therefore, the most appropriate action in your case will be a mass update:
Section1::where('name','like','ss1213')->update(['name'=>Input::get('name')]);
try dd($section) and find the available method to prevent call the undefined method.
I assume you are using $section->save();
if you want to update,
use $section->touch();
or $section->update(array('name' => Input::get'name'));

getting the value of the single field output using the codeigniter active record

the following function is supposed to read the name of the given asset code from the database. but it triggers the error: "Trying to get property of non-object"
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get()->result()->row('name');
}
All I want is to have the name of the asset returned back to the controller! Your help is highly appreciated!
Use row() like,
return $this->db->get()->row()->name;
Use row() for a single row, and result() for multiple rows.
do like this, asset_types is your table name
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get('asset_types');
}
And in your controller acess it like
$result=$this->modelname->sban_name('$asset')->row();
$name=$result->name;
I think it's important to check if the record that satisfies the conditions even exists in the database. Code for the model:
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
$row = $this->db->get()->row();
if (isset($row)) {
return $row->name;
} else {
return false;
}
}
Simply call the function from the controller like so:
$response = $this->model_name->sban_name($asset)
Try this code of block , I already checked and works fine:
function sban_name($asset)
{
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code', $asset);
return $this->db->get()->row()->name;
}

CodeIgniter problem retrieving and displaying data from DB

Here is my function. It is very simple.
function load_data() {
$query = $this->db->query('SELECT * FROM configurations WHERE username="' . $this->session->userdata('username') . '"');
return $query;
}
My controller has this line of code:
$data['query'] = $this->configurations->load_data();
In my view, I tried:
foreach($query->result_array() as $row) {
echo $row->first;
}
But I get an error that I am trying to get a property of a non-object. Isn't the query being returned from the model as an object?
You should use $query->result_array, row_array, result, or row otherwise your returning the object intead get the results. Check the CI manual.
You are returning the results as array and using $row as object!
Try:
foreach($query->result() as $row) {
Refer.
Try changing $this->load->model('login/configurations', '', TRUE); to $this->load->model('login/configurations', 'configurations', TRUE); and see if it works. If it does, it is related to how you're extending your model class. That is, it would be related to what name you give inside configurations.php.
Hope this helps.
Your undefined variable error tells me that your query might not be running correctly. To diagnose...enable the profiler to check your query.
From the documentation:
$this->output->enable_profiler();
Permits you to enable/disable the
Profiler, which will display benchmark
and other data at the bottom of your
pages for debugging and optimization
purposes.
To enable the profiler place the
following function anywhere within
your Controller functions:
$this->output->enable_profiler(TRUE);
When enabled a report will be
generated and inserted at the bottom
of your pages.
Your query will be shown at the end of
the page
Double check your query syntax to make sure it is running properly, and
your code in it's current state is returning an object of objects and arrays:
print_r($query)
CI_DB_mysql_result Object
(
[conn_id] => Resource id #29
[result_id] => Resource id #39
[result_array] => Array
(
)
[result_object] => Array
(
)
[current_row] => 0
[num_rows] => 3
[row_data] =>
)
you need to access the individual properties to get to the actual data.
$result=$query->result();
print_r($result);
should do it
Had this issue before - basic problem is that if the Query returns no result set then $query->result_array() == NULL
NULL is not a list and can't be processed in a foreach.
I have found that checking for this condition solves this issue.
From your question, you are getting the result as a pure array (result_array) but printing the data as object ($row->first).
result() or row() is returning in object but result_array is returning in array.
change your view part like,
foreach($query->result_array() as $row) {
echo $row['first'];
}
or if you want to use an object then,
foreach($query->result() as $row) {
echo $row->first;
}
Generating Query Results in Codeigniter

Resources