Foreach Laravel does not update with tabs navigation

Asked

Viewed 40 times

0

I have a view that has tabs navigation. When implementing a way that when saving or editing a record it goes to the respective tab, the foreach that lists the record has stopped working. I need to go to another view and return for the new record to appear. The tab in question is Differentials.

Tagcontroller

namespace App\Http\Controllers;

use App\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class TagController extends Controller
{

public function store(Request $request)
{
    $user = Auth::user();
    $supplier = $user->userable;
    $tab = 3;
    $tags = $supplier->tags;

    if (!$tags->contains('name', $request->name)) {
        $tag = new Tag($request->all());
        $tag->supplier_id = $supplier->id;
        $tag->save();
        Session::flash('alert-success', 'Diferencial incluído com sucesso!'); 
        return view('supplier.profile')
            ->with('user', $user)
            ->with('tab', $tab)
            ->with('supplier', $supplier);
    } else {
        Session::flash('alert-warning', 'Esse diferencial já existe!'); 
        return view('supplier.profile')
            ->with('user', $user)
            ->with('tab', $tab)
            ->with('supplier', $supplier);
    }

}

public function destroy(Tag $tag)
{
    $user = Auth::user();
    $supplier = $user->userable;
    $tab = 3;
    $tag->delete();
    Session::flash('alert-success', 'Diferencial excluído com sucesso!'); 
    return view('supplier.profile')
        ->with('user', $user)
        ->with('tab', $tab)
        ->with('supplier', $supplier);
}
}

View profile.Lade

@extends('supplier.app')
@section('title', 'Meu Perfil')
@section('content')
<div class="row">
<div class="col-lg-12">
    <div class="card-box card-tabs">
        @foreach (['danger', 'warning', 'success', 'info'] as $msg)
            @if(Session::has('alert-' . $msg))
                <p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }}</p>
            @endif
        @endforeach
        <ul class="nav nav-pills">
            <li class="{{$tab==1?'active':''}}">
                <a href="#cardpills-1" data-toggle="tab" aria-expanded="true">Dados Cadastrais</a>
            </li>
            <li class="{{$tab==2?'active':''}}">
                <a href="#cardpills-2" data-toggle="tab" aria-expanded="false">Release</a>
            </li>
            <li class="{{$tab==3?'active':''}}">
                <a href="#cardpills-3" data-toggle="tab" aria-expanded="false">Diferenciais</a>
            </li>
        </ul>
        <div class="tab-content m-t-40">
            <div id="cardpills-1" class="tab-pane fade {{$tab==1?'in active':''}}">
                <div class="row">
                    <div class="col-md-12">
                        <div class="row">
                            <div class="col-md-6">                      
                                <form class="form-horizontal" method="POST" action="{{ route('supplier.photo-update') }}" enctype="multipart/form-data">
                                    {{ csrf_field() }}  
                                    <label for="band_name">Foto do Perfil</label>
                                    <div class="form-group{{ $errors->has('photo') ? ' has-error' : '' }} foto-perfil">
                                        <img src="@if(!empty(Auth::user()->userable->photo)) {{ asset(Auth::user()->userable->photo) }} @else {{ asset('storage/foto.png') }} @endif" class="img-thumbnail m-b-10" alt="profile-image">
                                        <div class="clearfix"></div>
                                        <label class="alterar-foto" for="alterar-foto">Alterar Foto</label>
                                        <input style="display: none" id="alterar-foto" type="file" name="photo" onchange="this.form.submit()">
                                        @if ($errors->has('photo'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('photo') }}</strong>
                                        </span>
                                        @endif
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <form class="form-horizontal" method="POST" action="{{ route('supplier.profile-update') }}">
                            {{ csrf_field() }}
                            <div class="row">                         
                                <div class="col-md-6">
                                    <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="name">Nome</label>
                                            <input id="name" type="text" class="form-control" name="name" value="{{ $user->name }}" required>
                                            @if ($errors->has('name'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('name') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('cpf') ? ' has-error' : '' }}">
                                        <div id="cpf-invalido" class="col-md-9">
                                            <label for="cpf">CPF</label>
                                            <input id="cpf" type="text" class="form-control" name="cpf" value="{{ $supplier->cpf }}">
                                            @if ($errors->has('cpf'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('cpf') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('cnpj') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="cnpj">CNPJ</label>
                                            <input id="cnpj" type="text" class="form-control" name="cnpj" value="{{ $supplier->cnpj }}">
                                            @if ($errors->has('cnpj'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('cnpj') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('fantasy_name') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="fantasy_name">Nome Fantasia</label>
                                            <input id="fantasy_name" type="text" class="form-control" name="fantasy_name" value="{{ $supplier->fantasy_name }}">
                                            @if ($errors->has('fantasy_name'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('fantasy_name') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('legal_name') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="legal_name">Razão Social</label>
                                            <input id="legal_name" type="text" class="form-control" name="legal_name" value="{{ $supplier->legal_name }}">
                                            @if ($errors->has('legal_name'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('legal_name') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('state_registration') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="state_registration">Inscrição Estadual</label>
                                            <input id="state_registration" type="text" class="form-control" name="state_registration" value="{{ $supplier->state_registration }}">
                                            @if ($errors->has('state_registration'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('state_registration') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('zipcode') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="zipcode">CEP</label>
                                            <input id="zipcode" type="text" class="form-control" name="zipcode" value="{{ $supplier->zipcode }}" required>
                                            @if ($errors->has('zipcode'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('zipcode') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('address') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="address">Endereço</label>
                                            <input id="address" type="text" class="form-control" name="address" value="{{ $supplier->address }}" required>
                                            @if ($errors->has('address'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('address') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="number">Número</label>
                                            <input id="number" type="text" class="form-control" name="number" value="{{ $supplier->number }}" required>
                                            @if ($errors->has('number'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('number') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('complement') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="complement">Complemento</label>
                                            <input id="complement" type="text" class="form-control" name="complement" value="{{ $supplier->complement }}">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group{{ $errors->has('neighborhood') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="neighborhood">Bairro</label>
                                            <input id="neighborhood" type="text" class="form-control" name="neighborhood" value="{{ $supplier->neighborhood }}" required>
                                            @if ($errors->has('neighborhood'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('neighborhood') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('city') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="city">Cidade</label>
                                            <input id="city" type="text" class="form-control" name="city" value="{{ $supplier->city }}" required>
                                            @if ($errors->has('city'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('city') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('state') ? ' has-error' : '' }}">                         
                                        <div class="col-md-9">
                                            <label for="state">Estado</label>
                                            <input id="state" type="text" class="form-control" name="state" value="{{ $supplier->state }}" required>
                                            @if ($errors->has('state'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('state') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">                       
                                        <div class="col-md-9">
                                            <label for="email">Email</label>
                                            <input id="email" type="email" class="form-control" name="email" value="{{ $user->email }}" >
                                            @if ($errors->has('email'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('email') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('phone') ? ' has-error' : '' }}">                                         
                                        <div class="col-md-9">
                                            <label for="phone">Telefone</label>
                                            <input id="phone" type="text" class="form-control" name="phone" value="{{ $supplier->phone }}">
                                            @if ($errors->has('phone'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('phone') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>                                      
                                    <div class="form-group{{ $errors->has('cellphone') ? ' has-error' : '' }}">
                                        <div class="col-md-9">
                                            <label for="cellphone">Celular</label>
                                            <input id="cellphone" type="text" class="form-control" name="cellphone" value="{{ $supplier->cellphone }}">
                                            @if ($errors->has('cellphone'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('cellphone') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">                  
                                        <div class="col-md-9">
                                            <label for="password">Nova Senha</label>
                                            <input id="password" type="password" class="form-control" name="password" >
                                            @if ($errors->has('password'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('password') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group">                                            
                                        <div class="col-md-9">
                                            <label for="password-confirm">Confirmar Nova Senha</label>
                                            <input id="password-confirm" type="password" class="form-control" name="password_confirmation" >
                                        </div>
                                    </div>
                                    <input type="hidden" class="form-control" name="userable_type" value="App\Supplier">
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <div class="pull-right m-t-20">
                                        <button type="submit" class="btn btn-primary warning-alert">
                                            Salvar
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div id="cardpills-2" class="tab-pane fade {{$tab==2?'in active':''}}">
                <div class="row">
                    <div class="col-md-12">
                        <form class="form-horizontal" method="POST" action="{{ route('supplier.release-update') }}">
                            {{ csrf_field() }}
                            <div class="row">
                                <div class="col-md-12 m-b-20">
                                    <div class="form-group">
                                        <label for="band_name" class="col-md-6 control-label historia-banda">Insira informações relevantes sobre o seu negócio</label>
                                        <div class="col-md-12">
                                            <textarea id="elm1" name="history">{!!html_entity_decode($supplier->history)!!}</textarea>
                                        </div>
                                    </div>
                                </div>                                                               
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <div class="pull-left m-t-20">
                                        <button type="submit" class="btn btn-primary warning-alert">
                                            Salvar
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div id="cardpills-3" class="tab-pane fade {{$tab==3?'in active':''}}">
                <div class="row">
                    <div class="col-md-9">
                        @foreach($supplier->tags as $tag)
                            <a type="button" class="btn btn-custom btn-rounded w-md waves-effect waves-light m-b-30">{{ $tag->name }}<a class="danger-alert" style="
                                    position: relative;
                                    z-index: 999;
                                    right: 20px;
                                    bottom: 13px;"
                            data-link="{{ route('supplier.tags.destroy', $tag) }}" href="#"> <i class="zmdi zmdi-close-circle"></i></a></a>
                        @endforeach
                        <form class="form-horizontal" method="POST" action="{{ route('supplier.tags.store') }}">
                            {{ csrf_field() }}
                            <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                                <div class="col-md-6">
                                    <label for="name">Cadastre os seu diferenciais</label>
                                    <input id="name" type="text" class="form-control" name="name" required>
                                    @if ($errors->has('name'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('name') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <div class="pull-left m-t-20">
                                        <button type="submit" class="btn btn-primary">
                                            Salvar
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div><!-- end col -->
</div>
@endsection
  • You passed the list again to be loaded?

  • Yes, the list is in the $Supplier variable that I am passing to the view profile after saving the record. I foreach it to retrieve the existing records in the bank.

  • Before implementing to go to the respective tab, after saving I would do a redirect()->back() that went to the first tab, but bringing the differential tab updated.

  • Strange then, because, the variable should not ta returning anything! has to debug there to see what it is! is a local error .

  • I gave a dd($Supplier) before returning the view inside if Else and is bringing all the data correctly.

  • So it’s yours View you have to look for the problem in View .

  • I gave a dd(Supplier->tags) inside the foreach and the data is there tb. Only you need to refresh the page to appear, going to any view and returning to this.

  • it is difficult to reproduce this screen understood... it is complicated have to do the whole process to give an ideal answer... it is this.

  • How can I make a redirect()->back() by passing the variables that need to exist. I used redirect()->back()->with('var', '$var'); but it was not.

  • It has to be https://laravel.com/docs/5.5/session#flash-data gives a read $request->session()->keep(['username', 'email']); if you have to make the recovery there too in the original

  • I found the error, it was on this line: if (!$tags->contains('name', $request->name)) which returns true or false and caused some conflict with if that returns true or false tb.

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.