laravel return重定向内部函数 [英] laravel return Redirect inside function

查看:79
本文介绍了laravel return重定向内部函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器上有此代码,但返回值不起作用,因为它位于函数内部,如何使它起作用?
我这样做是为了确保将数据保存在数据库中并重定向到面板.

I have this code on my controller but the return doesn't work because it's inside a function, how can I make it work?
I do it to ensure that the data is saved on the database and the redirect to panel.

public function save(){
 .
 .
 .
 Offer::saved(function($offer)                
 {
    Log::info('saved'); 
    //send email, etc.
    return Redirect::to('/panel');
 });
}

谢谢您的帮助.

推荐答案

您是否真的需要在saved事件中进行重定向?

Do you really need to make that redirect inside the saved event?

类似的事情应该起作用:

Something along the lines of this should work:

public function save(){
    if($offer->save()){
        return Redirect::to('/panel');
    }
}

(如果一切顺利,save()将返回true)

(save() will return true if everything went down well)

但是,如果您真的真的需要这样做,可以调用send()手动返回"响应:

However if you really really need to do it in the event, you can call send() to "return" the response manually:

Offer::saved(function($offer)                
{
    Log::info('saved'); 
    Redirect::to('/panel')->send();
});

编辑

对于类似DB::table('offers')->where('id',$inputs['id'])->update($fields);

您也可以检查返回值. update方法将返回受影响的行数.因此,如果是> 0 truthy ,则记录已更新.

You can check the return value too. The update method will return a count of affected rows. So if that is > 0 or truthy the record was updated.

$affectedRows = DB::table('offers')->where('id',$inputs['id'])->update($fields);
if($affectedRows){
    return Redirect::to('/panel');
}

但是,如果您已经建立了Eloquent模型,那为什么还要去使用查询生成器呢?

But if you have set up your Eloquent model, why even bother with the Query Builder:

$offer = Offer::find($inputs['id']);
$saved = $offer->update($fields);
if($saved){
    return Redirect::to('/panel');
}

这篇关于laravel return重定向内部函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆