Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
How to use traits in Laravel 5.4.18?

I need an example of where to exactly create the file, to write to it, and how to use the functions declared in the trait. I use Laravel Framework 5.4.18

-I have not altered any folder in the framework, everything is where it corresponds-

From already thank you very much.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I have Create a Traits directory in my Http directory with a Trait called BrandsTrait.php

and use it like:

use App\Http\Traits\BrandsTrait;

class YourController extends Controller {

    use BrandsTrait;

    public function addProduct() {

        //$brands = Brand::all();

        // $brands = $this->BrandsTrait();  // this is wrong
        $brands = $this->brandsAll();
    }
}

Here is my BrandsTrait.php

<?php
namespace App\Http\Traits;

use App\Brand;

trait BrandsTrait {
    public function brandsAll() {
        // Get all the brands from the Brands Table.
        $brands = Brand::all();

        return $brands;
    }
}

Note: Just like a normal function written in a certain namespace, you can use traits as well

over 4 years ago · Santiago Trujillo Relatório

0

Trait description:

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way which reduces complexity and avoids the typical problems associated with multiple inheritance and Mixins.

The solution

Make a directory in your App, named Traits

Create your own trait in Traits directory ( file: Sample.php ):

<?php

namespace App\Traits;

trait Sample
{
    function testMethod()
    {
        echo 'test method';
    }
}

Then use it in your own controller:

<?php
namespace App\Http\Controllers;

use App\Traits\Sample;

class MyController {
    use Sample;
}

Now the MyController class has the testMethod method inside.

You can change the behavior of trait methods by overriding them it in MyController class:

<?php
namespace App\Http\Controllers;

use App\Traits\Sample;

class MyController {
    use Sample;

    function testMethod()
    {
        echo 'new test method';
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

Let's look at an example trait:

namespace App\Traits;

trait SampleTrait
{
    public function addTwoNumbers($a,$b)
    {
        $c=$a+$b;
        echo $c;
        dd($this)
    }
}

Then in another class, just import the trait and use the function with this as if that function was in the local scope of that class:

<?php

namespace App\ExampleCode;

use App\Traits\SampleTrait;

class JustAClass
{
    use SampleTrait;
    public function __construct()
    {
        $this->addTwoNumbers(5,10);
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda