Getting particular object

@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

Leave a Comment