如何在 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 call。

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

在代码中实现它,然后查看它抛出的错误。

结论:

第一个将是(假设您的其余代码没问题(静态错误。 如果要将其称为静态,请将其声明为静态。 但是控制器中的静态函数? 我看不出它有什么目的。

然后开始调试。 您的问题既是客户端,也是服务器端。 逐一处理。