1

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');
}
1
  • Why are you doing json_encode on $summary ? That will generate a string. Commented Jun 4, 2017 at 7:09

2 Answers 2

1

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
        ]);
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I'm getting an error of Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in Project_Folder\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 681 and defined
Share your InteractionSummary model and table schema.
I was taking comment as feedback that I changed even though same error is coming.
@NitishKumar i don't see anything wrong apart from the feedback field name. Anyway all saveMany() does under the hood is loop through the models and saves them individually. Try the code i've posted in the updated answer.
But this will run query multiple times. I was trying to implement in one query.
|
0

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']
    ]);
}

1 Comment

I'm getting an error of Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in Project_Folder\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 681 and defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.