How to use variables in view in laravel 5

2

I'm developing a project of "borrowing" things from a company and I'm locked in a key module. So I do not have much experience in laravel, so I came across the following situation:

Database - I have a LOCAL table that correlates with multiple keys. (a location needs one or more keys to be opened) - I have a table TYPE_CHAVE . (eg standard type 1, standard 2 and etc.) - I have a LOCAL_TIPO_CHAVE table linking LOCAL to KEY_TYPE

So far so good, ready all the right places, the problem happens when I will relate the keys to this location.

Controller (LocalController)

public function index()
{
    $localchaves = \App\LocalChave::orderBy('id_local');
    $locais = \App\Local::orderBy('id_local')->get();
    return view('site.index', compact('locais', 'localchaves '));
}

View (index.blade.php

...
@if($locais)
    @foreach($locais as $local)
        {{$strchave = ""}}
        {{$chaveslocal = $localchaves->where('id_local', '=', $local->id_local)->get()}};
        @foreach($chaveslocal as $chavelocal)
            {{$strchave = '<br>' + $chavelocal->tipochave->descricao}};
        @endforeach
    @endforeach
@endif
...
<a href='/local/chave' onMouseOver="toolTip('Clique para Editar as Chaves {{$strchave}}')" onMouseOut="toolTip()">Chave(s)</a>
...

The variable does not receive the correct value and the values inside the {{}} keys appear as an echo.

Ex. in the middle of the page appears

$ strchave=""; [{"key_id": "1", "created_at": "2016-06-16 17:32:00", "updated_at": "2016-06-16 17:32:00", "key_id": "1 "," local_id ":" 1 "}]; $ strchave = ' '+ Mul-t-lock M1;

The value I want the $ strchave variable to have is, for example

'<br> Padrão 1 <br> Padrão 2'

I tried searching in various ways on google, but I'm not finding the right terms for this search.

    
asked by anonymous 20.06.2016 / 23:53

1 answer

1

Before answering your question, I have some suggestions to make your code better and cleaner.

Use English as the base language

It is very important that any statement / assignment is in English, which is why Laravel performs some automatic conversions when necessary, especially within its tables and migrations.

For example, if you create the user template and do not declare the table property, Laravel automatically assumes that the table attached to this template is the plural of its name, in this case, users .

Try to include your classes at the top of your file, after the namespace declaration and before the class.

Do not get confused, use within your class is reserved for traits, I recommend a read about the subject .

This code

<?php

namespace app\Http\Controllers;

use App\User;

class UserController extends Controller {
    public function index() {
        $users = User::all();

        return view('user.index', compact('users'));
    }

    public function show(Request $request, $id) {
        $user = User::findOrFail($id);

        return view('user.show', compact('user'));
    }
}

It's better than this:

<?php

namespace app\Http\Controllers;

class UserController extends Controller {
    public function index() {
        $users = App\User::all();

        return view('user.index', compact('users'));
    }

    public function show(Request $request, $id) {
        $user = App\User::findOrFail($id);

        return view('user.show', compact('user'));
    }
}

It may not look like a small piece of code like this, but on a large scale if you are going to always include the full namespace of your classes every time you use them, this is going to be a disaster.

Make the relationships using eloquent model

I'm going to try to fit an example with the data you gave me in the question, but if there's any question left I would advise you to read the documentation official , is in English but reading is simple.

Considering that the place template corresponds to the local template, and keytype corresponds to tipo_chave :

in the app \ Place.php file

<?php

...

class Place extends Model
{
    ...

    public function keyTypes()
    {
        return $this->belongsToMany(KeyType::class);
    }

    ...
}

in the app \ KeyType.php file

<?php

...

class KeyType extends Model
{
    ...

    public function places()
    {
        return $this->belongsToMany(Place::class);
    }

    ...
}

If the relation is not explicit, to mount it the laravel will use the following rules:

  • The name of the two tables in the singular (one more reason to use the name of the classes in English).
  • In alphabetical order.

Now you can get all the keys of a place using your relation, for example:

// busca todos os locais, junto com os locais busca
// suas chaves também.
$places = Place::with('keyTypes')->get();

foreach ($places as $place) {
    // chaves de um local específico dentro do loop.
    $keys = $place->keyTypes;
}

...

In this case, the pivot table would be named key_type_place . Here's an example of your html file after the ready relations within the templates:

@foreach($places as $place)
    @foreach($place->keyTypes as $keyType)
        <a href='/local/chave'
            onMouseOver="toolTip('Clique para Editar as Chaves {{ $keyType->description }}')"
            onMouseOut="toolTip()"
        >
            Chave(s)
        </a>
    @endforeach
@endforeach
    
21.06.2016 / 03:39