Laravel hasManyThrough 2表

时间:2017-12-25 12:18:39

标签: php mysql laravel laravel-5 eloquent

我有两张表如下: 用户表:

|id|name|email|password|created_at|updated_at

消息表:

|id|sender_id|receiver_id|message|created_at|updated_at

用户模型:

public function threads()
{
    return $this->hasManyThrough(Message::class, User::class,'id','sender_id');
}

我正在尝试检索邮件线程 哪个不行。任何帮助表示感谢。

2 个答案:

答案 0 :(得分:1)

您似乎想要使用hasMany关系通过User模型检索属于单个线程(用户列表)的所有消息,要做到这一点您必须在Thread模型中定义hasManyThrough而不是在User模型中,这是一个例子:

用户:

|id|name|email|password|created_at|updated_at|thread_id

注意thread_id外键,因为线程是用户列表

主题:

class Thread extends Model {

    public function messages() {

        return $this->hasManyThrough(Message::class, User::class,'thread_id','sender_id', 'id', 'id');

    }

}
来自laravel doc的

Example

答案 1 :(得分:0)

这是laravel eloquent relationship

的示例
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xamarin.Forms;
/*
this class will help you to have bindable children with different sizes for the stacklayout with scrollview 
in you xaml add 
    <UIControls:SAStackLayout ItemsSource="{Binding YourDataSource}"  Orientation="Horizontal">
                            <DataTemplate>
                                <Grid>
                                    <Label Text="{Binding Name}" Margin="15,0" HorizontalOptions="Center" VerticalOptions="Center"    FontSize="Small"  VerticalTextAlignment="Center" HorizontalTextAlignment="Center" TextColor="White"/>
                                </Grid>
                            </DataTemplate>
   </UIControls:SAStackLayout>

 */
namespace Shop.UI.Controls
{
    [ContentProperty("ItemContent")]
    public class SAStackLayout : ContentView
    {
        private ScrollView _scrollview;
        private StackLayout _stacklayout { get; set; }
        public SAStackLayout()
        {
            _stacklayout = new StackLayout();
            _scrollview = new ScrollView()
            {
                Content = _stacklayout
            };
            Content = _scrollview;
        }
        public static readonly BindableProperty ItemContentProperty = BindableProperty.Create("ItemContent", typeof(DataTemplate), typeof(SAStackLayout), default(ElementTemplate));

        public DataTemplate ItemContent
        {
            get { return (DataTemplate)GetValue(ItemContentProperty); }
            set { SetValue(ItemContentProperty, value); }
        }


        private ScrollOrientation _scrollOrientation;
        public ScrollOrientation Orientation
        {
            get
            {
                return _scrollOrientation;
            }
            set
            {
                _scrollOrientation = value;
                _stacklayout.Orientation = value == ScrollOrientation.Horizontal ? StackOrientation.Horizontal : StackOrientation.Vertical;
                _scrollview.Orientation = value;
            }
        }

        public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(SAStackLayout), default(IEnumerable), propertyChanged: GetEnumerator);
        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        private static void GetEnumerator(BindableObject bindable, object oldValue, object newValue)
        {
            foreach (object child in (newValue as IEnumerable))
            {
                View view = (View)(bindable as SAStackLayout).ItemContent.CreateContent();
                view.BindingContext = child;
                (bindable as SAStackLayout)._stacklayout.Children.Add(view);
            }
        }
    }
}

你可以查看你的代码