【问题标题】:Joins in Laravel and showing data in a view在 Laravel 中加入并在视图中显示数据
【发布时间】:2016-06-28 19:33:47
【问题描述】:

如何在视图的连接中使用表的别名。我编写了以下连接查询

$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

在拉拉维尔 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');
}

}

【问题讨论】:

  • 您也可以发布您的数据库表吗?

标签: php laravel


【解决方案1】:

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

【讨论】:

  • 未定义属性:stdClass::$companyName(查看:C:\xampp\htdocs\bridging_the_gap\resources\views\adminspanel\admin\assigntrainer.blade.php)我尝试后收到此错误你的代码
  • 您是否已将&lt;your other selects&gt; 替换为您选择的其他属性?
  • 我没有其他选择的属性
  • 非常感谢先生,我解决了问题,非常感谢先生
  • 不,jobs_users 中不存在它。它存在于工作表中
猜你喜欢
  • 1970-01-01
  • 2021-07-18
  • 2017-07-31
  • 2016-09-09
  • 2023-01-08
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 2021-06-01
相关资源
最近更新 更多