添加项目到查询结果 - Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34528178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:55:58  来源:igfitidea点击:

Add items to query result - Laravel

jsonlaravellaravel-5eloquentquery-builder

提问by moh_abk

I'm slowly moving my API to Laravel and coming to grips with the Query Builder.

我正在慢慢地将我的 API 转移到 Laravel 并开始使用Query Builder

I'm trying to achieve this:

我正在努力实现这一目标:

$data = array();

$query = "SELECT * FROM blog_posts WHERE post_type = 3 AND post_status = 1  ORDER BY id DESC";
$result = mysqli_query($cms_connection, $query);
if($result) {
    while($row = mysqli_fetch_assoc($result)) {
        $row['post_seo'] = seoUrl($row['post_title']);
        $data['data'][] = $row;
    }
    $data['success'] = true;
    $response = json_encode($data);
}

My problem isn't necessarily with getting the query, but as you can see I'm using the result of the query and then injecting it back into the final array.

我的问题不一定是获取查询,但正如您所看到的,我正在使用查询的结果,然后将其注入到最终的array.

So essentially, I'm fetching rows, transforming some of the attributes fetched, and then injecting the newly created attributes into the resulting array.

所以本质上,我正在获取行,转换一些获取的属性,然后将新创建的属性注入到结果数组中。

This is what I have so far:

这是我到目前为止:

$posts = DB::table('blog_posts')
    -where(['post_type' => 1, 'post_status' => 1)
    ->orderBy('id', 'desc')
    ->take(5)->get();

回答by Marcin Nabia?ek

You could do it this way

你可以这样做

// get your data (yours part of code)

// 获取您的数据(您的代码部分)

$posts = DB::table('blog_posts')
-where(['post_type' => 1, 'post_status' => 1])
->orderBy('id', 'desc')
->take(5)->get();

// add post_seo

// 添加 post_seo

foreach ($posts as $post) {
   $post->post_seo = seoUrl($post->post_title);
}

// set result array

// 设置结果数组

$data['data'] = $posts;
$data['success'] = true;

// response

// 回复

$response = response()->json($data);

// or in case you want to return it just

// 或者如果您只想返回它

return response()->json($data);

EDIT

编辑

You could do it also a bit better, using Eloquent. If you have such model (you need to add valid namespaces and usestatements)

你也可以做得更好,使用Eloquent. 如果你有这样的模型(你需要添加有效的命名空间和use语句)

class BlogModel extends Model 
{
   protected $table = 'blog_posts';
   protected $appends = ['post_seo'];

   public function getPostSeoAttribute($value)
   {
       return seoUrl($this->post_title);
   } 
}

(added accessor to post_seoattribute and added post_seoto results when converting to array)

(将访问器添加到post_seo属性并post_seo在转换为数组时添加到结果中)

You can now do (shorter syntax than in previous example):

你现在可以做(比前面的例子更短的语法):

// get your data

// 获取你的数据

$posts = BlogPost::where('post_type',1)
->where('post_status',1)
->orderBy('id', 'desc')
->take(5)->get();

// response

// 回复

$response = response()->json(['data' => $posts, 'success' => true]);