How to get data from relationship table using laravel eloquent model?

Posted 1 year ago by Crazylife

This is my model

public function test()
        return $this->belongsTo('App\Models\Test', 'id_product');

in my blade i will loop it

foreach($products as $product){
            $products_id = $product->products_id;

I have a question on, can i get the data from relationship table using eloquent model if the value stored format is different for foreign?

For example, the primary key in table product is abc_001. And foreign key in another table is admin_abc_001. By doing some filter or condition on the value so is that possible i loop it like this?

foreach($products as $product){
            $products_id = $product->test->col_1;

Please sign in or create an account to participate in this conversation.