简体   繁体   中英

How To use Database as a variable with blade template in laravel?

i'm new in laravel, i have some problem to make database as variable to be shown on blade template, example i want to get any data from database :

{{$get = DB::table('perangkat')->get()}}

then:

@foreach ($get as $got)
   {{$got->jabatan}}
@endforeach

if the result of the $got->jabatan is kepala , But i want the result is Change To "Kepala Desa" , How can i do ?? Please Help Mee ...

Write database related stuff in model or controller in a method and pass that value to view. It will be very neat and clear

public function getData(){

    $get = DB::table('perangkat')->get();

    return view('myblade', ['data' => $get]);
}

In your view

@foreach ($data as $got)
   {{$got->jabatan}} Desa
@endforeach

Also can you update where you get Desa .so i can update answer according to that.

Updated you keep different roles in array and do some think like this

 <?php
    $role=['a'=>'operator','b'=>'admin'] ; ?>

      @foreach ($data as $got)
  <?php  $result = isset($role[$got->jabatan]) ? $role[$got->jabatan] : null; ?>
           {{$result}} 
        @endforeach

company.php

function showdata{
$sql= "this syntax sql";
}

//show data
$company = new company();
@foreach ($company->showdata() as $got)
  {{$got->jabatan}}
    @endforeach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM