[英]Select by name in laravel 5.6 api
我的数据库中有很多广告,我想按名称尝试从主题中选择一个,但返回的结果为空
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']);
}
这就是我在邮递员中得到的:
{“ success”:true,“ ads”:null,“ message”:“广告读取成功”}
在深入研究之前,需要注意一些事项:
您需要具有Request变量,以便可以获取用户输入,或者如果它是静态的,则只需将其提供为静态。 但是,static没有意义,因此我提供了将使用输入变量的代码。
您需要将值与列名进行比较才能获取它。
模型名称应为单数形式,并以大写字母开头,与类名称相同,因此您应使用广告代替广告,广告适合表名,而不适合模型名。
考虑以上注意事项,以下代码将为您服务:
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']);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.