douhuibo5635 2017-11-02 01:58
浏览 233
已采纳

如何在Laravel中查询“数据不存在的地方”?

I am trying to make a query which says something like if hire_status exist, do not print out those value instead only print out those with hire_status that doesn't have value yet. But I do not know how to do it. I did something like that for now, can someone help me? Is another kind of function to do it?

What I want to do is inside the view file, when the user has inserted his data into the database, their name should disappear inside the view file to avoid clicking the person name twice. But I do not know how to do that

Here is the code:

     public function getHire(){
        $data['data'] = DB::table('personal_infos')->join('hires', 'personal_infos.id', '=', 'hires.user_id')->select('personal_infos.id','personal_infos.Name','personal_infos.deleted_at','hires.hire_status')->where('deleted_at',NULL)->whereNull('hires.hire_status')->get()->sortByDesc('created_at');
       if(count($data)>0){
        return view('hire',$data);
    }else{
    return view('hire');
}

personal_info models:

class personal_info extends Eloquent
{
    use SoftDeletes;
    protected $fillable = array('Email', 'Name');
    protected $table = 'personal_infos';
    protected $primaryKey = 'id';
    protected $dates = ['deleted_at'];
public function hires() {
        return $this->hasOne('App\hire','user_id');
    }
}

hire model:

   protected $fillable = array('hire_status','user_id');

public function personal_infos() {
    return $this->belongsTo('App\personal_info', 'user_id', 'id');
}

hire.blade.php

   <table class="table table-bordered">
      <tr>
        <th><strong><big>Name: </big></strong></th>     
        <th><strong><big>Hire Action: </big></strong></th>  
      </tr>
      <td>
      <tr>
        @foreach($data as $value)
      <tr>    
      <th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
    <th> 
      <form class="form-horizontal" method="post" action="{{ url('/hire_status/'.$value->id) }}">
        {{ csrf_field() }}
         <input type="hidden" name="user_id" value="{{$value->id}}">
        <input type="radio" name="hire_status" value="Yes"> Yes<br>
        <input type="radio" name="hire_status" value="No"> No<br> 

         <div class="form-group">
            <div class="col-md-6-offset-2">
              <input type="submit" class="btn btn-primary" value="Save">
            </div>
          </div>

        </form>  
   </th>             
      </tr>
        @endforeach
      </tr>
      </tr>
    </table>
  • 写回答

1条回答 默认 最新

  • doulei8861 2017-11-02 02:49
    关注

    With Eloquent models and relations between them properly defined, you'll be able to fetch all objects of ** personal_info** class that do not have a related hire record with:

    $personalInfos = personal_info::whereDoesntHave('hires')->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题