数据库读取数据返回 null 怎么办?

laravel 5.5, win10+Homestead环境, 项目有两个数据库,一个项目自己的,在Homestead里面,一个外部数据库,在我物理机上。现在我需要从外部数据库读取数据。
业务逻辑是这样的:在视图里填写student id然后点击check,检查提交的student id在外部数据库中是否存在,然后返回信息。数据库连接正常,因为我在homestead里用tinke命令行能读到数据,但是点击按钮后报错,报错信息见下方。var_dump显示返回Null.
请问这该怎么呢?新手刚入门,检查了半天还是不行。求大神指导,不胜感激!

报错信息:

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function isNotEmpty() on null

以下是代码

视图 ServiceRequest.blade.php

......
 <form class="form" method="post" action="{{ route('check') }}">
            {{ csrf_field() }}
                <label for="student_id">Please Enter Student ID: </label>
                <input type="text" name="student_id" class="form-control" value="{{ old('student_id') }}">
                <button type="submit" class="btn btn-primary">Check</button>
            </form>
......

路由 web.php

//Check Student ID
Route::post('/ServiceRequest', 'StudentsController@check')->name('check');

控制器StudentsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Student;

class StudentsController extends Controller
{
    public function check(Request $request)
    {
        $result = DB::connection('mysql_campus') -> table('students') -> where('student_id', $request) -> first();
        // var_dump($result);
        // exit();
         if ($result->isNotEmpty($result)){
             session()->flash('success', 'Correct!');
             return back();
         }else{
             session()->flash('danger', 'Sorry, not found!');
             return back();
        }
    }
    ......
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

原:Call to a member function isNotEmpty() on null
以上系统报告都说明,调用函数 isNotEmpty 的 member 是 null 值,就考虑一下 你的 $result 的值是什么;
提示:
1、该考虑 $result 的值,从这个变量赋值语句来判断,它的用处是什么才写下一步的代码;
2、既然查询结果有 null ,就应该考虑一下 null 结果的处理方案;
3、好好看文档

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!