Select laravel 5.6 api 中的名字

Select by name in laravel 5.6 api

我的数据库中有很多广告,我想 select 一个来自主题的名称我尝试但我得到一个空 return

 public function index()
    {
        # code...
       // $Ads = ads::all();
     //  return $this->sendResponse($Ads->toArray(), 'Ads read succesfully');
        $column = 'name'; // This is the name of the column you wish to search

        $Ads = ads::where($column)->first();

        return response()->json(['success'=> true,'ads'=>$Ads, 'message'=> 'Ads read succesfully']);
    }

这就是我在 post 中得到的,伙计:

{ "success": true, "ads": null, "message": "Ads read succesfully" }

在开始之前有一些注意事项:

  1. 您需要有 Request 变量,以便您可以获取用户输入,或者如果它是静态的,则只提供静态的。但是,static 没有意义,所以我提供了一个将采用输入变量的代码。

  2. 您需要将该值与列名进行比较才能获取它。

  3. 模型的名称应该是单数形式并且以大写开头,因为它与 class 名称相同所以你应该使用 Ad 而不是 ads,ads 适用于 table名称,不是型号名称。

考虑到上述注意事项,以下是适合您的代码:

public function index(\Illuminate\Http\Request $request)
        {
            # code...
            $column = 'name'; // This is the name of the column you wish to search
            $columnValue = $request->input('name');// This is the value of the column you wish to search
            $Ads = Ad::where($column, $columnValue)->first();

            return response()->json(['success'=> true,'ads'=>$Ads, 'message'=> 'Ads read succesfully']);
        }