Difficulties with foreign codes

0

You see, I have a User table with these variables:

Schema::create('users', function(Blueprint $table){
        $table->increments('id');
        $table->string('name');
        $table->string('second_name')->nullable();
        $table->string('provincia')->nullable();
        $table->string('localidad')->nullable();
        $table->string('direccion')->nullable();
        $table->string('telefono');
        $table->date('fecha_nacimiento');
        $table->string('dni');
        $table->boolean('vehiculo')->default(false);
        $table->string('foto')->default('anonimo.jpg');
        $table->boolean('activado')->default(false); // Se marca aqui como falso y ya se pone automaticamente así al hacer el formulario.
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

And a table with the name Inscribe, which I use to indicate that a user has signed up for a job offer:

    Schema::create('inscribes', function (Blueprint $table){
        $table->increments('id');
        $table->unsignedInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users');
        $table->unsignedInteger('oferta_id');
        $table->foreign('oferta_id')->references('id')->on('ofertas');
        $table->boolean('seleccionado')->default(false);
        $table->timestamps();
    });

The Inscribe table is related to User through this relationship:

public function usuario(){
    return $this->belongsTo(User::class);
}

And here I have the view with the data of the user's registration, which I call $ i.

<div class="card card-01">
    <img class="card-img-top" src="{{url($i->usuario()->ruta())}}" alt="{{$i->usuario()->name}}"/>
    <div class="card-body">
        <h2 class="card-title text-center">{{$i->usuario()->name}} {{$i->usuario()->second_name}}</h2>
        <span class="badge badge-danger badge-cat">DNI: {{ $i->usuario()->dni }}</span>
        <span class="badge badge-danger badge-cat">Telefono: {{ $i->usuario()->telefono }}</span>
        <hr>
        @if(!$i->seleccionado)
            <a href="{{ url('/inscrito_seleccion/'.$i->id) }}" class="btn btn-course btn-block">Confirmar contratación</a>
            <a href="{{ url('/eliminar_seleccion/'.$i->id) }}" class="btn btn-warning btn-block">Denegar contratación</a>
        @else
            <p class="btn btn-success btn-block">El usuario fue seleccionado para este trabajo</a>
        @endif
    </div>
</div>

But I get this:

I also get an error message for things like $i->usuario()->id . How do I access the user from the registration?

More data: This is the function that summons the view:

public function elegidos(Oferta $oferta){
        $inscritos=Inscribe::all()->where('oferta_id',$oferta->id);

        if(count($inscritos))
            return view('listas.inscritos',compact('inscritos','oferta'));
        else
            return back()->with('message',['danger','Por ahora nadie se ha inscrito a esta oferta de trabajo']);
    }

And before reaching this view, you have to go through this one:

@extends('layouts.app')
@section('content')
<h1 class="text-center text-mute"><u>Usuarios inscritos a {{$oferta->titulo}}:</u></h1>
    <div class="pl-5 pr-5">
        <div class="row justify-content-center">
            @forelse($inscritos as $i)
                    <div class="col-md-3">
                        @include('listas.interno.datos_inscripcion')
                    </div>
            @empty
                <div class="alert alert-danger">
                    <h1>No hay nadie inscrito a esta oferta actualmente</h1>
                </div>
            @endforelse
        </div>
    </div>
@endsection

By the way, I discovered this. This is the migration of the table Oferta.php:

    Schema::create('ofertas', function (Blueprint $table){
        $table->increments('id');
        $table->string('titulo');
        $table->text('descripcion');
        $table->string('empresa');
        $table->string('sector');
        $table->date('fecha_limite');
        $table->timestamps();
    });

And this is the function with which the Inscribe table relates to Offer:

public function oferta(){
    return $this->belongsTo(Oferta::class);
}

If I put {{$i->oferta->titulo}} , everything goes well.

But if I try to write {{$i->usuario->name}} :

Now it does not work.

    
asked by Miguel Alparez 10.05.2018 в 16:46
source

1 answer

0

I have found a "solution".

This is the new feature that links Register with User:

public function user(){
    return $this->belongsTo(User::class);
}

I adapt the view to the new function:

<div class="card card-01">
    <img class="card-img-top" src="{{url($i->user->ruta())}}" alt="{{$i->user->name}}"/>
    <div class="card-body">
        <h2 class="card-title text-center">{{$i->user->name}} {{$i->user->second_name}}</h2>
        <span class="badge badge-danger badge-cat">DNI: {{ $i->user->dni }}</span>
        <span class="badge badge-danger badge-cat">Telefono: {{ $i->user->telefono }}</span>
        <hr>
        @if(!$i->seleccionado)
            <a href="{{ url('/inscrito_seleccion/'.$i->id) }}" class="btn btn-course btn-block">Admitir al usuario en el trabajo</a>
            <a href="{{ url('/inscrito_eliminar/'.$i->id) }}" class="btn btn-warning btn-block">Rechazar al usuario</a>
        @else
            <p class="btn btn-success btn-block">El usuario ha sido admitido en el trabajo</p>
        @endif
    </div>
</div>

Go with this capricious program! Is there any way to give the function to link the foreign key the name you want?

    
answered by 10.05.2018 в 21:18