Pankaj
Pankaj

Reputation: 10115

Issue in Laravel 5.1 Inner Join Query

Below is my Query in Laravel 5.1

\App\Models\Project\Bids\ProjectBid_Model
::selectRaw('B.*')
->join('tblproject P','B.projectid','=','P.projectid')
->where('P.WhoCreatedTheProject',14)
->first()

and below is the equivalant query

select B.* from `tblprojectbid` 
inner join `tblproject P` on `B`.`projectid` = `P`.`projectid` 
where `P`.`WhoCreatedTheProject` = 14 limit 1

What's the problem ?

Please check the line 1 in Query: select B.* from tblprojectbid.

What's the question ?

How can I change

select B.* from tblprojectbid

to

select B.* from tblprojectbid B

Upvotes: 3

Views: 227

Answers (3)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111879

If you want to use Eloquent I'm afraid there is no easy way to do it.

I use in this case full table name for model for instance

\App\Models\Project\Bids\ProjectBid_Model
::selectRaw('bid_table.*')
->join('tblproject AS P','bid_table.projectid','=','P.projectid')
->where('P.WhoCreatedTheProject',14)
->first()

However it's also possible that you set alias in ProjectBid_Model:

protected $table = 'bid_table AS B';

The con is you will have this table always aliased with B, so in case you have 2 models with same alias (in this case B), you won't be able to change it later just for one table, so I think the better is 1st approach (without using alias)

Upvotes: 2

Pankaj
Pankaj

Reputation: 10115

Here is the final solution.

\App\Models\Project\Bids\ProjectBid_Model
    ::selectRaw('B.*')
    ->from('tblprojectbid as B')
    ->join('tblproject as P','B.projectid','=','P.projectid')
    ->where('P.WhoCreatedTheProject',14)
    ->first()

Upvotes: 1

asolenzal
asolenzal

Reputation: 500

try this.

\DB::table('tblprojectbid as B')
->select()
->join('tblproject as P','B.projectid','=','P.projectid')
->where('P.WhoCreatedTheProject',14)
->first()

Upvotes: 0

Related Questions