0

So I'm trying to add get a specific data from a related table using the below method, but I don't know if that is the correct way to do it. here is what it looks like.

public function  transspecific($lid){
    return $this->belongsTo('raplet\Keepertrans')->where("lang_id", $lid);
}

and then I try to get data from it

dd($akeeper->transspecific($akeeper->id));

it doesn't act like there is anything but when I type dd("hello") inside the model, it works. so clearly I have something wrong with my relationship context

Agil
  • 376
  • 5
  • 26
  • By the way, the variable `$lid` also passes correctly, it all comes down to the tiny query I make there. Any idea how can make it work? – Agil Feb 11 '19 at 22:22
  • 1
    You probably need to `get()` - `dd($akeeper->transspecific($akeeper->id)->get());` – ceejayoz Feb 11 '19 at 22:39
  • @ceejayoz I tried that but it throws a messy SQL error that I can't work out – Agil Feb 12 '19 at 16:55
  • So give us that messy SQL error. – ceejayoz Feb 12 '19 at 16:58
  • So it was mainly throwing an SQL error about the `pivotForigenKey`, I did some regulations and now it doesn't throw any errors, but there are no data too. – Agil Feb 12 '19 at 17:05
  • This is what I have in main relationship: `return $this->belongsToMany('raplet\Keepertrans', 'keepers', 'id', 'id');` – Agil Feb 12 '19 at 17:07
  • When I just do `return $this->belongsToMany('raplet\Keepertrans');` it gives me whiios page with the following error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'raplet_db.keeper_keepertrans' doesn't exist (SQL: select `keeper_translate`.*, `keeper_keepertrans`.`keeper_id` as `pivot_keeper_id`, `keeper_keepertrans`.`keepertrans_id` as `pivot_keepertrans_id` from `keeper_translate` inner join `keeper_keepertrans` on `keeper_translate`.`id` = `keeper_keepertrans`.`keepertrans_id` where `keeper_keepertrans`.`keeper_id` = 1 and `lang_id` = 1) – Agil Feb 12 '19 at 17:09

2 Answers2

1

What are you are trying to do is adding a [dynamic scope in laravel][1] model, which is totally fine. Except you need to declare the scope seperated from relationship method.

Relationship:

public function keepertrans(){
    return $this->belongsTo('raplet\Keepertrans');
}

Scoped:

public function transspecific($lid){
    return $this->keepertrans()->where("lang_id", $lid);
}

Then you can call the scope with a get() to execute the query builder:

(SomeOtherRelatedModel::first())->transspecific($someId)->get();
Leo
  • 7,274
  • 5
  • 26
  • 48
1

The methods available in Eloquent model for relationship are different than what you need. Whenever you need to add a custom function which internally adds some filters to your query (builder), you need to use scopes

The generic rule of scope function is scope + yourfunction

In your case you will need to create scopeTranspecific function.

Each scope gets the first argument as builder which you update inside the function. The later arguments are optional.

public function scopeTranspecific($query, $lid){

    return $query->keepertrans()->where("lang_id", $lid);
}

And then you can use it :

Model::where('column1' , 'value')->transpecific($id)->get()

If you just dump it without ->get() you will get the query builder instance. You will have to do ->get() to get the data

Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37
  • Excuse me, sir, is the `$query` represent the main model or is it an entire query itself? – Agil Feb 12 '19 at 17:01
  • It is the instance of query builder. Conceptually what Model::query() gives you. obviously it gets updated as you apply where clauses and join etc. :) – Mihir Bhende Feb 12 '19 at 17:15