关系表上的雄辩查询

时间:2017-09-18 13:09:03

标签: php laravel laravel-5.4

你好我有两个模特地址 Kontokorrent 他们有一对一的关系。我想创建一个搜索功能并在地址 Kontokorrent

之后搜索

我的模特:

地址模型

class Adress extends Model
{
    protected $table = "Adressen";
    protected $primaryKey = 'Adresse';
    public $timestamps = false;

    public function scopeSearchAdress($query, $searchTerm)
    {
        return $query->where('LieferOrt', 'LIKE', '%'.$searchTerm.'%')
                     ->orWhere('Name1', 'LIKE', '%'.$searchTerm.'%')
                     ->orWhere('LieferStrasse', 'LIKE', '%'.$searchTerm.'%');              
    }

    public function kontokorrent()
    {
        return $this->hasOne(Kontokorrent::class, 'Adresse');
    }
}

Kontokorrent模型

class Kontokorrent extends Model
{
    protected $table = "Kontokorrent";
    protected $primaryKey = 'Adresse';
    public $timestamps = false;

    public function scopeSearchKto($query, $searchTerm)
    {
        return $query->where('Kto', 'LIKE', '%'.$searchTerm.'%');              
    }

    public function adress()
    { 
        return $this->belongsTo(Adress::class, 'Adresse');
    }
}

控制器

class AdressesController extends Controller
{
    public function index(Request $request)
    {
        $searchTerm = $request->input('searchTerm');

        $adresses = Adress::whereHas('kontokorrent', function($query) use($searchTerm) {
            $query->where('KtoArt', 'D')
                  ->searchKto('K75688'); // this is working
                  // -> searchKto($searchTerm); this is NOT working       
        })->orderBy('Name1')
        ->searchAdress($searchTerm)
        ->paginate(60);

        return view('adresse.index', compact('adresses', 'searchTerm'));
    }
}

地址模型中的搜索功能正常运行。现在我想在 Kontokorrent 中搜索客户ID(Kto)。所以我在 Kontokorrent 中创建了一个范围,并将其链接到控制器搜索功能。

->searchKto('K75688') ....这是有效的我得到一个结果
->searchKto($searchTerm) ....当我在输入字段中输入客户ID并点击回车时,这不起作用。我得到一个0行表。

查看

@section('content')

<div class="row">
    <div class="col-xs-12 col-md-12">
        <form role="search" method="GET" action="/adresses">
            <div class="input-group">
                <input type="text" class="form-control" name="searchTerm" value="{{ isset($searchTerm) ? $searchTerm : '' }}" placeholder="Suche nach Name, Straße, Ort">
                <div class="input-group-btn">
                    <button type="submit" class="btn btn-search btn-default">
                       Suchen...
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="row">
    <div class="col-xs-12 col-md-12">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Name</th>
                    <th>Straße</th>
                    <th>Ort</th>
                    <th>PLZ</th>
                    <th>Land</th>
                </tr>
            </thead>
            <tbody>
                @if(count($adresses) > 0)
                    @foreach($adresses as $adress)
                        <tr> 
                            <th scope="row">{{ $loop->iteration }}</th>   
                            <td>
                                <a href="/adresses/{{$adress->Adresse}}">{{$adress->Anrede}} {{ $adress->Name1}}</a>
                                @if($adress->kontokorrent)
                                <small>{{ $adress->kontokorrent->Kto }}</small>
                                @endif
                            </td>
                            <td>{{ $adress->LieferStrasse }}</td>
                            <td>{{ $adress->LieferOrt }}</td>
                            <td>{{ $adress->LieferPLZ }}</td>
                            <td>{{ $adress->LieferLand }}</td>
                        </tr>
                    @endforeach
                @else
                    <tr><td colspan="6" class="text-center">Kein Eintrag gefunden.</td></tr>
                @endif
            </tbody>
        </table>
    </div>
</div>
{{ $adresses->appends(['searchTerm' => $searchTerm])->links() }}
@endsection

我不明白为什么我会得到一张空桌子。

0 个答案:

没有答案