php 与Laravel语法错误混淆:意外的“.”(我觉得没问题)

2g32fytz  于 2023-02-03  发布在  PHP
关注(0)|答案(4)|浏览(111)

我刚到拉腊维尔。
我一直收到这个语法错误:

Symfony\Component\Debug\Exception\FatalThrowableError
syntax error, unexpected '.'

这句话是这样的:

$post = .\DB::table('posts')->where('slug', $slug)->first();

下面是无法正常工作的完整代码:

<?php

namespace App\Http\Controllers;

class PostsController extends Controller

{
  public function show($slug)
  {
    $post = .\DB::table('posts')->where('slug', $slug)->first();

    dd($post);

     // This was to simulate a database
     // $posts = [
     //   'my-first-post' => 'Hello This is my first blog post',
     //   'my-second-post' => 'Hello This is my second blog post',
     // ];

     // if it doesn't exist then throw this error:
     if (! array_key_exists($post, $posts)){
       abort(404, 'Sorry, that post was not found.');
     }

     // return this view:
      return view('post', [
        'post' => $posts[$post]
      ]);
  }

}

我到底错过了什么!?
谢谢

xnifntxz

xnifntxz1#

您不应该使用.\。请将您的代码$post = .\DB::table('posts')->where('slug', $slug)->first();更改为$post = DB::table('posts')->where('slug', $slug)->first();,并且不要忘记导入数据库use DB

ifsvaxew

ifsvaxew2#

您应该删除变量$post后面的./,因此将此行更改为

$post = DB::table('posts')->where('slug', $slug)->first();
ycl3bljg

ycl3bljg3#

像我一样,您显然已经学习了laracast教程。由于某种原因,正在使用的IDE使“\”看起来像一个."\“
下面是它在VS代码

中的样子
去掉句号你就没事了!

jgwigjjp

jgwigjjp4#

编辑代码以:

$post = DB::table('posts')->where('slug', $slug)->first();

并在顶部导入DB:

use Illuminate\Support\Facades\DB;

相关问题