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 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)