【发布时间】:2019-10-30 12:14:50
【问题描述】:
我试图让所有元素共享一个共同的行数据 但是我一直收到这个错误我不知道为什么
在 null 上调用成员函数 get()
这是我使用的代码,我已经导入了 App\User 和所有内容,但仍然没有得到它,并且我的表中有一个具有该 role_id 的用户
我的控制器
<?php
namespace App\Http\Controllers;
use App\Animal;
use App\Clinic;
use App\Role;
use App\Slaughter;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ClinicController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$farms = User::where('role_id', 3);
$user = Auth::user();
$animal = Animal::all();
return view('clinic.index', compact('user', 'animal', 'farms'));
}
我的用户表
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->unsignedBigInteger('address_id')->index();
$table->unsignedInteger('role_id');
$table->string('description')->nullable();
$table->timestamps();
$table->foreign('address_id')->references('id')->on('addresses');
});
}
但我不断收到下面的错误,我不知道为什么
【问题讨论】:
-
我的数据库没有问题
-
请输入您的控制器功能代码。
-
您确定在您的命名空间中定义了
User吗?你的语法是正确的。 -
@EyadJaabo - 如果没有
role_id,它会给出一个SQLException