如何解决语法错误,意外的'->'(T_OBJECT_OPERATOR)(0)

时间:2018-11-04 16:15:00

标签: php laravel-5.4

<tbody> 
@if(count($articles) > 0)
 @foreach(@articles->all() as $article)

<tr class="table-active">
  <th ></th>
  <td></td>
  <td></td>
  <td>
      <a herf="{{ url('')}}" ><button class="label label-primary">Read |</button></a>
      <a herf="{{ url('')}}" ><button class="label label-success"> Update |</button></a>
      <a herf="{{ url('')}}" ><button class="label label-danger">Delete |</button></a>
  </td>
</tr>
@endforeach
@endif

Contoller:

class CreatesController extends Controller
{
    public function home()
    {
        $articles =Article::all();
        return view('home',['article'=>$articles]);
    }
}

出现以下错误。

syntax error, unexpected '->' (T_OBJECT_OPERATOR) (View: C:\xampp\htdocs\laravelcrud\resources\views\home.blade.php)

2 个答案:

答案 0 :(得分:1)

使用以下语法返回视图

return view('home', compact('articles'));

,并将以下内容用于foreach

@foreach ( $articles as $article ) //your code here @endforeach

答案 1 :(得分:0)

替换

 @foreach(@articles->all() as $article)

使用

 @foreach($articles as $article)