如何在视图连接中使用表使用的别名。我编写了以下连接查询
$show Data = DB::table('jobs_users')
->join('jobs', 'jobs_users.job_id', '=', 'jobs.id')
->join('users as u1', 'jobs_users.user_id', '=', 'u1.id')
->join('users as t1', 'jobs_users.trainer_id', '=', 't1.id')
->get();
我有两个表Jobs和users,它们的id存储在一个数据透视表中。在用户表中,我有用户培训师和用户学生。现在我想在视图中显示工作,学生和培训师的名称。我得到了工作的名字,培训师,但没有得到学生的名字。在视图中,我使用连接结果如下
@for each ($show Data as $u)
<TD>
{{$u->company Name}} // job name
</TD>
<TD>
{{$u ->user Name}} //trainer name
</TD>
<TD>
{{$u->user Name}}</TD> // student name
</tr>
@end for each
在La ravel 5.2用户表
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('user_id')->unsigned()->null able();
$table->integer('user_type_id')->unsigned();
$table->en um('account Type',['Fresh', 'Professional'])->null able();
$table->string('user Name',30);
$table->string('email', 30)->unique();
$table->string('password',64);
$table->date('dob',30)->null able();
$table->en um('gender',['Male', 'Female']);
$table->string('country',30)->null able();
$table->string('city',15)->null able();
$table->string('mobile No', 15)->null able();//+92 42 5689896
$table->string('c n i c',60)->null able();
$table->string('address',512)->null able();
$table->string('degree Level',30)->null able();
$table->string('degree Title',30)->null able();
$table->string('institution',60)->null able();
$table->string('degree Country',60)->null able();
$table->string('degree City',60)->null able();
$table->string('experience')->null able();;
$table->unsigned Small Integer('work Experience')->null able();
$table->string('industry', 60)->null able();
$table->string('aced Country',30)->null able();
$table->string('c v',30)->null able();
$table->remember Token();
$table->time stamps();
$table->soft Deletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
使用select()
:
$showData = DB::table('jobs_users')
->join('jobs', 'jobs_users.job_id', '=', 'jobs.id')
->join('users as u1', 'jobs_users.user_id', '=', 'u1.id')
->join('users as t1', 'jobs_users.trainer_id', '=', 't1.id')
->select(<your other selects>, 'u1.name as u_name', 't1.name as t_name') //<- add your other selects
->get();
并显示在你的视图中:
<td>
{{$u->u_name}} <!--trainer name-->
</td>
<td>
{{$u->t_name}} <!--student name-->
</td>