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条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题