Problem with editing in laravel 5.5

0

I have a module to edit, which works very well, I show the controller and the view below.

public function edit(Request $request, $id)
    {
       
            $request->user()->authorizeRoles(['admin','referido']);
            $usuarios=usuarios::find($id);
            return view('usuario.edit',compact('usuarios'));
    

    }

the view.

@extends('layouts.layout')
@section('content')
<div class="row">
	<section class="content">
		<div class="col-md-8 col-md-offset-2">

			@if(Session::has('success'))
			<div class="alert alert-info">
				{{Session::get('success')}}
			</div>
			@endif
 
			<div class="panel panel-default">
				<div class="panel-heading">
					<h3 class="panel-title">Nuevo usuario</h3>
				</div>
				<div class="panel-body">					
					<div class="table-container">

						<form method="POST" action="{{ route('usuario.update',$usuarios->id) }}"  role="form">
							{{ csrf_field() }}
							<input name="_method" type="hidden" value="PATCH">



                             	<div class="row">
								<div class="col-xs-6 col-sm-6 col-md-6">
									<div class="form-group">
										<input type="number" name="id" id="id" class="form-control input-sm" placeholder="id" value="{{{ $usuarios->id }}}" readonly="readonly" style="visibility:hidden">
									</div>
								</div>
						
                          </div>


							<div class="row">
								<div class="col-xs-6 col-sm-6 col-md-6">
									<div class="form-group{{ $errors->has('nombre') ? ' has-error' : '' }}">
										<input type="text" name="nombre" id="nombre" class="form-control input-sm" value="{{$usuarios->nombre}}">
									</div>
								</div>
								<div class="col-xs-6 col-sm-6 col-md-6">
									<div class="form-group{{ $errors->has('apellido') ? ' has-error' : '' }}">
										<input type="text" name="apellido" id="apellido" class="form-control input-sm" value="{{$usuarios->apellido}}">
									</div>
								</div>
							</div>


                            <div class="row">
							   <div class="col-xs-6 col-sm-6 col-md-6">
							      <div class="form-group{{ $errors->has('telefonos') ? ' has-error' : '' }}">
								    <input type="number" name="telefonos" id="telefonos" min="1111111" max="9999999999" class="form-control input-sm" value="{{$usuarios->telefonos}}">
								       @if ($errors->has('telefonos'))
                                         <span class="help-block">
                                             <strong>{{ $errors->first('telefono') }}</strong>
                                         </span>
                                        @endif
							    </div>
							 </div>
							<div class="col-xs-6 col-sm-6 col-md-6">
								<div class="form-group{{ $errors->has('cedula') ? ' has-error' : '' }}">
										<input type="number" name="cedula" id="cedula" min="1111111" max="9999999999" class="form-control input-sm" value="{{$usuarios->cedula}}">
										   @if ($errors->has('cedula'))
                                              <span class="help-block">
                                                    <strong>{{ $errors->first('cedula') }}</strong>
                                              </span>
                                           @endif
									 </div>
								  </div>
                               </div>


                            <div class="row">
							   <div class="col-xs-6 col-sm-6 col-md-6">
									<div class="form-group">
										<input type="text" name="barrio" id="barrio" class="form-control input-sm" value="{{$usuarios->barrio}}">
									</div>
								</div>
								<div class="col-xs-6 col-sm-6 col-md-6">
									<div class="form-group">
										<input type="text" name="lugar_votacion" id="lugar_votacion" class="form-control input-sm" value="{{$usuarios->lugar_votacion}}">
									</div>
								</div>
							</div>

							<div class="row">
								<div class="col-xs-6 col-sm-6 col-md-6">
									<div class="form-group">
										<input type="text" name="mesa_votacion" id="mesa_votacion" class="form-control input-sm" value="{{$usuarios->mesa_votacion}}">
									</div>
								</div>
								<div class="col-xs-6 col-sm-6 col-md-6">
									<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
										<input type="email" name="email" id="email" class="form-control input-sm" value="{{$usuarios->email}}">
										@if ($errors->has('email'))
                                              <span class="help-block">
                                                    <strong>{{ $errors->first('email') }}</strong>
                                              </span>
                                           @endif
									</div>
								</div>
							</div>


                           <div class="row">
							  <div class="col-xs-6 col-sm-6 col-md-6">
									<div class="form-group"> 

                                        <select name="ciudad" id="ciudad" class="form-control input-sm" value="{{$usuarios->ciudad}}">
                                           <option value="{{$usuarios->ciudad}}">{{$usuarios->ciudad}}</option>
                                           <option value="Pereira">Pereira</option>
                                           <option value="Apía">Apía</option>
                                           <option value="Balboa">Balboa</option>
                                           <option value="Belén de Umbría">Belén de Umbría</option>
                                           <option value="Dosquebradas">Dosquebradas</option>
                                           <option value="Guática">Guática</option>
                                           <option value="La Celia">La Celia</option>
                                           <option value="La Virginia">La Virginia</option>
                                           <option value="Marsella">Marsella</option>
                                           <option value="Mistrató">Mistrató</option>
                                           <option value="Pueblo Rico">Pueblo Rico</option>
                                           <option value="Quinchía">Quinchía</option>
                                           <option value="Santa Rosa de Cabal">Santa Rosa de Cabal</option>
                                           <option value="Santuario">Santuario</option>
                                        </select>

									</div>
								</div>
							<div class="col-xs-6 col-sm-6 col-md-6">
									<div class="form-group">
										<input type="text" name="direccion" id="direccion" class="form-control input-sm" value="{{$usuarios->direccion}}">
									</div>
								</div>
							</div>

							<div class="row">
 
								<div class="col-xs-12 col-sm-12 col-md-12">
									<input type="submit"  value="Actualizar" class="btn btn-success btn-block">
								</div>	
 
							</div>
						</form>
					</div>
				</div>
 
			</div>
		</div>
	</section>
	@endsection

The problem comes when I make the next change in the controller.

  public function edit(Request $request, $id)
    {
       
        
            $request->user()->authorizeRoles(['admin','referido']);
            $filtro = \Auth::user()->id;
            $usuarios=usuarios::where('id',$id)->where('user_id',$filtro)->get();
            return view('usuario.edit',compact('usuarios'));
    

    }

The only thing I want to achieve is that those who log in can see the records of it and not another, the query is fine, the problem is in the view, because I see this error.

  

Property [id] does not exist on this collection instance. (View:   C: \ laragon \ www \ stsroles \ resources \ views \ User \ edit.blade.php)

I do not know why, how can I adapt this, so that the view reads the information as it happens with find ()?

    
asked by zereft 29.12.2018 в 19:24
source

2 answers

0

The error is in the get () because it returns an array of objects, you should try first () which if it returns a single object.

$usuarios=usuarios::where('id',$id)->where('user_id',$filtro)->first();

It is not the solution to the logic problem but to the implementation one.

    
answered by 29.12.2018 / 21:19
source
1

The accepted answer resolves what has been raised, but it is not the solution to the logic problem that the OP has.

There is not much logic when doing a check in this way in the controller, it should be BEFORE you get there, and generate a message, why?

The reasons are very simple:

  • It should not be the responsibility of the controller to verify if he has permission or not to edit.
  • It is not necessary to carry out a supposed revision of permits obtaining the model and not making a comparison until reaching the view (this logic is not the responsibility of the view either).

Appropriate solutions in my opinion:

  • Take advantage of Laravel policies .
  • Using the middleware from Laravel, they really work very well for simple checks if you do not want to address the possible complexity of the policies .
  • Use Model Binding to convert the id to its respective model in advance, we could say that it is the Laravel standard for these cases of using known models in a controller.
answered by 31.12.2018 в 06:32