【问题标题】:Property 'id' is being taken from a non-object属性“id”取自非对象
【发布时间】:2020-10-09 02:39:02
【问题描述】:

我是 Laravel 和 MacOS 的新手,我正在使用 Laravel 7。在尝试开发新闻网站时,我创建了一个按钮来“编辑”用户创建的新闻,但出现了以下错误:

试图获取非对象的属性“id”(查看:/Users/rodri6uez/Documents/LARAVEL/noticias/resources/views/noticias/edit.blade.php)

错误指向下一行代码:

<form action="{{ route('noticias.update',['id' => $noticia ?? '' ->id]) }}" method="post" enctype="multipart/form-data">

这是影响编辑功能的控制器代码:

public function edit($noticia)
{
    $user = auth()->user();
    $noticia = DB::table('noticias')
        ->join('categorias', 'noticias.categorias_id', '=', 'categorias.id')
        ->join('users', 'noticias.users_id', '=', 'users.id')
        ->where('noticias.users_id', $user->id)
        ->where('noticias.id', $noticia)
        ->select('noticias.*', 'categorias.descripcion as categoriadescripcion', 'users.name as usuario')
        ->first();

    $categorias = categoria::where('estatus', 1)
        ->orderBy('descripcion', 'desc')
        ->get();
    return view('noticias.edit', ['id' => $noticia, 'categorias' => $categorias]);
}

我不知道这是否足够的信息,如果不是,请告诉我发布它。

非常感谢!

【问题讨论】:

  • 您将$noticia 传递给视图并将其命名为id,因此在视图中您应该使用route('noticias.update',['id' =&gt; $id-&gt;id]),尽管我会考虑重命名变量。

标签: php mysql laravel macos


【解决方案1】:

欢迎使用 stackoverflow。

$noticia 中,您正在获取对象值。所以按照下面的代码-

<form action="{{ route('noticias.update',['id' => $noticia ?? '' $noticia->id]) }}"
 method="post" enctype="multipart/form-data">

希望这会对您有所帮助。编码愉快。

【讨论】:

  • 感谢您的欢迎!我这样做了,现在它告诉我:语法错误,意外的 '$noticia' (T_VARIABLE),期待 ']'
  • 试试['id' =&gt; $noticia-&gt;id]
  • 这样说: $noticia 未定义 使变量在刀片模板中是可选的。将 {{ $noticia }} 替换为 {{ $noticia ?? '' }}
  • 你能 dd($noticia) 吗?
  • 还是不行。我不知道发生了什么,但是,非常感谢,我真的很感激!
【解决方案2】:

解决办法是:

<form action="{{ route('noticias.update',['id' => $noticia ?? '' $noticia->id]) }}"
 method="post" enctype="multipart/form-data">

【讨论】:

    猜你喜欢
    • 2019-04-30
    • 1970-01-01
    • 2021-05-26
    • 2019-03-10
    • 1970-01-01
    • 2013-07-24
    • 2020-06-26
    • 2018-07-03
    • 1970-01-01
    相关资源
    最近更新 更多