Solved: Getting particular object is a common problem faced by many developers. It can be difficult to find the right object when dealing with large datasets or complex data structures. Fortunately, there are a few techniques that can be used to help you find the object you are looking for. In this article, we will discuss some of the most common methods for locating a particular object, including using search algorithms, sorting algorithms, and data structures. We will also discuss the pros and cons of each approach and provide some tips for getting the most out of your search.
from json array in angularjs
You can use the angular.forEach() method to loop through the array and find the object you are looking for.
Example:
var myArray = [{name: ‘John’, age: 25}, {name: ‘Jane’, age: 30}, {name: ‘Bob’, age: 20}];
var myObject;
angular.forEach(myArray, function(value, key) {
if (value.name == ‘Jane’) {
myObject = value;
}
});
console.log(myObject); // {name: ‘Jane’, age: 30}
@kayal if you are still learning then this is good but sometime you should read the documentation
here is the method that you can do this easily
For Example
Assuming Table name test
+------------+
| id | data |
+------------+
| 1 | {...} |
+------------+
suppose you have Model Test.php
Then add a protected property $casts
it is an array in this array add key => value
key is column name and value will be in which type you want to cast like object or array in this case i am using object.
namespace App;
class Test extends Model
{
protected $casts = ['data' => 'object']; // add this property
in your controller
in this case i am using TestController.php
namespace App\Http\Controllers;
use App\Test;
class TestController extends Controller
{
public function seeUserId()
{
$test = Test::find(1); // or your query
$user_id = $test->data->thread->user_id; // you can access like this this is only example
}
public function saveData($data)
{
$test = new Test;
$test->data = $data; // don't use json_encode() just pass any object or array ;
$test->save();
}
laravel doc :- https://laravel.com/docs/5.5/eloquent-mutators#array-and-json-casting