加入Laravel并在视图中显示数据

时间:2016-06-28 11:00:01

标签: php laravel

如何在View.I中的连接中使用表的别名来编写以下连接查询

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

我有两个表作业和用户,他们的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');
}

}

1 个答案:

答案 0 :(得分:2)

使用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>