簡體   English   中英

如何在Laravel 4中將模型函數從控制器導出到視圖

[英]How to export a model function from a controller to a view in Laravel 4

我試圖顯示數據庫中的某些數據,這些數據取決於用戶的某些輸入。 我正在使用ajax請求來獲取數據,將其發送回控制器中的函數,然后將其導出回我的視圖。 我想收集此數據並顯示它,而無需進入另一個視圖(我只是隱藏了以前的表單而未隱藏新表單)。

以下是相關代碼:

Javascript:

$('#submit_one').on('click', function(event) {
        event.preventDefault();

        if(! $(this).hasClass('faded')) {


        var fbid = $("input[name='like']:checked").val();
//variable to be collected is fbid


        request = $.ajax({ 
        url: "http://crowdtest.dev:8888/fans/pick_favorite", 
        type: "post", success:function(data){}, 
        data: {'fbid': fbid} ,beforeSend: function(data){
            console.log(data);
        } 
        });

        to_welcome_two();
        }

    });

function to_welcome_two()
{
    $('#welcome_one').addClass('hidden');
    $('#welcome_two').removeClass('hidden');
}

控制器功能:

public function pick_favorite() {

            $fbid=Input::get('fbid');

            return Artist::specific_artist($fbid);

        }

公共功能getWelcome(){

        return View::make('fans.welcome')
        ->with('artists', Artist::artists_all())
        ->with('favorite_artist', Artist::favorite_artist())
        ->with('pick', FansController::pick_favorite());

    }

型號功能:

public static function specific_artist($fbid) {
        $specific_artist = DB::table('artists')
                        ->where('artists.fbid', '=', $fbid)
                        ->get();

        return $specific_artist;

    }

該視圖位於“歡迎”頁面上。 我的問題是如何在我的視圖中顯示模型數據,並確保它從fbid輸入中打印出正確的數據?

我嘗試過這樣的事情:

@foreach($pick as $p)
    <span class="artist_text">{{$p->stage_name}}</span>
    <br>
    <span class="artist_city">{{$p->city}}</span>
@endforeach

但這並沒有打印出任何東西。 有任何想法嗎?

我在這里看到很多問題。

服務器端:

public function pick_favorite() ....它做什么? 它只是返回一些數據。

public function getWelcome() { ,您編寫了FansController::pick_favorite() 假設兩者是相同的方法,則您將訪問靜態方法,而該方法是非靜態的。 您為此收到一個錯誤,但由於未定義fail()而沒有看到它。

而且我不知道聲明一個不執行其他任何操作然后直接進行模型調用的方法有什么意義。

例如,假設我有一個fooModel

public function index(){}

在控制器中,我可以寫,

public function bar()
{
  $model = new fooModel;
  return View::make(array('param1'=>$model->index()));

}

或者如果我在fooModel中將 index()方法聲明為靜態方法,那么我可以編寫,

public function bar()
{
    return View::make(array('param1'=>fooModel::index()));
}

客戶端:

現在在您的JavaScript中,

$('#submit_one').on('click', function(event) {
    event.preventDefault();

    if(! $(this).hasClass('faded')) {


    var fbid = $("input[name='like']:checked").val();
//variable to be collected is fbid


    request = $.ajax({ 
    url: "http://crowdtest.dev:8888/fans/pick_favorite", 
    type: "post", success:function(data){}, 
    data: {'fbid': fbid} ,beforeSend: function(data){
        console.log(data);
    } 
    });

    to_welcome_two();
    }

});

function to_welcome_two()
{
    $('#welcome_one').addClass('hidden');
    $('#welcome_two').removeClass('hidden');
}

為什么要打印任何數據? 您沒有要求腳本打印任何內容 您的.done.success參數在代碼中的什么位置?

如果您查看控制台,就會發現很多php錯誤,我幾乎可以肯定。

一個建議,您需要學習一些基礎知識。 例如jquery ajax調用。

a basic ajax call can be 

    var request = $.ajax({
url: "script.php",
type: "POST",
data: { id : menuId },
dataType: "html"
});
request.done(function( msg ) {
$( "#log" ).html( msg );
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});

在您的代碼中實現它,然后查看它會引發什么錯誤。

結論:

第一個是(假設您其余的代碼都可以) 靜態錯誤。 如果要將其稱為靜態,則將其聲明為靜態。 但是控制器中的靜態功能? 我沒有看到任何目的。

然后開始調試。 您的問題是客戶端和服務器端。 一一成交。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM