Include model relationships in json response using Eloquent and Laravel 5

前端 未结 2 855
南旧
南旧 2021-01-31 03:33

I have a Model setup as so:



        
2条回答
  •  迷失自我
    2021-01-31 04:06

    Okay I believe this is what you're looking for ...

    Upload.php (no changes here)

    hasOne('App\Models\Mime', 'mime');
        }
    }
    

    Then you have your Mime model

    Mime.php

    if you do this for a test I believe you should see the type

    routes.php

    Route::get('test', function() {
        $upload = \Upload::with('mime')->first();
        // return $upload //here would return it as JSON I'm pretty sure!
        return $upload->mime->type;
    });
    

    Check docs for further details on eager loading: http://laravel.com/docs/5.0/eloquent#eager-loading

提交回复
热议问题