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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)