在Laravel中显示来自不同表的数据

时间:2018-08-20 11:19:30

标签: php laravel eloquent

整个下午都研究了雄辩的人际关系和幼虫,似乎找不到解决方法。

我有一个事务表,在“发件人”和“收件人”列中显示用户ID。我想向用户显示与他们的ID相对应的电子邮件。

这是我的关系:

交易模型:

 @Named("contactForm") 

@RequestScoped 

public class ContactFormBean implements Serializable { 



    private static final long serialVersionUID = 1L; 

    private static final Logger log = Logger.getLogger(ContactFormBean.class); 



    private String name; 

    private String email; 

    private String requestMessage; 

    private String privacyAccordForm; 



    @PostConstruct 

    public void init() { 

        name = ""; 

        email = ""; 

        requestMessage = ""; 

        privacyAccordForm = ""; 

    } 



    //@URLAction 

    public void contactFormSendMessage() { 



        FacesMessage message = null; 



        // Connexion and receive the element text from the properties files message 

        // language 

        FacesContext context = FacesContext.getCurrentInstance(); 

        Application app = context.getApplication(); 

        ResourceBundle backendText = app.getResourceBundle(context, "i18n"); 

        String validationMessageSendEmail = backendText.getString("validation.form.send.email"); 

        String errornMessageSendEmail = backendText.getString("warning.form.error.send.email"); 





        //Methode for send email and return Bolean for validation  

        if (SendMail.sendMessage(name, requestMessage, privacyAccordForm, email)) { 

            message = new FacesMessage(FacesMessage.SEVERITY_INFO, validationMessageSendEmail, ""); 

        }else { 

            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, errornMessageSendEmail,""); 

        } 

        FacesContext.getCurrentInstance().addMessage(null, message); 

    } 



    public String getName() { 

        return name; 

    } 



    public void setName(String name) { 

        this.name = name; 

    } 



    public String getEmail() { 

        return email; 

    } 



    public void setEmail(String email) { 

        this.email = email; 

    } 



    public String getRequestMessage() { 

        return requestMessage; 

    } 



    public void setRequestMessage(String requestMessage) { 

        this.requestMessage = requestMessage; 

    } 



    public String getPrivacyAccordForm() { 

        return privacyAccordForm; 

    } 



    public void setPrivacyAccordForm(String privacyAccordForm) { 

        this.privacyAccordForm = privacyAccordForm; 

    } 



} 

用户模型:

public function user()
    {
        return $this->belongsTo('App\User', 'id', 'email');
    } 

这是来自控制器的代码。但是我知道这只会返回所有交易。但是我真的找不到如何显示电子邮件而不是ID:

public function transaction()
    {
        return $this->hasMany(App\Transaction);
    }

1 个答案:

答案 0 :(得分:0)

也许您应该尝试使用相关模型(用户)或联接到用户表来获得交易?

$transactions = Transaction::query()->with(['user'])->get();
$table = Table::create(Transaction::get());
$table->addColumn('email', 'E-mail', function($model) {
return $model->user->email;
});
return view('table', compact('table'));