【问题标题】:laravel 7 join() and first() don't work togetherlaravel 7 join() 和 first() 不能一起工作
【发布时间】:2020-11-20 07:43:18
【问题描述】:

我想同时使用 join 和 first() 从两个表中选择信息,但我收到一条错误消息:

    TypeError: Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of 
    the type string or null, object given,

这就是我正在做的事情:

    public function getResult(Request $req){
       $result= DB::table('form')
       ->select('form.*', 'test.*')
       ->join('test','test.form_id' , '=', 'form.id' )
       ->where('test.id',$req->id)
       ->first();

       return $result;
}

注意:当我使用 get() 时它可以工作,但我只需要第一个选择的

你能告诉我该怎么做吗?

【问题讨论】:

  • 会使用限制并获得工作吗? ->limit(1)->get() 而不是 ->first()
  • limit(1) 让我得到类似 [{......}] 的结果,但我想要类似 {........} 的结果

标签: sql laravel join


【解决方案1】:

如果我使用leftJoin(),first() 对我有用,也许你可以试试这个?

@edit

你应该可以使用->get()-first()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 2021-05-14
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 2015-04-23
    • 2019-06-21
    相关资源
    最近更新 更多