0

I'm new to Laravel. And i've done researches but i can't seem to find the right answer for my code.

So i have this controller

namespace Modules\Inventory\Http\Controllers;

use Modules\Setup\Init;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;

use Modules\Inventory\Entities\mongodb_logs as mongodb_logs;

use Modules\Template;

class ActivityLogsController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    protected $data;
    protected $page_title = 'Login';

    function setup($vars = null)
    {
        $Init = new Init;
        $vars['page'] = $this->page_title;
        $this->data['template'] = $Init->setup($vars);
        return $this->data;
    }

    public function index()
    {
        $loglists = mongodb_logs::orderBy('date_added', 'desc')->get();
        $this->data['loglist'] = $loglists;

        return view('inventory::_logs', $this->setup());
    }
}

and my view was just this simple

<table id="tbl_logs" class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <td>Type</td>
            <td>User</td>
            <td>Date</td>
            <td>Message</td>
            <td>Before</td>
            <td>After</td>
        </tr>
    </thead>
    <tbody>
        @if($loglist != null)
            @foreach($loglist as $logs)
                <tr>
                    <td>{{$logs->type}}</td>
                    <td>{{$logs->logged_in_user}}</td>
                    <td>{{$logs->date_added}}</td>
                    <td>{{$logs->message}}</td>
                    <td>{{$logs->before}}</td>
                    <td>{{$logs->after}}</td>
                </tr>
             @endforeach
         @endif
    </tbody>
</table>

I retrieve the loglist from my mongodb collection 'logs'. I successfully retrieved the data you can try it via 'print_r' but when i view it on my view, this is the error that shows

htmlspecialchars() expects parameter 1 to be string, array given

this is the print_r result of my $loglist which i successfulyl retrieved from mongodb print_r result

here's a few lines of my var_dump.

object(Illuminate\Database\Eloquent\Collection)#408 (1) {
  ["items":protected]=>
  array(40) {
    [0]=>
    object(Modules\Inventory\Entities\mongodb_logs)#409 (26) {
      ["connection":protected]=>
      string(7) "mongodb"
      ["collection":protected]=>
      string(4) "logs"
      ["primaryKey":protected]=>
      string(3) "_id"
      ["parentRelation":protected]=>
      NULL
      ["table":protected]=>
      NULL
      ["keyType":protected]=>
      string(3) "int"
      ["incrementing"]=>
      bool(true)
      ["with":protected]=>
      array(0) {
      }
      ["perPage":protected]=>
      int(15)
      ["exists"]=>
      bool(true)
      ["wasRecentlyCreated"]=>
      bool(false)
      ["attributes":protected]=>
      array(11) {
        ["_id"]=>
        object(MongoDB\BSON\ObjectID)#288 (1) {
          ["oid"]=>
          string(24) "58acfb2b202f941b50002ddf"
        }
        ["user_id"]=>
        int(2)
        ["admin_id"]=>
        int(2)
        ["logged_in_user"]=>
        string(5) "admin"
        ["date_added"]=>
        string(19) "2017-02-22 02:44:59"
        ["type"]=>
        string(6) "update"
        ["message"]=>
        string(69) "Deceased Documents ofAniyah Gottlieb ID number 4          successfully updated"
        ["before"]=>
        string(188) "    {"id":2,"d_id":4,"death_cert_no":null,"trans_permit_no":null,"crem_permit_no":nu    ll,"exhum_permit_no":null,"senior_card_no":null,"disabled_card_no":null,"created    _at":null,"updated_at":null}"
        ["after"]=>
        string(211) "        {"id":2,"d_id":4,"death_cert_no":"20171542","trans_permit_no":null,"crem_permit_    no":null,"exhum_permit_no":null,"senior_card_no":null,"disabled_card_no":null,"c    reated_at":null,"updated_at":"2017-02-22 02:44:59"}"
        ["updated_at"]=>
        object(MongoDB\BSON\UTCDateTime)#287 (1) {
          ["milliseconds"]=>
      string(10) "1672815231"
    }
    ["created_at"]=>
    object(MongoDB\BSON\UTCDateTime)#286 (1) {
      ["milliseconds"]=>
      string(10) "1672815231"
    }
  }
  ["original":protected]=>
  array(11) {
    ["_id"]=>
    object(MongoDB\BSON\ObjectID)#288 (1) {
      ["oid"]=>
      string(24) "58acfb2b202f941b50002ddf"
    }
    ["user_id"]=>
    int(2)
    ["admin_id"]=>
    int(2)
    ["logged_in_user"]=>
    string(5) "admin"
    ["date_added"]=>
    string(19) "2017-02-22 02:44:59"
    ["type"]=>
    string(6) "update"
    ["message"]=>
    string(69) "Deceased Documents ofAniyah Gottlieb ID number 4 successfully updated"
    ["before"]=>
    string(188) "{"id":2,"d_id":4,"death_cert_no":null,"trans_permit_no":null,"crem_permit_no":null,"exhum_permit_no":null,"senior_card_no":null,"disabled_card_no":null,"created_at":null,"updated_at":null}"
    ["after"]=>
    string(211) "{"id":2,"d_id":4,"death_cert_no":"20171542","trans_permit_no":null,"crem_permit_no":null,"exhum_permit_no":null,"senior_card_no":null,"disabled_card_no":null,"created_at":null,"updated_at":"2017-02-22 02:44:59"}"
    ["updated_at"]=>
    object(MongoDB\BSON\UTCDateTime)#287 (1) {
      ["milliseconds"]=>
      string(10) "1672815231"
    }
    ["created_at"]=>
    object(MongoDB\BSON\UTCDateTime)#286 (1) {
      ["milliseconds"]=>
      string(10) "1672815231"
    }
  }
13
  • on which line ? replace your if condition @if(!empty($loglist)) Commented Feb 22, 2017 at 5:24
  • Show us your printed array... Commented Feb 22, 2017 at 5:29
  • Hi @rahul_m , there's no specific line specified. the error just said ErrorException in helpers.php line 532: htmlspecialchars() expects parameter 1 to be string, array given (View: C:\laragon\www\template\Modules\Inventory\Resources\views_logs.blade.php) I tried your suggestion but i still get the error Commented Feb 22, 2017 at 5:30
  • if you remove that foreach block is it working ? Commented Feb 22, 2017 at 5:32
  • firstly you print your $loglist array on view Commented Feb 22, 2017 at 5:34

2 Answers 2

0

Your before and after variables are array that's why you getting this error. Try this code:

<tbody>
        @if($loglist != null)
            @foreach($loglist as $logs)
                <tr>
                    <td>{{$logs->type}}</td>
                    <td>{{$logs->logged_in_user}}</td>
                    <td>{{$logs->date_added}}</td>
                    <td>{{$logs->message}}</td>
                    @foreach($logs->before as $before)
                    <td>{{$before->whatStringYouWant}}</td>
                    @endforeach
                    @foreach($logs->after as $after)
                    <td>{{$after->whatStringYouWant}}</td>
                    @endforeach
                </tr>
             @endforeach
         @endif
    </tbody>
Sign up to request clarification or add additional context in comments.

5 Comments

oh! my before and after is json_encoded. But even if i remove them from the table view, i still get the error
Ok it seems before and after is not problem. Let's see other than this. What is the $this->data['template'] = $Init->setup($vars)
I'm doing all of these in a specific module where i use a template for blade view. The values from the controller is recognized to the view via '$this->data['name_in_view'] = name_in_controller', and all of the '$this->data' is contained inside '$this->setup()' for easier transfer of values bet. controller and views.
I think your array is multiple so please look inside foreach and pint the $logs.
Thank you, i'll check on it when i get home and reply back the result :)
0

One or more of the returned object properties is array. try to var_dump($logs)


I can't comment yet

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.