【问题标题】:How to set the field can be null when there is relationship有关系时如何设置字段可以为空
【发布时间】:2021-01-07 03:49:46
【问题描述】:

我有一个问题,当我删除与该住宅相关的承包商时,索引视图<li>Contractor: {{$res->contractor->name}} 的这一行中会出现类似trying to get property name 的错误。我想通过删除承包商使其成为可能,然后住宅视图中的承包商字段可以为空或为空。反之亦然。我怎样才能做到这一点?这是我的模型。

Residential.php

class Residential extends Model
{
    protected $fillable = ['name','res_code','postcode','city','state','image','contractor_id'];

    public function contractor()
    {
        return $this->belongsTo(Contractor::class);
    }

    public function buyer()
    {
        return $this->hasMany(Buyer::class);
    }
}

Contractor.php

class Contractor extends Model
{
    protected $table = 'contractors';
    protected $fillable = ['name','address','phone_no','email','avatar'];

    public function residential()
    {
        return $this->hasMany(Residential::class);
    }

    public function users()
    {
        return $this->morphMany(User::class, 'typable');
    }
}

这是我的控制器。

ResidentialController.php

class ResidentialController extends Controller
{
    public function index(Request $request)
    {
        $contractor = Contractor::find($request->id);
        $residential = Residential::all();
        return view('residentials.index',compact('residential','contractor'));
    }

ContractorController.php

public function index(Request $request)
    {
       $residential = Residential::find($request->id);
       $contractor = Contractor::all();
       return view('admins.contractors.index', compact('residential', 'contractor'));       
    }

这是我的观点。

住宅\index.blade.php

@foreach($residential as $res)
<div class="panel-body">
   <ul class="list-unstyled list-justify">
       <li>ID: {{$res->id}}</li>
       <li>Code: {{$res->res_code}}</li>
       <li>Postcode: {{$res->postcode}}</li>
       <li>City: {{$res->city}}</li>
       <li>State: {{$res->state}}</li>
       <li>Contractor: {{$res->contractor->name}}
   </ul>
</div>
@endforeach

承包商\index.blade.php

<table class="table table-hover" id="contractor-table">
  <tr>
     <th>ID</th>
     <th>Company</th>
     <th>Phone No</th>
     <th>Email</th>
     <th>Residence</th>
     <th>Action</th>
  </tr>

@foreach($contractor as $cont)
   <tr class="contractor{{$cont->id}}">
       <td>{{$cont->id}}</td>
       <td><a href="/contractor/{{$cont->id}}/profile">{{$cont->name}}</a></td>
       <td>{{$cont->phone_no}}</td>
       <td>{{$cont->email}}</td>
       <td>{{$cont->residential->pluck('name')->implode(', ')}}</td>
@endforeach
</table>

希望有人能理解我的问题并帮助我。

【问题讨论】:

  • 通常你会在显示任何属性之前检查关系数据是否存在
  • {{ optional($res-&gt;contractor)-&gt;name ?? 'n/a' }}
  • 谢谢@Tpojka,这是我能做的最简单的事情。

标签: php laravel relationship one-to-many


【解决方案1】:

您必须在查询中使用LEFT JOIN。使用它您将自动收到NULL 值。

SQL LEFT JOIN 返回左表中的所有行,即使右表中没有匹配项。这意味着如果 ON 子句匹配右表中的 0(零)条记录;连接仍将在结果中返回一行,但右表中的每一列都为 NULL。

这意味着左连接返回左表中的所有值,加上右表中的匹配值,如果没有匹配的连接谓词,则返回 NULL。

参考链接: https://www.tutorialspoint.com/sql/sql-left-joins.htm#:~:text=Advertisements,column%20from%20the%20right%20table.

【讨论】:

    猜你喜欢
    • 2018-07-09
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多