我有两个名为users
和calls.
的数据库
通话表
<?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做到这一点?
答案 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();