在Laravel Eloquent中使用已定义的标准从数据库中获取数据

时间:2018-11-26 14:23:00

标签: php laravel eloquent

我有两个名为userscalls.的数据库

通话表

<?php

public function up()
{
    Schema::create('calls', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned()->nullable();
        $table->timestamps();
        $table->text('terminal_id', 20);
        $table->text('terminal_name', 100);
        $table->text('fault_description');
        $table->string('call_status', 10)->default('New call');
        $table->string('pending_on', 20)->nullable();
        $table->text('closed_on', 20)->nullable();
        $table->text('closed_by', 50)->nullable();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

CallsController

public function index()
{
    $calls = Call::orderBy('created_at', 'desc')->get(); 

    return view('pages.newCall')->with('calls', $calls); 
}

public function viewCall($id) { 
    $calls = Call::find($id);

    return view('pages.viewCall')->with('calls', $calls);
}

当前,CallsController返回calls表中的所有行,但我希望它仅返回{{ 1}}我该如何从CallsController做到这一点?

1 个答案:

答案 0 :(得分:1)

如果要获取具有

的行

1。call_status等于“新通话”:

$calls = Call::where('call_status', 'New call')->orderBy('created_at', 'desc')->get();

2。call_status不等于“新通话”:

$calls = Call::where('call_status', '<>', 'New call')->orderBy('created_at', 'desc')->get();

您可以使用!=代替上面的<>

3。call_status等于NULL(空):

$calls = Call::whereNull('call_status')->orderBy('created_at', 'desc')->get();

4。call_status等于“新通话”或NULL

$calls = Call::where('call_status', 'New call')->orWhereNull('call_status')->orderBy('created_at', 'desc')->get();

Laravel的ORM为Eloquent,它使用名为Fluent的查询生成器。有关查询构建的更多信息,请查看官方文档。