Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel使用JSON和循环功能口若悬河_Php_Json_Laravel_Eloquent - Fatal编程技术网

Php Laravel使用JSON和循环功能口若悬河

Php Laravel使用JSON和循环功能口若悬河,php,json,laravel,eloquent,Php,Json,Laravel,Eloquent,我有新闻文章,我想将特定字段传递给JSON 我尝试用以下代码循环所有新闻: public function appNews() { $news = News::orderBy('id', 'desc')->get(); foreach($news as $newsOne) { $title = $newsOne->title; $body = $newsOne->body; $image = $newsOne->image;

我有新闻文章,我想将特定字段传递给JSON

我尝试用以下代码循环所有新闻:

public function appNews() {
  $news = News::orderBy('id', 'desc')->get();

  foreach($news as $newsOne) {
    $title = $newsOne->title;
    $body = $newsOne->body;
    $image = $newsOne->image;


    return response()->json([
      'title' => $title,
      'body' => $body,
      'image' => $image
    ]);

  }
}
public function appNews() {
  $news = News::orderBy('id', 'desc')->get();

  foreach($news as $newsOne) {
    $title = $newsOne->title;
    $body = strip_tags($newsOne->body);
    $image = $newsOne->image;

    echo '<div>' . $newsOne->title . '</div>';

    /*return response()->json([
      'title' => $title,
      'body' => $body,
      'image' => $image
    ]);*/

  }
}
但这会返回一个结果,而不是多个结果

如果我回显循环,我将使用以下代码获得所有需要的结果:

public function appNews() {
  $news = News::orderBy('id', 'desc')->get();

  foreach($news as $newsOne) {
    $title = $newsOne->title;
    $body = $newsOne->body;
    $image = $newsOne->image;


    return response()->json([
      'title' => $title,
      'body' => $body,
      'image' => $image
    ]);

  }
}
public function appNews() {
  $news = News::orderBy('id', 'desc')->get();

  foreach($news as $newsOne) {
    $title = $newsOne->title;
    $body = strip_tags($newsOne->body);
    $image = $newsOne->image;

    echo '<div>' . $newsOne->title . '</div>';

    /*return response()->json([
      'title' => $title,
      'body' => $body,
      'image' => $image
    ]);*/

  }
}
公共函数appNews(){
$news=news::orderBy('id','desc')->get();
foreach($news作为$newsOne){
$title=$newsOne->title;
$body=strip_标签($newsOne->body);
$image=$newsOne->image;
回显“.$newsOne->title.”;
/*返回响应()->json([
“title”=>$title,
“body”=>$body,
'image'=>$image
]);*/
}
}

如何使用response()->json()修复此问题,以便获得所有结果?

制作一个数组并附加它

public function appNews() {
  $news = News::orderBy('id', 'desc')->get();
  $array = [];
  foreach($news as $newsOne) {
    $title = $newsOne->title;
    $body = strip_tags($newsOne->body);
    $image = $newsOne->image;

    $array[] = [
      'title' => $title,
      'body' => $body,
      'image' => $image
    ];

  }

   return response()->json($array);
}

制作一个数组并附加它

public function appNews() {
  $news = News::orderBy('id', 'desc')->get();
  $array = [];
  foreach($news as $newsOne) {
    $title = $newsOne->title;
    $body = strip_tags($newsOne->body);
    $image = $newsOne->image;

    $array[] = [
      'title' => $title,
      'body' => $body,
      'image' => $image
    ];

  }

   return response()->json($array);
}

假设您的实际代码如下所示,最好的方法可能是:

// here you select only fields you need
$news = News::select('title', 'body', 'image')->orderBy('id', 'desc')->get();

// here you return changed content of body by striping tags
return response()->json($news->map(function($singleNews) {
   $singleNews->body = strip_tags($singleNews->body);
   return $singleNews;
})->all());

假设您的实际代码如下所示,最好的方法可能是:

// here you select only fields you need
$news = News::select('title', 'body', 'image')->orderBy('id', 'desc')->get();

// here you return changed content of body by striping tags
return response()->json($news->map(function($singleNews) {
   $singleNews->body = strip_tags($singleNews->body);
   return $singleNews;
})->all());