Nitish Kumar
Nitish Kumar

Reputation: 6276

How to fetch array data in json or get property of data in laravel

I'm trying to retrieve data from request, so I'm placing $data = $request->only('user_id', 'clientParticipants', 'event_type','contactParticipants', 'schedule', 'stellarParticipants', 'summaries', 'type', 'venue', 'with_client') the following is the format when I did dd($data) in Laravel:

array:10 [
  "user_id" => 1
  "clientParticipants" => array:2 [
    0 => array:2 [
      "label" => "Check 2 Contact - Test Company 5"
      "value" => 4
    ]
    1 => array:2 [
      "label" => "Ammy Contact - Test Company 6"
      "value" => 5
    ]
  ]
  "event_type" => 2
  "contactParticipants" => array:3 [
    0 => array:2 [
      "label" => "Check Contact - Test Company 3"
      "value" => 2
    ]
    1 => array:2 [
      "label" => "Check 1 Contact - Test Company 2"
      "value" => 3
    ]
    2 => array:2 [
      "label" => "Check 4 contact - Test Company 8"
      "value" => 6
    ]
  ]
  "schedule" => "2017-06-04 05:02:12"
  "stellarParticipants" => array:1 [
    0 => array:2 [
      "label" => "Analyst"
      "value" => 1
    ]
  ]
  "summaries" => array:2 [
    0 => array:5 [
     "client" => array:2 [
        "label" => "Test Company 4"
        "value" => 7
      ]
      "type" => "1"
      "mention" => array:2 [
        "label" => "Analyst"
        "value" => 1
      ]
      "action" => "Action Test 1"
      "comment" => "Comment Test 1"
    ]
    1 => array:5 [
      "client" => array:2 [
        "label" => "Test Company 5"
        "value" => 8
      ]
      "type" => "1"
      "mention" => array:2 [
        "label" => "Analyst"
        "value" => 1
      ]
      "action" => "Action Test 2"
      "comment" => "Comment Test 2"
    ]
  ]
  "type" => "Meeting"
  "venue" => "Mumbai"
  "with_client" => "0"
]

I want to retrieve summaries so for this I'm trying to do following:

if($data['summaries'])
{
    $container = [];
    foreach($data['summaries'] as $summary)
    {
        $summary = json_encode($summary);
        $user_id = $summary->mention['value'];
        $container[] = new InteractionSummary([
            'company_id' => $summary->client,
            'nature' => $summary->type,
            'user_id' => $user_id,
            'action' => $summary->action,
            'feedback' => $summary->comment
        ]);
    }
    $interaction->meetingSummaries()->saveMany($container);
}

I'm getting error while retrieving any data in summaries:

Trying to get property of non-object

Update My InteractionSummary model is:

class InteractionSummary extends Model
{
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'company_id', 'interaction_id', 'nature', 'user_id', 'action', 'feedback'
    ];

    /**

     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [

    ];

    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
    ];
}

Database schema:

public function up()
{
    Schema::create('interaction_summaries', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('company_id')->index();
        $table->integer('interaction_id')->index();
        $table->string('nature');
        $table->integer('user_id')->nullable();
        $table->string('action')->nullable();
        $table->string('feedback')->nullable();
        $table->softDeletes();
        $table->timestamps();
    });
}

And Interaction have following relationship:

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function meetingSummaries()
{
    return $this->hasMany('App\InteractionSummary');
}

Upvotes: 1

Views: 1858

Answers (2)

Sandeesh
Sandeesh

Reputation: 11916

You're converting the array to a json string by using json_encode. Judging from your code you probably meant to convert the array to an object.

Change

$summary = json_encode($summary);

To

$summary = (Object)$summary;

Also $summary->client returns an array. You probably meant to use $summary->client['value'].

Edit : Try this

if($data['summaries']) {
    foreach($data as $summary) {
        $summary = (Object)$summary;

        $interaction->meetingSummaries()->create([
            'company_id' => $summary->client['value'],
            'user_id' => $summary->mention['value'],
            'nature' => $summary->type,
            'action' => $summary->action,
            'feedback' => $summary->comment
        ]);
    }
}

Upvotes: 1

kopaty4
kopaty4

Reputation: 2296

The error is that json_encode() function always returns a string, and you are trying to access it as an object.

Moreover, the entire $data variable is already an array, so you do not need to implement any JSON-related actions with its elements.

Just work with it like a normal array:

foreach($data['summaries'] as $summary)
{
    $container[] = new InteractionSummary([
        'company_id' => $summary['client']['value'],
        'nature' => $summary['type'],
        'user_id' => $summary['mention']['value'],
        'action' => $summary['action'],
        'comment' => $summary['comment']
    ]);
}

Upvotes: 0

Related Questions