Safe and clean code in laravel 5.1

Actually, I have a question about clean code,

I'm trying to get some value in a blade server file , I get confused between the two approaches, I think both of them are right, but I need to know who is clean and protected more by reason

The first approach in my blade directly using Eloquent

@foreach
    (Auth::user()->company->country->cities as $city) {{$city->name}}
@endforeach

The second approach using Injecting Services, create this method in my model and use it in my client using laravel 5.1 Injecting Services

public function getCity()
{
    foreach(Auth::user()->company->country->cities as $city) {
        return $city->name ;
      // OR 
        return $city ;  
          //  i think this is one of benefits to use this approach   
          //  because in my view i can  use getCity()->id or getCity()->name
    }
}

Thank you for your time.

+4
source share
4 answers

, :

MVC, : . - . , , - ,

, , . :

:

public function index()
{
    //get data from model
    $cities = Auth::user()->company->country->cities; 

    //pass the data to the view 
    return View::make('your_view', ['cities' => $cities] );
}

, :

@foreach ($cities as $city)
     {{$city->name}}
@endforeach
+1

, , ( ). , , .

, , :

@foreach($serviceName->getCities() as $city)
    {{ $city->name }}
@endforeach

, , . ​​ , .

: . {{}}. XSS.

+4

, MVC

 public function getCities()
 {
//in your model model
   return $cities = Auth::user()->company->country->cities; 
 }

 public function index()
{
     //call return getCities(); 
}

 //finally in your view loop over $cities 
  @foreach ($cities as $city)
    {{$city->name}}
  @endforeach
+2

, ,

foreach(Auth::user()->cities as $city)
{
   {!! $city->whatever !!}
}
0

Source: https://habr.com/ru/post/1614028/


All Articles