lundi 4 mars 2019

Injecting model and return value 'with' relationship in controller in laravel

I'm trying to build application on Laravel and I want to know whether we can use with relation while injecting the model inside a controller function for example:

I've a model named Stream and I've a model named Standard. Standard belongs to Stream so my code looks like:

class Standard extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    public function stream()
    {
        return $this->belongsTo('Models\Stream', 'stream_id', 'id');
    }
}

Now in my StandardController I've following function to show the Model:

public function show(Standard $standard)
{
    return response()->json(['data' => $standard], 200);
}

I'm trying to use return response()->json(['data' => $standard->with('stream')], 200); but it is not working accordingly. Can someone suggest me how to execute in this kind of scenario.

Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire