Laravel将对象检测为数组:试图获取非对象的属性“ title”

时间:2019-12-03 07:04:07

标签: php laravel laravel-5 session-state

将检索到的数据传递到视图(using redirect()->back()-with())时遇到问题

我的控制器中有一个方法,在检索到数据之后,我创建了一个名为$data的新变量,并将其发送到视图中,就像这样

$message = Message::find($request->id);

$data    = [
    "{$do}Modal"   => "{$do}Modal",
    'msg'           => $message
];

return redirect()->back()->with($data);

然后,在我看来,我需要为数据$msg启动新变量。但是我遇到一个错误,说我想显示该变量时是一个数组。

@php
    $msg = Session::get('msg')
@endphp

{{ is_object($msg) }} // it shows 1 ($msg is an object)
{{ $msg->id }} // the error message shown up
{{ $msg['id'] }} // shows the id

我的第一个意图是将ID,标题等显示为对象,但是我只能将值显示为数组。 当我做var_dump($msg)时,得到了以下输出:

            object(App\Models\Message)#200 (26) {
  ["fillable":protected]=>
  array(4) {
    [0]=>
    string(5) "title"
    [1]=>
    string(4) "body"
    [2]=>
    string(8) "password"
    [3]=>
    string(5) "image"
  }
  ["connection":protected]=>
  string(5) "mysql"
  ["table":protected]=>
  string(8) "messages"
  ["primaryKey":protected]=>
  string(2) "id"
  ["keyType":protected]=>
  string(3) "int"
  ["incrementing"]=>
  bool(true)
  ["with":protected]=>
  array(0) {
  }
  ["withCount":protected]=>
  array(0) {
  }
  ["perPage":protected]=>
  int(15)
  ["exists"]=>
  bool(true)
  ["wasRecentlyCreated"]=>
  bool(false)
  ["attributes":protected]=>
  array(7) {
    ["id"]=>
    int(101)
    ["title"]=>
    string(19) "Tes ketiga hari ini"
    ["body"]=>
    string(18) "sfkafaf
afaf
afa"
    ["created_at"]=>
    string(19) "2019-12-03 11:20:56"
    ["updated_at"]=>
    string(19) "2019-12-03 11:39:29"
    ["password"]=>
    string(60) "$2y$10$hSTdJZN24aIddOjrGMmUceYN1cPFvLM.IjCcEL/BPb/4C6ayk12va"
    ["image"]=>
    string(91) "kala-gotongan-dan-semut-sadulur-pantang-kubur-jenazah-ini-alasannya_m_39626_1575343256.jpeg"
  }
  ["original":protected]=>
  array(7) {
    ["id"]=>
    int(101)
    ["title"]=>
    string(19) "Tes ketiga hari ini"
    ["body"]=>
    string(18) "sfkafaf
afaf
afa"
    ["created_at"]=>
    string(19) "2019-12-03 11:20:56"
    ["updated_at"]=>
    string(19) "2019-12-03 11:39:29"
    ["password"]=>
    string(60) "$2y$10$hSTdJZN24aIddOjrGMmUceYN1cPFvLM.IjCcEL/BPb/4C6ayk12va"
    ["image"]=>
    string(91) "kala-gotongan-dan-semut-sadulur-pantang-kubur-jenazah-ini-alasannya_m_39626_1575343256.jpeg"
  }
  ["changes":protected]=>
  array(0) {
  }
  ["casts":protected]=>
  array(0) {
  }
  ["dates":protected]=>
  array(0) {
  }
  ["dateFormat":protected]=>
  NULL
  ["appends":protected]=>
  array(0) {
  }
  ["dispatchesEvents":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["relations":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["timestamps"]=>
  bool(true)
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
}

1 个答案:

答案 0 :(得分:1)

您好,我创建了相同的代码,所以如果您执行以下操作:

    $message=Test::find(1);

    $data    = [
        "Modal"   => "Modal",
        'msg'           => $message
    ];

    return redirect()->back()->with($data);

在dd($ msg)之后的视图中,您应该看到: enter image description here

您可以编辑帖子并显示为dd返回代码吗?

编辑:因此应该可以使用

$msg = Session::get('msg');
dd($msg->title);