如何在两个表之间建立连接?

时间:2019-10-02 12:44:25

标签: php database laravel

我需要根据表profile_picture中的users从表user_id中获取log,但是我使用的所有内容都是错误的。请帮我看看这段代码?

这是我的模特

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Log extends Model
{
    protected $table = 'logs';


    public $timestamps = false;

    use SoftDeletes;
    // protected $softDelete = true;

    protected $fillable = [
        'user_id','log','date','type','article_type', 'formatted_date','link','action_user_id','action_name','action_username','item_id'
    ];


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

这是我的观点

@foreach($users as $user)

<img style="margin-right: 10px" src="{{ asset('thumbnail') }}/{{ $user->profile_picture }}" alt="">
@endforeach 

这是我的控制人

public function myAlerts($page = null)
{
$users =  \App\Log::with('users')
            ->where('user_id', '=', 'profile_picture')
            ->get();
}         

但是我遇到了这个错误:Call to undefined method Illuminate\Database\Query\Builder::users()

1 个答案:

答案 0 :(得分:3)

您在模型中创建了 user 方法,并且正在调用 users

example.com

如果您要检查$ user id,则可以像这样

public function myAlerts($page = null){
    $users =  \App\Log::with('user')->get();
}