Home > Net >  Laravel hasOne() Function Using $this when not in object context
Laravel hasOne() Function Using $this when not in object context

Time:12-03

I have 2 models named AdminContent, AdminCategory. I have content_category_id in my admin_contents table. I have category_id and category_name in my admin_categories table. I linked category_id with content_category_id foreign. I am using the hasOne() function in my Admin Content model. But I get the error Using $this when not in object context! My main goal is to get content_category_id value from admin_categories table name column

Migrations

// Admin Categories Migration
Schema::create( 'admin_categories', function(Blueprint $table) {
            $table->bigIncrements('ctgry_id')->unique();
            $table->string('category_name', 50)->unique();
            $table->timestamps();
        });
// Admin Contents Migration
Schema::create('admin_contents', function (Blueprint $table) {
            $table->bigIncrements('cntnt_id')->unique();
            $table->string('content_title');
            $table->text('content_content');
            $table->string('content_slug');

            $table->bigInteger('content_category_id');
            $table->foreign('content_category_id')->references('ctgry_id')->on('admin_categories');
            
            $table->string('content_status');
            $table->string('create_user');
            $table->string('content_tags');
            $table->string('content_excerpt');
            $table->dateTime('posted_at');
            $table->timestamps();
        });

Models

// AdminContent Model
protected $table = "admin_contents";

    protected $fillable = [
        'content_title', 'content_content',
        'content_category_id', 'content_status', 'create_user','content_tags',
        'content_excerpt',
        'created_at', 'updated_at'
    ];

    protected $guards = [
        'cntnt_id',
    ];

    public function setCategoryName()
    {
        return $this->hasOne(AdminCategory::class);
    }

When I want to access with $this->hasOne(AdminCategory::class) I get this error!

CodePudding user response:

First: relationships in Laravel are based in standardize models, using 'id' as column name for ids. If you are using another name for firstKey, you should add it to relationship definition, as stated in documentation. I mean, your relationship should not work because Eloquent doesn't know which are your tables first keys.

Second: when you define a relationship you should call id from your model. So how are you accessing to $this->hasOne(AdminCategory::class)? It should be something like AdminContent::with('setCategoryName')

Maybe showing some code from your controller we can give you a more accurate reply.

CodePudding user response:

What I want is this, I get the blog content with query and print it. But I am printing the content_category_id value as the id value in category table. What I need to do is get the content_category_id and the id value in the category table, the category name linked to that id. Thanks in advance for your help.

Admin Content Model

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\DB;

class AdminContent extends Model
{
    use HasFactory;

    protected $table = "admin_contents";

    protected $primaryKey = 'cntnt_id';

    protected $fillable = [
        'content_title', 'content_content',
        'content_category_id', 'content_status', 'create_user','content_tags',
        'content_excerpt',
        'created_at', 'updated_at'
    ];

    protected $guards = [
        'cntnt_id',
    ];

    public function _all()
    {
        return self::all();
    }

    public static function setCategoryName()
    {
        return $this->hasOne(AdminCategory::class, 'content_category_id', 'ctgry_id');
    }
}

Admin Category Model

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class AdminCategory extends Model
{
    use HasFactory;

    protected $table = 'admin_categories';

    protected $primaryKey = 'ctgry_id';

    protected $fillable = [
        'category_name', 'updated_at'
    ];

    protected $quards = [
        'ctgry_id', 'created_at'
    ];

}

Post Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Admin\AdminContent;

class PostController extends Controller
{
    public function index()
    {
        return view('frontend.blog');
    }

    public function getCategoryName()
    {
        return AdminContent::find(1)->setCategoryName;
    }
}

MySQL Tables

https://www.hizliresim.com/2z0337a

  • Related