0

I need to pass a variable on multiple views in order to perform an UPDATE on multiple views ( editScadenza and elaborazioneScadenza). Or do you know another simpler solution?

ScadenzaController.php

public function edit($id)
{
    $data['scadenzaRecuperata'] = \App\Scadenza::find($id);

    return view('scadenze.editScadenza', $data);
}

UPDATE

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

    $this->validate($request,[
        'titolo'=>'required',
        'termine_stimato'=>'required',
        'responsabile'=>'required',
        'tipologia_id'=>'required',
        'giorni_avviso'=>'required',
    ],
    [
        'titolo.required'=>'Il titolo é obbligatorio',
        'termine_stimato.required' => 'Il termine stimato é obbligatoria',
        'responsabile.required' => 'Il responsabile é obbligatorio',
        'tipologia_id.required' => 'Il tipo é obbligatorio',
        'giorni_avviso.required'=> 'I giorni di avviso sono obbligatori',
    ]);

    $scadenza = \App\Scadenza::find($id);

    $now = Carbon::now();
    $end = Carbon::parse($scadenza->termine_stimato);
    $length = $end->diffInDays($now);

    $scadenza->titolo = $request->input('titolo');
    $scadenza->termine_stimato = date_create($request->input('termine_stimato'))->format('Y-m-d H:i');
    $scadenza->responsabile = $request->input('responsabile');
    $scadenza->tipologia_id = $request->input('tipologia_id');
    $scadenza->processo_id = $request->input('processo_id');
    $scadenza->stato = $request->input('stato');
    $scadenza->giorni_avviso = $request->input('giorni_avviso');
    $scadenza->osservazioni  = $request->input('osservazioni');

    $scadenza->save();

    return redirect('scadenza');

}
3
  • Try to use view composer Commented Mar 27, 2018 at 8:04
  • Did you know, when you're passing the ID of a model to a function you can access it like so public function edit (Scadenza $scadenza) which you could then do return view('scadenze.editScandenza', compact('scadenza'); It's one of laravel's great features and it saves you doing another DB query! :) Commented Mar 27, 2018 at 8:05
  • You should use view()->share (i.e laravel.com/docs/5.6/views) Commented Mar 27, 2018 at 8:05

2 Answers 2

1

The best recommended solution is to share variables from the controller function. If you need it just for 2, 3 views and all comes under the same controller then share it as usual:

public function function_nameX($id)
{
  ..
  return view('scadenze.viewNameX')->withData($data);
}

...

public function function_nameY()
{
  ..
  return view('scadenze.viewNameY')->withData($data);
}

Do it even if you have views returned from multiple controllers. Because this is the most convenient way.

To share a variable with all views in your project, share it from AppServiceProvider's boot() function like:

public function boot()
{
  $data['scadenzaRecuperata'] = \App\Scadenza::find($id);
  View::share('data', $data);
}

View Composer also help to bind specific data to view in different ways. You can directly bind variable to specific view or to all views. For Example you can create your own directory to store your view composer file according to requirement. and these view composer file through Service provide interact with view.

Here is the doc.

Sign up to request clarification or add additional context in comments.

4 Comments

I use the UPDATE form in two views. Is it possible that this thing conflicts? Because in the edit page it works, in the other it is not. But I used the same form.
You will definitely get the variable/object sent from the controller. You may not have the same values in both cases. Try: dd($name_of_variable); from controller before you send it to the view. You won't get the same or you may have different code in your views.
So the situation is strange. From the view I make an UPDATE (see updated code). I try to get the values ​​in the controller (works). If I print dd () before $ this-> validate ($ request) I see the values ​​if I insert it after this code I do not get the dd ();
Because the validation fails and Laravel redirects back. You have to print the error in the view. Take count($errors) in the view. @CarmineRub
1

To share data with views you can set a view Composer

in app/Providers/AppServiceProvider.php into boot() method

public function boot() {

  view()->composer('scadenze.editScadenza', function($view) {

    $data = \App\Scadenza::find(request()->id);

    $view->with('data', $data);
  });

}

For more date, see Laravel View Composers

  • if you need to share only with one: composer('VIEW_NAME', ...)
  • If you need to share the data with more than one: composer(['VIEW_NAME_1', 'VIEW_NAME_2'], ...)
  • If you need to share with all views: composer('*', ...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.