mercredi 30 décembre 2015

Add items to query result - Laravel

I'm slowly moving my API to Laravel and coming to grips with the 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.

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();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire