Eloquent model is returning as builder

Viewed 2091

I'm relearning Laravel with laravel 7 and have hit an issue where I'm unable to query a record in my database table. So instead of a call like $test = Test::find_by_id_and_name(1, 'test 1'); (and also $test = Test::where('id', 1); returning a class of Illuninate\Database\Eloquent\Model it returns a class of Illuminate\Database\Eloquent\Builder.

I have created a Migration for a table called Tests and seeded it with a few rows of test data. The Test Model in App is as follows

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Test extends Model
{
    protected $guarded = [];
    use SoftDeletes;

}

the Migration is:

se Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->string( 'name' );
            $table->string( 'url', 255 );
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}

So anyone any idea why I'm not getting the Model I need so i can do for instance a dd($test); and see the values stored in the database for the row with the id of 1? or even do an echo($test->name); and see the name of this item?

thanks

* ADDITIONAL * Should of pointed out my initial code had Test::find_by_id_and_name(1, 'test 1'); but this didn't work and throw an exception about finding the class. I modified if with where and above was a typo as it was where( 'id', 1 ); (I've corrected the code using my initial find_by code). Adding a get() or any other thing now returns null. I have verified that the database contains the table tests and that an item with the id and name of 'test 1' exists

* RESULT * The underlying issue in the end was the data, the url had https::// in it (additional colon) so indeed it would return null. Thanks guys helped me find the reason.

2 Answers

Misunderstanding of query builder vs models in Laravel. Check doc for reference.

Calling query builder method statically on a model returns a builder.

User::where('id', 1); // returns builder

To resolve a query builder you can either use get() or first().

User::where('id', 1)->get(); // Returns a collection of users with 1 element.
User::where('id', 1)->first(); // Returns one user.

You can also fetch the user out of the collections this is not recommended as you might as well just call first().

User::where('id', 1)->get()->first(); // Returns collection fetches first element that is an user.

Laravel has static methods for finding models by id.

User::find(1); // returns user or null
User::findOrFail(1); // returns user or exception

Try to use the following

$test = Test::find(1);

Then you will get the record,

Related