How to use view variables in Laravel 5

Asked

Viewed 5,249 times

2

I am developing a project of "loans" of things of a company and I am locked in a module of keys. So I don’t have much experience in the Laravel, so I came across the following situation:

Database - I have a table LOCAL that correlates with several keys. ( a location needs one or more keys to open ) - I have a table TIPO_CHAVE. (ex. standard type 1, standard 2 and so on) - I have a table LOCAL_TIPO_CHAVE connecting the LOCAL at the TIPO_CHAVE

So far so good, I list all the places right, the problem happens when I relate the keys to this site.

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 that are inside the {{}} keys appear as an echo.

Ex. in the middle of the page appears

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

The value I wish the variable $strchave possess is for example

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

I tried to google several ways, but I’m not finding the right terms for this search.

1 answer

1

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

Use English as the base language

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

For example, if you create the model user and does not declare ownership table, Laravel automatically assumes that the table linked 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.

Don’t get confused, the use within its class is reserved for traits, recommend a reading on 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’re going to always include the full namespace of your classes every time you use them, it’s going to be a disaster.

Make relationships using the eloquent model

I will try to fit an example with the data you gave me in the question, but if there is still any doubt I advise you to read the official documentation, is in English but the reading is simple.

Taking into account that the model place corresponds to the model local, and keytype corresponds to tipo_chave:

in the Place.php app file

<?php

...

class Place extends Model
{
    ...

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

    ...
}

in the Keytype.php app file

<?php

...

class KeyType extends Model
{
    ...

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

    ...
}

If the relation is not explicit, to assemble it the Standard will use the following rules:

  • The name of the two tables in the singular (another reason to use the name of the classes in English).
  • Alphabetically.

Now you can take all the keys of a location using its interface, 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 after key_type_place. See 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

  • Very good, I’ll test you this way. I end up prioritizing development and do not stop to read, small details that are in the manual end up solving big things. Thanks for the tips!

Browser other questions tagged

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