0

I am bit struggling to convert below given SQL query to eloquent.

Here is an example of what I am trying to do -

SELECT u.wallet_address AS wallet_address, p.id AS project_id
FROM users u, projects p, investment_investor i
WHERE u.id = i.user_id AND 
      p.id=i.project_id AND 
      u.wallet_address <> '' AND 
      p.wallet_address <> '' AND
      p.contract_address <> ''
GROUP BY u.id, p.id;

The below SQL query gives me the expected output, which I am able to build with the Laravel DB query builder. But want to standardise it to Laravel framework.

Your help is much appreciated!

2 Answers 2

1

First thing you need to have to be able to write queries in the laravel way is something called Model. Example of a model would be like so

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    //
}

You can read more about it here

Once you create it, you can start writing queries. For example

$results = Users::selectRaw('users.wallet_address as `wallet_address`,'.
                 'id as `project_id`')
                ->get();

You can read more about joins and other things here

Sign up to request clarification or add additional context in comments.

1 Comment

I already have my DB query builder snippet running well. I am looking for a way to write eloquent query for the above given example.
0
 $result=DB::table('investment_investor')
    ->select('users.wallet_address as wallet_address','projects.id AS project_id')
    ->join('users','users.id','=','investment_investor.user_id')
    ->join('projects','projects.id','=','investment_investor.project_id')
    ->where('users.wallet_address','!=','')
    ->where('projects.wallet_address','!=','')
    ->where('projects.contract_address','!=,'')
    ->groupBy('users.id', 'projects.id')
    ->get();

1 Comment

Thanks @jithesh, the same query I already have.. It is with DB builder, which I am not looking for.. I am looking for Model relations eloquent query.

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.