Laravel通过Route将变量从Form解析为Controller方法

时间:2018-12-08 04:17:08

标签: php html sql laravel laravel-routing

我正在尝试不使用资源就更新对帖子的编辑。我尝试使用{id}解析从表单到路由的变量,但该变量被忽略。这是我要发布的表格。

{!! Form:: open(['action'=> ['ManageBooksController@updateBook', $book->id], 'method' => 'POST']) !!}
<div class="form-group">
  {{Form::label('Book_NAME', 'Name')}}
  {{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::hidden('_method', PUT)}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}

这是我的路线

Route::put('manageBooks', 'ManageBooksController@updateBook');

这是我在控制器中的方法

public function updateBook(Request $request, $id)
{

    $this->validate($request, ['Book_NAME' => 'required']);

    $books = Books::find($id);
    $books->Book_NAME =$request->input('Book_NAME');
    $books->save();

    return redirect('/manageBook')->with('success', 'Book Edited');
}

6 个答案:

答案 0 :(得分:0)

您的路线期望PATCH。尝试将您的路线更新为:

Route::post('/manageBooks/{id}', 'ManageBooksController@updateBook');

或者在您的表单中包含Laravel的@method('PATCH')

此外,您的控制器名称不匹配:)

答案 1 :(得分:0)

考虑更改函数参数的顺序:

public function updateBook($id, Request $request)  // Notice the sequence of the arguments
{
    ......
}

答案 2 :(得分:0)

如果要使用Route,则必须具有类似的内容

{!! Form:: open(['route'=> ['manage_book', $book->id], 'method' => 'POST']) !!}

在您的路线中,您可能需要正确命名

Route::post('/manageBooks/{id}', array('as'=>'manage_book','uses'=>'ManageBooksController@updateBook'));

希望有帮助。

答案 3 :(得分:0)

不使用资源进行更新:

您的路线:

Route::get('/manageBooks', 'ManageBooksController@whateverer')->name('manageBooks');

Route::post('/manageBooks/{id}/edit', 'ManageBooksController@updateBook')->name('updateBook');

您的刀片:

{!! Form:: open(['route'=> ['updateBook', $book->id], 'method' => 'POST']) !!}
<div class="form-group">
  {{Form::label('Book_NAME', 'Name')}}
  {{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::hidden('id', $book->id)}} //hidden field is not required
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}

您的控制器:

public function updateBook(Request $request, $id)
{

    $this->validate($request, ['Book_NAME' => 'required']);

    $books = Books::where('id',$id)->update(['Book_NAME'=>$request->Book_NAME]);
    return redirect()->route('manageBooks')->with('success', 'Book Edited');
}

答案 4 :(得分:0)

最后,我添加了另一个隐藏字段,在该字段中,我解析正在编辑的帖子的ID。我还更改了find方法,使请求变量指向ID。

我的表格:

FirebaseDatabase.getInstance().setPersistenceEnabled(false); 

我的功能:

using UnityEngine;
using admob;
public class admobdemo : MonoBehaviour {
    Admob ad;
    string appID="";
    string bannerID="";
    string interstitialID="";
    string videoID="";
    string nativeBannerID = "";

    void Start () {
        Debug.Log("start unity demo-------------");
         initAdmob();
    }

    void Update () {
        if (Input.GetKeyUp (KeyCode.Escape)) {
            Debug.Log(KeyCode.Escape+"-----------------");
        }
    }

    void initAdmob()
    {
#if UNITY_IOS
                 appID="ca-app-pub-3940256099942544~1458002511";
                 bannerID="ca-app-pub-3940256099942544/2934735716";
                 interstitialID="ca-app-pub-3940256099942544/4411468910";
                 videoID="ca-app-pub-3940256099942544/1712485313";
                 nativeBannerID = "ca-app-pub-3940256099942544/3986624511";
#elif UNITY_ANDROID
                 appID="ca-app-pub-3940256099942544~3347511713";
                 bannerID="ca-app-pub-3940256099942544/6300978111";
                 interstitialID="ca-app-pub-3940256099942544/1033173712";
                 videoID="ca-app-pub-3940256099942544/5224354917";
                 nativeBannerID = "ca-app-pub-3940256099942544/2247696110";
#endif
        AdProperties adProperties = new AdProperties();
        adProperties.isTesting = true;

        ad = Admob.Instance();
            ad.bannerEventHandler += onBannerEvent;
            ad.interstitialEventHandler += onInterstitialEvent;
            ad.rewardedVideoEventHandler += onRewardedVideoEvent;
            ad.nativeBannerEventHandler += onNativeBannerEvent;
            ad.initSDK(appID,adProperties);//reqired,adProperties can been null
    }
    void OnGUI(){
        if (GUI.Button(new Rect(120, 0, 100, 60), "showInterstitial"))
        {
           Debug.Log("touch inst button -------------");
            if (ad.isInterstitialReady())
            {
                ad.showInterstitial();
            }
            else
            {
                ad.loadInterstitial(interstitialID);
            }
        }
        if (GUI.Button(new Rect(240, 0, 100, 60), "showRewardVideo"))
        {
            Debug.Log("touch video button -------------");
            if (ad.isRewardedVideoReady())
            {
                ad.showRewardedVideo();
            }
            else
            {
                ad.loadRewardedVideo(videoID);
            }
        }
        if (GUI.Button(new Rect(0, 100, 100, 60), "showbanner"))
        {
            Admob.Instance().showBannerRelative(bannerID,AdSize.SMART_BANNER, AdPosition.BOTTOM_CENTER);
        }
        if (GUI.Button(new Rect(120, 100, 100, 60), "showbannerABS"))
        {
            Admob.Instance().showBannerAbsolute(bannerID,AdSize.BANNER, 20, 220,"mybanner");
        }
        if (GUI.Button(new Rect(240, 100, 100, 60), "removebanner"))
        {
            Admob.Instance().removeBanner();
            Admob.Instance().removeBanner("mybanner");
        }

        if (GUI.Button(new Rect(0, 200, 100, 60), "showNative"))
        {
            Admob.Instance().showNativeBannerRelative(nativeBannerID,new AdSize(320,280), AdPosition.BOTTOM_CENTER);
        }
        if (GUI.Button(new Rect(120, 200, 100, 60), "showNativeABS"))
        {
            Admob.Instance().showNativeBannerAbsolute(nativeBannerID,new AdSize(-1,132), 0, 300);
        }
        if (GUI.Button(new Rect(240, 200, 100, 60), "removeNative"))
        {
            Admob.Instance().removeNativeBanner();
        }
    }
    void onInterstitialEvent(string eventName, string msg)
    {
        Debug.Log("handler onAdmobEvent---" + eventName + "   " + msg);
        if (eventName == AdmobEvent.onAdLoaded)
        {
            Admob.Instance().showInterstitial();
        }
    }
    void onBannerEvent(string eventName, string msg)
    {
        Debug.Log("handler onAdmobBannerEvent---" + eventName + "   " + msg);
    }
    void onRewardedVideoEvent(string eventName, string msg)
    {
        Debug.Log("handler onRewardedVideoEvent---" + eventName + "  rewarded: " + msg);
    }
    void onNativeBannerEvent(string eventName, string msg)
    {
        Debug.Log("handler onAdmobNativeBannerEvent---" + eventName + "   " + msg);
    }
}

答案 5 :(得分:0)

首先将方法POST更改为PUT,

{!! Form:: open(['action'=> ['ManageBooksController@updateBook', $book->id],'method' => 'PUT']) !!}
<div class="form-group">
    {{Form::label('Book_NAME', 'Name')}}
    {{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}

然后,您必须在路由中传递参数,因为您的方法需要$ id

Route::put('manageBooks/{id}/update', 'ManageBooksController@updateBook');