【发布时间】: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