0

i am trying to get dataset with join, and i wanna do it using eloquent and automatically without no manual query. I did the relations too but when i try to list the data joined, somehow it does it otherway around.

Laravel version 5.7 MySQL database

UPDATED, added the database schema. my_tab3s table: my_tab3s

mytab1 table: enter image description here

Error:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'myTab1.my_tab3_id' in 'where clause' (SQL: select * from myTab1 where myTab1.my_tab3_id in (1, 2))"

myTab3.php

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class myTab3 extends Model
{
    //
    protected $table = 'my_tab3s';
    public $timestamps = true;


    protected $fillable = [
    'coolField',
    'muhCurrentDate',
    'rast',
    'myTab1_id'
  ];


    public function myTab1(){
        return $this->hasOne('App\myTab1');
    }
}

myTab1.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class myTab1 extends Model
{
    //
    public function scopeBigger($query){
        return $query->where('id','>','1');
    }
    public function scopeHasLetterZ($query){
        return $query->where('someField','like','%z%');
    }

    public function gimmeAll(){
        return myTab1::All();
    }
    protected $table = 'myTab1';
    public $timestamps = true;

    protected $fillable = [
    'someField'
  ];


    public function myTab3(){
        return $this->belongsTo('App\myTab3');
    }
}

myTab3Controller.php INDEX method

 public function index()
    {

        /*$dataSet = myTab3::All();*/
        //$dataSet->myTab1()->get();
        $dataSet = myTab3::with('myTab1')->get();
        return view('myTab3.index',compact('dataSet'));
    }

view part

@foreach($dataSet as $data)
        <tr>
            <td>{{$data->id}}</td>
            <td>{{$data->coolField}}</td>
            <td>{{$data->muhCurrentDate}}</td>
            <td>{{$data->created_at}}</td>
            <td>{{$data->updated_at}}</td>
            <td>{{$data->rast}}</td>
            <td>{{$data->myTab1->someField}}</td>
        </tr>
        @endforeach
Bagus Tesa
  • 1,317
  • 2
  • 19
  • 42
Skywarth
  • 623
  • 2
  • 9
  • 26

1 Answers1

0

It's hard to say without the database but i think you reverse your relationships, it shoud be a belongsTo for myTab3 and hasOne for myTab1

EDIT:

try with :

public function myTab1(){
    return $this->belongsTo('App\myTab1', 'myTab1_id');
}
lovis91
  • 1,988
  • 2
  • 14
  • 24
  • Almost worked, but now i got "Trying to get property 'someField' of non-object" error. Is it because i use {{$data->myTab1->someField}} ? – Skywarth Jan 03 '19 at 01:17
  • @Skywarth, try to `dd($data)` and inspect whether the `myTab3` is defined for all of the objects, otherwise you need to check `myTab3` field for null before `{{`printing`}}` them. – Bagus Tesa Jan 03 '19 at 01:21
  • @Louis R And with that, it returns me a result set like this: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":null}] which indicate that relation is not done by looking at the nulls. – Skywarth Jan 03 '19 at 01:21
  • @Louis R, yes seems like to do the magic, one of the row from the returning row is like that: {"id":2,"coolField":"rtryrtyrtyty","muhCurrentDate":"2019-01-03 00:12:19","created_at":"2019-01-03 00:12:19","updated_at":"2019-01-03 00:12:19","rast":3,"myTab1_id":3,"my_tab1":{"id":3,"someField":"zuhahahaha","updated_at":"2019-01-02 04:00:15","created_at":"2019-01-02 04:00:15"}}] But the problem is, how can i get that value for listing(in the foreach) ? – Skywarth Jan 03 '19 at 01:38
  • Ok, figured out how to list. Thanks for the solution. – Skywarth Jan 03 '19 at 02:10