【问题标题】:Left join Query with eloquent左连接 Query with eloquent
【发布时间】:2013-08-13 22:22:18
【问题描述】:

我有两个课程:学位和大学,

class Degree extends Eloquent {
      public function title(){
         return $this->degree_title;
      }
      public function college(){
         return $this->belongsTo('College','college_code');
      }
}
class College extends Eloquent {
      public function title(){
         return $this->college_title;
      }
}

我将它设置为belongsTo,因为它在hasOne()上中断,错误是:

Call to a member function getResults() on a non-object

如果这是在正确的方向,我不确定。

我一直在尝试的是使用 slug 搜索学位(它有助于更​​好的分析),然后将结果传递给我创建的刀片。

注意:有一个 Degrees 表和一个 Colleges 表,我可以在没有连接的情况下运行查询:

学位控制器:

class DegreeController extends BaseController {
    public function single($slug)
       {
            $degree = Degree::where('slug', '=', $slug)->get();
            return View::make('degrees.single', array('degree' => $degree));
       }
}

单一视图:

@extends('master')
{{ -- Single Degree Blade-- }}
@section('content')

@foreach($major as $m)
    <div>
        <h1>{{ $m->title() }}</p>
        <p>{{ $m->college->title() }}</p>
        <p>Holland: {{ $m->holland() }}</p>
    </div>
@endforeach

@stop

我注意到的是使用$m-&gt;college-&gt;title() 会导致错误: SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列 'colleges.id'(SQL:select * from colleges where colleges.id = ?limit 1)(绑定:数组(0 => 'UAGSC', ))

这表明它忽略了我的自定义键,它不是 id 也不是整数,它是一个特定的 5 字母 varchar。

任何人都可以就如何处理这个问题给我任何建议吗?

【问题讨论】:

    标签: join laravel laravel-4


    【解决方案1】:

    加入时,使用主键。主要是身份证。

    将主键设为 INT 和 AUTO INCREMENT。

    你会成功的。

    【讨论】:

      【解决方案2】:

      我通过将 College 表的主键 (ID) 更改为与 college_code 引用的值相同的值来解决此问题。 ID 不是自动递增或整数,我使用的是已经唯一的标识符,从长远来看,这对我来说更容易。

      【讨论】:

        猜你喜欢
        • 2015-06-15
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 2019-04-20
        相关资源
        最近更新 更多