doufuhao0566 2017-11-03 17:09 采纳率: 100%
浏览 40
已采纳

Laravel:在传递给`view`之前检查控制器是否有字段值

I have two table, one is User that stores login credentials and other is Userinfo that deals with storing personal information about the users. When a user sign up, they do not require filling up all the inputs for UserInfo table (i.e. they might only fill up the Gender and Religion fields out of a few other). So, the end result is that the UserInfo ends up being filled with only a couple of values or none at all.

The issue I am facing right now is when I pass through the data from both tables to view, and display them in typical laravel way, it throws an error Trying to get property of non-object. This is expected since not all the users have complete data from UserInfo table. I tried many methods both in controller and view to deal with the error message but failed to find a solution.

Is there a way to check the columns for empty or half complete value and deal with them properly in controller before passing onto the view? I think these checks should be performed in the controller or anywhere other than in the view. If there are none, how do I deal with them in the view? Maybe inside the loop? Solution and suggestions are greatly appreciated! Here's my code in the view:

@forelse($donors as $donor)

    <div class="panel panel-default">

        <div class="panel-heading"><i class="fa fa-list-alt"></i> {{$donor->name}}</div>

        <div class="panel-body">

            <div class="row">
                <div class="col-md-6">

                    <div class="basic-info">

                        <h4>Basic Information</h4>

                        <p><strong>Blood Group: </strong>{{  $donor->user_info->blood  }}</p>
                        <p><strong>Sex: </strong>{{ $donor->user_info->sex }}</p>
                        <p><strong>Religion: </strong>{{ $donor->user_info->religion }}</p>
                        <p><strong>Age: </strong>{{ \Carbon\Carbon::parse($donor->user_info->dob)->age }}</p>

                    </div>

                </div>

            </div>

        </div>

    </div>


@empty

    <p>No records found!</p>

@endforelse

As you can see, because not all the user have complete data in the table, laravel throws the Trying to get property of non-object error. My controller has standard laravel method of passing data to views:

public function index(User $user, Request $request)
{

    $donors = $user->with('user_info')->latest()->paginate(5);

    return view('donors.index', compact('donors'));
}
  • 写回答

1条回答 默认 最新

  • duandiao3961 2017-11-03 17:17
    关注

    Since version 5.5, Laravel has an optional() helper that I think it can help you.

    Try something like this in your view:

    optional($donor->user_info)->blood
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大