-1

I have 2 two tables: one is an admission and the other is a class table. I am saving class id in admission class field of admission table by json_encode method.

My controller

public function store(Request $request)
{
         $inputs = $request->all();

    $admission = new Admission;
    $admission->school_id = Auth::User()->id;
    $admission->admission_classes=json_encode($inputs['admission_classes']);
    $admission->save();   

}

My index function

public function index(Request $request) {

    $school_id= Auth::user()->id;

    $admissions= Admission::where('school_id',$school_id)->orderBy('id','desc')->paginate(10);
    return view('frontend.index',compact('admissions','active_class'));

}

My view

@foreach($admissions as $i => $admission)

  {{ $admission->admission_classes }}

@endforeach

I am getting data in this way:-

["1","2","4","5","6","7","8","9"]   

But I want to get in this format:

Nursery,Class 1, Class2, Class3 etc

My class controller
class Classes extends Authenticatable
{
    use EntrustUserTrait;
    use Billable;
    use Messagable;
  
    protected $fillable = [
 'name','status'
    ];
}
ravi
  • 81
  • 1
  • 2
  • 9

2 Answers2

0

The best way to achieve this would be to have a one-to-many relationship with App\Classes.

However, since you already have something up and running, I would probably do it like this.

First, I would cast admission_classes to an array. This makes sure that admission_classes will always be casted to an array whenever it is fetched. It makes it easier for us to work with it.

protected $casts = [
    'admission_classes' => 'array'
]; 

Finally, while fetching your admission records, you would also need to map over it and hydrate the Classes from its ids. This is how I'd try to achieve it.

$admissions = Admission::where('school_id',$school_id)
    ->orderBy('id','desc')
    ->paginate(10)
    ->map(function($admission) {
        return array_map(function($class) {
            $class = Classes::find($class); 
            return isset($class) ? $class->name : ''; 
        }, $admission->admission_classes); 
    });

You will notice that I wrapped the Classes::find() method into the optional() helper. This is because, in case, a record is not found, it will not fail.

Finally, to print your class names in your blade, you would do something like this:

implode(',', $admission->admission_classes); 
Mozammil
  • 8,520
  • 15
  • 29
  • done as you said but this error is coming Call to undefined method Illuminate\Database\Query\Builder::map() – ravi Jan 11 '19 at 09:21
  • Forgot that there was a paginate at the end there. Edited my answer, could you give this a try? – Mozammil Jan 11 '19 at 16:36
  • Can i have your email address sir, i need your help please sir – ravi Jan 12 '19 at 09:07
  • $admissions = Admission::where('school_id',$school_id) ->orderBy('id','desc') ->paginate(10) ->map(function($admission) { return array_map(function($class) { return optional(Classes::find($class))->name; }, $admission->admission_classes); }); this is the error Call to undefined function App\Http\Controllers\optional() – ravi Jan 12 '19 at 12:31
  • `optional()` was added in Laravel 5.6. You might be running an earlier version, in which case, you can try my updated code. – Mozammil Jan 12 '19 at 13:09
0

You need to save integer value in as json array and do the following code

$integerIDs = array_map('intval',  $inputs['admission_classes']);
 $admission->admission_classes= json_encode($integerIDs);   

 public function index(Request $request){
    $admissions = DB::select('SELECT a.*, GROUP_CONCAT(c.name) as classes FROM academy as a LEFT JOIN class c ON JSON_CONTAINS(a.classes, CAST(c.id as JSON), '$') WHERE a.school_id =2 GROUP BY a.id');

    $admissions = $this->arrayPaginator($admissions, $request);

    return view('frontend.index',compact('admissions','active_class'));
 }

 public function arrayPaginator($array, $request)
 {
       $page = Input::get('page', 1);
       $perPage = 10;
       $offset = ($page * $perPage) - $perPage;
       return new LengthAwarePaginator(array_slice($array, $offset, 
         $perPage, true), count($array), $perPage, $page,
            ['path' => $request->url(), 'query' => $request->query()]);
  }

I have not checked the code hope this will help u to continue.......

Shibon
  • 1,552
  • 2
  • 9
  • 20
  • i dont understand sir the way you sent me.The way i had done is incorrect , if yes then please guide me – ravi Jan 11 '19 at 16:22