【问题标题】:Syntax error: unexpected '->' in call for destroy method语法错误:调用销毁方法时出现意外的“->”
【发布时间】:2019-07-10 09:02:21
【问题描述】:

在我的 laravel 应用程序中,index.blade.php 和 edit.blade.php 不会显示,因为我调用了“destroy”和“update”方法。

老实说,不知道该怎么做。看了其他例子,看不出有什么不同。

index.blade.php(有问题的那一行是表格):

@extends('layouts.app')
@section('content')
<h1>This is your task list</h1>
<ul>
@foreach($tasks as $task)
<li>
    <a style ="padding-right:5%" href = "{{route('tasks.edit',$task->id)}}">{{$task->title}} </a>

    <form method = 'POST' action ="{{action('TaskController@destroy', @task->id)}}">
@csrf
@method('DELETE')
<div class = "form-group">
<input type = "submit" class= "form-control" name = "submit" value = "Delete">
</div>
</form>
</li>

@endforeach
</ul>
<a href = "{{route('tasks.create')}}">Add a new Todo </a>
@endsection

edit.blade.php(这里也是,问题出在表格行):

@extends('layouts.app')
@section('content')
<h1>Edit Existing Task</h1>
<form method = 'POST' action = "{{action('TaskController@update' , @task->id)}}">
@method('PATCH')
@csrf
<div class = "form-group">
<label for = "title">Task to edit:</label>
<input type = "text" class= "form-control" name = "title" value = "{{$task->title}}">
</div>

<div class = "form-group">
<input type = "submit" class= "form-control" name = "submit" value = "Save">
</div>
</form>

语法错误,意外 '->' (T_OBJECT_OPERATOR),期望 ')'(视图:C:\xampp\htdocs\task\resources\views\tasks\index.blade.php)

【问题讨论】:

  • @task?也许$task

标签: php laravel


【解决方案1】:

在您的两个刀片文件中,您都使用了@task 而不是$task,因此请更改此内容

<form method = 'POST' action = "{{action('TaskController@update' , @task->id)}}">

<form method = 'POST' action = "{{action('TaskController@update' , $task->id)}}">

【讨论】:

  • 是的,就是这样。
  • 很好 :) 甚至没有注意到那个错字。
  • 是的。它有时会发生。我也经历过很多次同样的事情:p
【解决方案2】:

&lt;form&gt; 的操作上传递的变量需要是$task-&gt;id 而不是@task-&gt;id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多