Laravel 拉威尔模型关系问题

Laravel 拉威尔模型关系问题,laravel,laravel-5,Laravel,Laravel 5,目前我有两个模型,Roundtable和TableCategory,我用下面的代码创建了它们之间的关系 圆桌会议 表2.1类别 表类别模型 public function roundtables(){ return $this->hasMany('App\Roundtable'); } 圆桌模型 public function table_category() { return $this->belongsTo('App\Table_Category'); }

目前我有两个模型,Roundtable和TableCategory,我用下面的代码创建了它们之间的关系

圆桌会议

表2.1类别

表类别模型

public function roundtables(){
    return $this->hasMany('App\Roundtable');
}
圆桌模型

public function table_category()
{
    return $this->belongsTo('App\Table_Category');
}
我的控制器

public function show(Request $request){
    $id=Auth::user()->id; //Logged user id is 1
    $table= Roundtable::where('user_id',$id)->get();
    return view('users.tables.show')->withTables($table);

}
这应该把它们连接起来,对吗

然后当我试图展示

 @foreach($tables as $table)
                    <tr>
                        <td><a href="{{ route('table_page',['id'=>$table->id]) }}">{{$table->name}}</a></td>
                        <td>
                            <a href="#" class="btn btn-info btn-xs">View</a>
                            <a href="#" class="btn btn-primary btn-xs">Edit</a>
                            <a href="#" class="btn btn-danger btn-xs">Delete</a>
                        </td>
                    </tr>
                        <tr>
                            <td>{{ $table->table_category->name }}</td>
                        </tr>
                    @endforeach
@foreach($tables作为$table)
{{$table->table_category->name}
@endforeach
将出现“尝试获取非对象的属性”错误


我能知道有什么问题吗?

您想先打电话给

$table->table_category()->first()->name
get()
将返回集合。使用
first()
获取对象:

$table = Roundtable::where('user_id', $id)->first();
您正在查询中使用get()。这是一个雄辩的收藏

您需要使用foreach或first进行查询


我希望,它会对您有所帮助。

您可以通过两种方式来实现这一点。首先是利用关系。在控制器上,尝试以下操作:

public function show(Request $request){
    $id=Auth::user()->id; //Logged user id is 1
    $table= Roundtable::with('table_category')->where('user_id',$id)->get();
    return view('users.tables.show', compact('table'));
}
users/tables/show.blade.php
在此场景中应保持不变

第二种方式:

在圆桌会议模型上添加以下方法:

public function getCategoryAttribute(){
        $category = CategoryModel::where('id', $this->attributes['category_id'])->first();
        if(!$category ){
            return "";
        }
        return $category->name;
    }
在您看来,您必须从:

{{ $table->table_category->name }}


更新了错误“未定义属性:Illumb\Database\Eloquent\Relations\BelongsTo::$name”答案。鉴于
圆桌会议
只能属于1
TableCategory
,您可以对关系调用
first
。仍然有“尝试获取非对象的属性”,我更新了控制器代码,它们是否相关?如果使用$table=Roundtable::where('user_id',$id)->first();显示结果的页面将显示“正在尝试获取非对象的属性”,这意味着
$table
null
。您可以使用
dd($table)
进行检查。当没有
user\u id=$id
Yes为空的表时,您将得到
null
,我发布了我的数据库表列。你知道如何实现吗?正如我所说的,这意味着没有适合该用户的表。例如
Roundtable::where('user\u id',1)->first()->table\u category->name
应该向您显示
娱乐
,因为有一个表供id=1的用户使用,并且该表有一个类别。您可以使用
is_null()
检查结果是否为
null
是我记录的用户id为1,这就是为什么我不知道它仍然返回null的原因。请尝试使用
Roundtable::with('table_category')->where('user_id',$id)->first()
public function getCategoryAttribute(){
        $category = CategoryModel::where('id', $this->attributes['category_id'])->first();
        if(!$category ){
            return "";
        }
        return $category->name;
    }
{{ $table->table_category->name }}
{{ $table->category }}