2017-06-11 70 views
0

我對laravel 5.4.i新開發的search_code控制器是這樣的。Laravel 5控制器返回值不工作,因爲我想

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); 
} 

我需要的是$項目必須經過,並從該控制器像

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

而且

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

我如何能做到這一點有兩種不同的觀點被調用?

+0

您可能需要閱讀HTTPS ://laravel.com/docs/5.4/views#passing-data-to-views –

+0

先生,請你給我加上必要的代碼樣本,這將非常讚賞。我讀了,但我不明白它希望新的laravel可以請你幫忙嗎? – Dasun

+0

「不能按我想要的方式工作」,因爲它不是以您想使用它的方式創建的(您無法從1方法返回2個視圖)。而不是試圖強迫你的方式工作,強迫自己工作的方式工具。讓你的生活更輕鬆 –

回答

-1

首先,停止使用DB門面的MVC框架,除非它是非常複雜的查詢。 創建一個模型,而不是

namespace App\Models; 

use Illuminate\Database\Eloquent\Model; 

class RegisterDetails extends Model 
{ 
    protected $table  = 'registerdetails'; 
    /* Look into documentation for rest of the fields */ 
} 

然後你需要搜索創建模型中的任何作用域

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); 
     } 
    } 
} 

簡化您的控制器的代碼

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); 
    } 
} 

在現在的索引視圖有項數據,如果您正在索引視圖內使用考勤視圖,它也可以訪問此數據。

如果這兩個指標和出席不同的看法(一個不包括由其他),那麼你做錯了什麼,在設計水平,你需要解釋的情況下多一點點

+0

不明原因的downvote是您可以在SO社區感受到的最好的東西 –

-1

使用類似view::share('variable_name', 'variable_value')的東西。

您的案例:

轉到AppServiceProvider上引導方法添加一行: *不要忘記導入類

然後添加上引導方法行:

View::share('items', search_code()); 

這將採用方法search_code()的返回值並使其可用於所有視圖$items

像這樣:在App- Providers- AppServiceProvider上轉到AppServiceProviders.php,並在引導方法上添加以下內容,包括頂部的命名空間。

use Illuminate\Support\Facades\View; 
use App\Http\Controllers\YourController; 

    public function boot() 
     { 
      $controller = new YourController; 

      View::share('items', $controller->search_code()); 
     } 
+0

先生,你可以請解釋一下代碼更please.because我不明白你在說什麼,如果我知道這不會有問題 – Dasun

+0

@LKelmendi我怎麼能將這個應用到我的search_code控制器? – Dasun

+0

使用視圖::分享()當你真的需要,這是沉重的矯枉過人使用它的2個意見 –

0
<?php 

namespace App\Providers; 

use Illuminate\Support\Facades\View; 

class AppServiceProvider extends ServiceProvider 
{ 
    /** 
    * Bootstrap any application services. 
    * 
    * @return void 
    */ 
    public function boot() 
    { 
     View::share('items', search_code()); // this line will inject the return value of search_code() in all views in your app 
    } 

    /** 
    * Register the service provider. 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     // 
    } 
} 
+0

@Thoms如何將這可以應用到我給我的search_code控制器 – Dasun

+0

我舉個例子,如果還不清楚,我擔心你應該先閱讀文檔,自己做一個小演示,然後再試一次。雖然最好的luch! –