Laravel Eloquent with and find

后端 未结 4 1967
孤城傲影
孤城傲影 2020-12-30 18:54

Why is this not working?

Article::with(\'category\')->find($ids)

I got a Array to String Conversion Exception.

But if i split t

4条回答
  •  爱一瞬间的悲伤
    2020-12-30 19:25

    Create object and set his properties with relationships

    for example, code in your Controller or Service

    $article = new Article;
    $article = $article->find($id);
    
    $result->article = $article;
    $result->article->category = $article->category;
    
    return response()->json($result);
    

    Code in your Model

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

提交回复
热议问题