【问题标题】:Search Data from multiple tables laravel从多个表中搜索数据 laravel
【发布时间】:2020-03-20 13:43:28
【问题描述】:

我正在尝试从两个相关表中搜索多个数据。具体来说,我只想从用户表中获取“名称列”,从帖子表中获取其余列。但是每当我尝试搜索时,它都会打印以下错误“尝试获取非对象的属性'名称'”

下面是我的用户模型

<?php

namespace App;

use App\Mail\NewUserWelcomeMail;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Mail;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email','phone', 'username', 'password', 
        'admin', 'address', 'description', 'approved_at',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function posts()
    {
        return $this->hasMany(Post::class)->orderBy('created_at', 'DESC');
    }
}

并发布模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

还有我的控制器

public function showcampaign(User $user) {
    $q = Input::get( 'q' );

    if( !empty( $q ) ) {
        $showcampaign = User::join('posts','posts.user_id','users.id')
            ->where('name','LIKE','%'.$q.'%')
            ->orWhere('caption','LIKE','%'.$q.'%')
            ->orWhere('description','LIKE','%'.$q.'%')
            ->orWhere('duration','LIKE','%'.$q.'%')
            ->orWhere('amount','LIKE','%'.$q.'%')
            ->get();

        if(count($showcampaign) > 0) {
            return view('admin.campaignreport', ['show' => $showcampaign]);
        } else { 
            return redirect('/campaignreport')->with('status', 'No Details found. Try to search again !');
        }
    } else { 
        $showcampaign = Post::all();

        return view('admin.campaignreport')->with('show', $showcampaign);
    }
}

请帮忙谢谢

【问题讨论】:

  • 可以指定laravel版本吗
  • 我正在使用 laravel 5.8 @Manojkiran.A
  • @Owdenpk 您需要在 where 查询中使用table_name.column_name 语法。
  • 从错误看来,您在视图文件中收到此错误,但在控制器中没有。对吗??
  • 是的,当我从视图中搜索时得到

标签: php laravel laravel-5.8


【解决方案1】:

尝试.. 使用where 而不是orwhere

$showcampaign = User::join('posts','posts.user_id','users.id')
                ->where('name','LIKE','%'.$q.'%')
                ->Where('caption','LIKE','%'.$q.'%')
                ->Where('description','LIKE','%'.$q.'%')
                ->Where('duration','LIKE','%'.$q.'%') 
                ->Where('amount','LIKE','%'.$q.'%')->get();

【讨论】:

  • 如果您得到答案,请标记为有用谢谢@Owdenpk
  • where和orwhere与错误信息无关
  • 如果我尝试用 where 替换 orWhere,即使有现有记录,它也不匹配两个表中的任何信息 - @VikasKatariya
【解决方案2】:

我认为您需要为 where 子句使用参考表。

$showcampaign = User::join('posts','posts.user_id', '=', 'users.id')
            ->where('users.name','LIKE', '%'.$q.'%')
            ->orWhere('posts.caption', 'LIKE','%'.$q.'%')
            ->orWhere('posts.description', 'LIKE','%'.$q.'%')
            ->orWhere('posts.duration', 'LIKE','%'.$q.'%') 
            ->orWhere('posts.amount', 'LIKE','%'.$q.'%')
            ->get();

如果你正确定义了关系,那么使用:

$showcampaign = SampleReception::with(['posts' => function($query) use($q) {
            return $query->where('caption', 'LIKE','%'.$q.'%')
            ->orWhere('description', 'LIKE','%'.$q.'%')
            ->orWhere('duration', 'LIKE','%'.$q.'%') 
            ->orWhere('amount', 'LIKE','%'.$q.'%');
        }])
        ->orWhere('name','LIKE','%'.$q.'%')
        ->get();

【讨论】:

  • 谢谢@Amit senjaliya。我已经尝试按照您的建议添加引用并且错误已经消失,但现在它可以匹配两个表中的任何数据。即使我尝试搜索现有记录,它也无法匹配
【解决方案3】:

因为您已经在模型中声明了关系所以您可以使用whereHasorWhereHas

所以

$showcampaign = SampleReception::query()
            ->whereHas('posts',function(\Illuminate\Database\Eloquent\Builder $query) use ($q){
                return $query->where('caption', 'LIKE','%'.$q.'%')
                ->orWhere('description', 'LIKE','%'.$q.'%')
                ->orWhere('duration', 'LIKE','%'.$q.'%') 
                ->orWhere('amount', 'LIKE','%'.$q.'%');
            })
            ->orWhere('name','LIKE','%'.$q.'%')
            ->get();

有什么问题可以留言

【讨论】:

  • 它仍然打印相同的错误“尝试获取非对象的属性'名称'”
  • 能不能把posts和users表的表结构贴出来
猜你喜欢
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
相关资源
最近更新 更多