Home > Enterprise >  How to remove array in blade - Laravel
How to remove array in blade - Laravel

Time:08-14

i am calling a column from different table to display it in the current table like showing below:

DB::table('tbl_users')
->where('id', $complaints->user_id)
->limit(1)
->get('phone');

and this is the current output:

[{"phone":"0123456789"}]

how can i achieve the output to be like this:

0123456789

i tried to encode/decode and tried to wrap the whole query with json but none of that worked

UPDATE:

controller:

public function index()
{


    $complaint = DB::table('complaint')->get();
    $phone = DB::table('tbl_users')
->where('id', $complaint->user_id)
->select('phone')
->first();

    return view('admin.complaints',compact('complaint','phone'));
}

Blade:

@foreach ($complaint as $complaints)
     <tr>
     <th style="padding: 20px">{{ $complaints->id }}</th>
     <th style="padding: 20px">{{ $complaints->createdDate }}</th>
     <th style="padding: 20px">New</th>
     <th style="padding: 20px">{{ $complaints->user_id }}</th>
     <th style="padding: 20px">{{$phone->phone}}</th>
     </tr>
@endforeach

CodePudding user response:

Since you need only one record , so you can use first() method instead of limit() and get().get() method will return multidimensional array.

    $user = DB::table('tbl_users')
    ->where('id', $complaints->user_id)
    ->select('phone')
    ->first();

   return view('urBladeFileName', compact('user'));

and in your view file

{{$user->phone}}

Updated

use join query

$complaints = DB::table('complaint')
    ->select(['complaint.id','complaint.createdDate','complaint.user_id','complaint.createdDate','tbl_users.phone'])
    ->join('tbl_users', 'complaint.user_id', '=', 'tbl_users.id')
    ->get();

return view('admin.complaints',compact('complaints'));

and in your view

  @foreach ($complaints as $complaint)
     <tr>
     <th style="padding: 20px">{{ $complaint->id }}</th>
     <th style="padding: 20px">{{ $complaint->createdDate }}</th>
     <th style="padding: 20px">New</th>
     <th style="padding: 20px">{{ $complaint->user_id }}</th>
     <th style="padding: 20px">{{$complaint->phone}}</th>
     </tr>
    @endforeach
  • Related