Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hamelraj's avatar

Eloquent get data from second level table

I have a table operations remarks adjustments operations-

|  id | user_id  |operation|start_date |end_date  |
 |   1|  2  |  opeartion1|  1/1/16 | 5/5/16  |
|   2|  2  |  opeartion2|  1/1/16 | 5/5/16  |

remarks -

|id|user_id|operation_id|remarks|
|  10| 4  | 1 | remarks1 new| 
|  13| 4  | 2 |remarks22new| 

adjustments -

| id |remarks_id |user_id   |before  |after |created_at|updated_at|
 |   1|  10   |  6 |  {"remarks":"remarks1"}  | {"remarks":"remarks1 new"} |
|   2|  13   |  7 |  {"remarks":"remarks1"}  | {"remarks":"remarks1 new"} |

in this all three tables- user_id => Auth::id();

Adjustment Model

class Adjustment extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User');
    }
 public function remark()
    {
        return $this->belongsTo('App\Remark');
    }}

i want get data from adjustments table for particular operation_id like this Operation controller show method :

$history = Adjustment::with('user','remark')
            ->get();

from this im getting all the rows from adjustments table like this

Jerry Nienow 2016-05-12 07:00:24 {"remarks":"remarks1"} {"remarks":"remarks1 new"}
Jerry Nienow 2016-05-12 07:01:09 {"remarks":"remarks22"} {"remarks":"remarks22new"}

but i want particular operation_id ???

0 likes
4 replies
Hamelraj's avatar

@Prez tottaly headache this for me...... cause i know this is simple but it ate my whole day :( :(

pmall's avatar

I dont understand the problem as you didn't show the operation model

Hamelraj's avatar
Hamelraj
OP
Best Answer
Level 5
$history =Adjustment::join('remarks', 'remarks.id','=', 'adjustments.remark_id')
            ->where('remarks.operation_id', '=', $operation->id)->get();

Please or to participate in this conversation.