如何拼接所有类别的用户?

时间:2017-03-18 07:51:31

标签: laravel laravel-5.4

我有两种模式:UserCategory

每个用户可以在表User_categories中拥有一个或多个类别。

还有表格UserCategories

我的关系是:

用户

public function categories(){
    return $this->hasMany('App\Category');
}

分类

public function users(){
    return $this->belongsToMany('App\User');
}

我尝试通过模型Category获取所有类别的用户:

User::where("id", $id)->with("country", "city", "categories")->first();

结果:

enter image description here

我尝试获取类别名称:

@foreach($profile->categoriesPivot() as $item)
<span class="label label-default">{{$item->category()->name}}</span>
 @endforeach

3 个答案:

答案 0 :(得分:1)

由于您在此处尝试使用多对多,请将关系更改为:

import java.util.ArrayList;
import java.util.Scanner;

public class Passenger
{
    // private varibales of the list.
    private  String pname;
    private  int seat;
    private  int age;

    public Passenger(String passengername, int pseat, int page)
    {
        // initialise instance variables
       pname = passengername;
       seat = pseat;
       age = page;
    }

    public static void main(String[] args)
    {
        Plane p = new Plane("Joey", 45, 26);


        String text = "Please select your option:\n" + "1.Add a passenger.\n" + "2.Find a passenger.\n" + "3.Total number of passengers.\n" + "4.Remove a passenger.\n" + "5.Print all passengers\n";;
        System.out.println(text);

        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();//waiting type the choice

        if(choice > 5 || choice < 0)
        {//if choice is wrong
            System.out.println("Please select a vailable option!");
        }

        while(choice <=5 && choice >= 0)
        {
            if(choice == 1)
            {
            Scanner inputname = new Scanner(System.in);
            System.out.println("Please enter the name of passenger");
            String x = inputname.nextLine();
            Scanner inputseat = new Scanner(System.in);
            System.out.println("Please enter the number of seat.");
            int y = inputseat.nextInt();
            Scanner inputage = new Scanner(System.in);
            System.out.println("Please enter the age of passenger.");
            int z = inputage.nextInt();
            Passenger padd = new Passenger(x, y, z);
            p.addPassenger(padd);
            System.out.println(text);
            choice = input.nextInt();
        }
        if (choice == 2)
        {System.out.println("Please enter the name you want to find.");
            String a = input.nextLine();
            p.findPassenger(a);
            System.out.println(text);
            choice = input.nextInt();
        }
        if (choice == 3)
        {
            p.numberofPassenger();
            System.out.println(text);
            choice = input.nextInt();
        }
        if (choice == 4)
        {System.out.println("Please enter the number of list which one you want to remove.");
            int b = input.nextInt();
            p.removePassenger(b);
             System.out.println(text);
            choice = input.nextInt();
        }
        if (choice == 5)
        {System.out.println("Here are all the variables of the list.");
            p.listPassenger();
             System.out.println(text);
            choice = input.nextInt();
        }
    } 
}

   public  void setpname(String pn)
    {
        pname = pn;
    }

    public  String getpname()
    {
        return pname;
    }   
}

数据透视表名称应为public function categories() { return $this->belongsToMany('App\Category'); }

答案 1 :(得分:1)

由于您已在用户模型中设置了关系,因此正确的方法是:

// Get all categories from user by $id $categories = User::find($id)->categories()->get();

答案 2 :(得分:0)

此案例定义为一对多

User模型中定义

public function getAllCategoriesAttribute()
{
    return Category::where('user_id', $this->id)->get();
}

尝试使用控制器

$user = User::find($user_id);
dd($user->allCategories);

更新:一对多案例中的上述源代码

此案例定义了“多对多”

User模型中定义

public function categories(){
    return $this->belongsToMany('App\Category', 'category_user', 'user_id', 'category_id');
}

在您的控制器中

dd($user->categories);