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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化