简体   繁体   中英

How to use 'get' method with the where eloquent in laravel

I was trying to get all the elements sharing a common row data but I keep getting this error I do not know why

Call to a member function get() on null

Here is the code I used, I have imported the App\User and everything but still not getting it and I have a user in my table with that role_id

My controller

<?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'));
    }

My user table

<?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');
        });
    }

But I am keep getting that error bellow I do not know the why

use App/User;

$farm = User::where('role_id', 3)->get();

Try below code for getting the record

$farm = User::select(['id','name'])->where('role_id', 3)->get();

In case the User class extends Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model{}

calling

$builder = User::where('role_id', 3);

always returns an object of type Illuminate\Database\Eloquent\Builder, therefore, calling

$user= User::where('role_id', 3)->get();

always returns a collection (which might be empty when there is no user with role_id = 3). Check the type of your User class.

I think there are some missed steps that you didn't take.

The User does not refer to the table name but to the model class name. So first things first you need to create a model.

In order to do that you use the command:

php artisan make:model User

If you want a dedicated Models folder(i always use one) you use

php artisan make:model Models/User

By that time you should have a model created but you need to set it up.

Going inside your User.php file you should have something like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
 /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are NOT mass assignable.
     *
     * @var array
     */
    protected $guarded = [
        'created_at', 'updated_at', 'password,
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        //
    ];
}

Now by including your model at the top of your file you can use your Model class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM