1

I am trying to concatinate 2 tables for the blog using laravel, however i can't seem to get it working and i have no clue as to why..

if i use selectRaw i can see the concat but i lose all the blog content and i only see the author and nothing else.

I have tried to switch to raw and if i do the concat doesn't work but everything else works.

Here is the php code i am using:

 $app->get('/', function() use ($app) {

    $blog = $app->blog->selectRaw('CONCAT(users.first_name, " ", users.last_name) as author')->join('users', 'blog.user_id', '=', 'users.id')->get();

    $app->render('home.php', [
        'blog' => $blog
    ]);
})->name('home');

1 Answer 1

0

You can use the Eloquent join function to achieve this.

For example

DB::table('blog')
            ->join('users', 'blog.user_id', '=', 'users.id')
            ->select('blog.*', 'CONCAT(users.first_name, " ", users.last_name) as author')
            ->get();
Sign up to request clarification or add additional context in comments.

3 Comments

well i did give it a try but that doesn't work, since class DB is not found
Try putting a `\` in front of it. See details stackoverflow.com/a/25787734/170539
hold on let me edit the code section, thats how i currently have it since the other options don't work for this case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.