php Laravel 中的关系和刀片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14480510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:22:47  来源:igfitidea点击:

relationship and blade in laravel

phpmysqllaraveleloquentblade

提问by Daniel Euchar

I have 3 table as mentioned below.

我有 3 个表,如下所述。

Table 1(user): 
id    username   password  Name Age

Table 2(tasks):
id  task_name  description

Table 3(logs) 
id user_id task_id date hours

Table Relationships:

表关系:

user has_many logs
task has_many logs

logs belongs_to user 
logs belongs_to  task

what i am trying to achieve is to get the logs with the user Name, task Name, date and hours.

我想要实现的是获取包含用户名、任务名称、日期和时间的日志。

Controller:

控制器:

return View::make('log.index')
            ->with('logs',log::all());

Blade template

刀片模板

@foreach($logs as $log)
             <tr>
                <td>{{$log->id}}</td>
                <td>{{$log->users()->name}}</td>
                <td>{{$log->tasks()->name}}</td>
            <tr>
@endforeach

but unable to fetch users Name and Tasks name from the respective table. any help is appreciated.

但无法从相应的表中获取用户名称和任务名称。任何帮助表示赞赏。

回答by Xiao Liu

A better way is to define inverse hasMany relation in your Model, as documented here

更好的方法是在您的模型中定义反向 hasMany 关系,如此处所述

So in your logs model, probably you need to define:

因此,在您的日志模型中,您可能需要定义:

class Log extends Eloquent {
    protected $table = "logs";

    public function user(){
         return $this->belongsTo('User');
    }
    public function task(){
         return $this->belongsTo('Task');
    }
}

Then in your view you can either use :

然后在您看来,您可以使用:

$log->user()->first()->name

or even better, by using Dynamic Properties:

甚至更好,通过使用动态属性:

$log->user->name

回答by Blessing

$log->users() and $log->tasks() returns a query object. Below, each call returns the result which is the same as calling $log->users()->get() and $log->tasks()->get(). Because the relationships are many to many, you'll need to iterate over $log->users and $log->tasks to retrieve each record.

$log->users() 和 $log->tasks() 返回一个查询对象。下面,每次调用都返回与调用 $log->users()->get() 和 $log->tasks()->get() 相同的结果。因为关系是多对多的,所以您需要遍历 $log->users 和 $log->tasks 来检索每条记录。

@foreach($logs as $log)
         <tr>
            <td>{{$log->id}}</td>
            <td>
                @foreach($log->users as $user)
                {{$user->name}},
                @endforeach
            </td>
            <td>
                @foreach($log->tasks as $task)
                {{$task->name}},
                @endforeach
            </td>
        <tr>
@endforeach

If you want a specific user/task attached to a log you'll have to build a query.

如果您希望将特定用户/任务附加到日志,则必须构建查询。

@foreach($logs as $log)
         <tr>
            <td>{{$log->id}}</td>
            <td>{{$log->users()->where('id', '=', $userID)->first()->name}} </td>
            <td>{{$log->tasks()->where('id', '=', $taskID)->first()->name}} </td>
        <tr>
@endforeach