douyi2798 2017-06-11 16:36
浏览 119
已采纳

Laravel 5控制器返回值无法正常工作

I`m new to the laravel 5.4.i developed search_code controller like this.

public function search_code(Request $request){
    $query      = $request->search;
    $queryType  = $request->institute; // 'id' or 'name'
    $items      = DB::table('registerdetails');        

    if($queryType == 'id'){
        $items = $items->where('trainee_id', 'LIKE',"%$query%");
    }
    if($queryType == 'full_name'){
        $items = $items->where('full_name', 'LIKE',"%$query%");
    }
    $items = $items->get();
    return view('traineeattendance.index')->with('items',$items);
}

What i need is $item need to be pass and get called in two different views from this controller like

return view('traineeattendance.index')->with('items',$items);

And

return view('traineeattendance.attendance')->with('items',$items);

How can i do it?

  • 写回答

3条回答 默认 最新

  • duanchouyi6730 2017-06-11 18:39
    关注

    First, stop using DB facade in MVC framework unless it's very complicated query. Create a model instead

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class RegisterDetails extends Model
    {
        protected $table        = 'registerdetails';
        /* Look into documentation for rest of the fields */
    }
    

    Then for everything you need to search create model scopes

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class RegisterDetails extends Model
    {
        protected $table        = 'registerdetails';
    
        public static function scopeTrainee($query, $input)
        {
            if(!empty($input)) {
                $query->where("trainee_id", $input);
            }
        }
    
        public static function scopeFullname($query, $input)
        {
            if(!empty($input)) {
                $query->where("full_name", 'LIKE', $input);
            }
        }
    }
    

    Simplify your controller's code

    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    use App\Models\RegisterDetails;
    
    class RegisterDetailsController extends Controller
    {   
        public $data = [];
    
        public function search_code(Request $request){
            if($request->institute == 'id'){
                $this->data['items'] = RegisterDetails::Trainee($request->search)->get();
            }
            if($request->institute == 'full_name'){
                $this->data['items'] = RegisterDetails::FullName($request->search)->get();
            }
            return view('traineeattendance.index', $this->data);
        }
    }
    

    At now the index view has items data, if you are using attendance view inside index view it will also have this data accessible.

    If both index and attendance are different views (one is not included by another), then you did something wrong at design level, and you need to explain the case a little bit more

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证