从两个不同表中的相同字段名称中获取数据,并检测我们指的是哪个文件名

问题描述 投票:2回答:1

我已经为我的应用程序分配了搜索方法,并且我在两个表“ users”表和“ posts”表之间建立了关系,它们都具有相同的字段名称“ created_at”,并且当我想获取数据时,创建后,它将在用户表created_at字段中而不是在帖子表created_at字段中显示日期。我想要的只是如何告诉系统我要区分的表字段名称之间的区别。

此日期来自用户表中的created_at字段This date comes from created_at field in users table

我想在帖子表中的created_at字段中指定日期I wand to come date in this created_at field in posts table

ListingController.php

public function search(Request $request){

     $posts = Post::with(['comments','category','creator'])
                 ->join('users', 'posts.created_by', '=', 'users.id')
                 ->where('slug', 'LIKE', '%'.$request->search. '%')
                 ->orWhere('users.name', 'LIKE', '%'.$request->search. '%')
                 ->where('status',1)->orderBy('posts.id','DESC')->paginate(5); 

         return view('front.listing',compact('posts'));

 }

listing.blade.php


<div class="article_date">
               by: <a href="{{ url('/author') }}/{{ $post->creator->id }}">{{ $post->creator->name }} </a> , {{$post->created_at->diffForHumans()}}
            </div>


php laravel laravel-5.8
1个回答
0
投票
您不正确地称呼您的关系。您不应该使用联接。使用此查询代替:
© www.soinside.com 2019 - 2024. All rights reserved.