jeudi 26 octobre 2017

Laravel method that can be used by multiple controllers and commands. Where should it be?

I'd like to define a "global" method that can be used by multiple controllers and commands. Where should it be placed in Laravel 5.4?

Let's say I have the following controller. How would I call the "global" method instead, and where would that "global" method be located exactly?

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

use App\Flight;

class FlightsController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Index
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $flights = Flight::where('active', 1)
               ->orderBy('name', 'desc')
               ->take(10)
               ->get();

        foreach ($flights as $flight) {
            if ( $flight->price == 0 )
            {
                $output = "some value";
            }
            else
            {
                $output = "some other value";
            }
        }

        return view('flights.index')
                ->with(['output'   => $output])
                ;
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2iAjY7v
via IFTTT

Aucun commentaire:

Enregistrer un commentaire