Mohd Asyraf
Mohd Asyraf

Reputation: 45

Get data from database with condition and show it in a view

This is the data that I have in database

This is what I want to make in the view.blade.php

What I want to do is I want to get the data from the database, if the data inside the column is 1, I want to get the column name as you can see in image 2, but there could be more than 1 column name because the column with data can be column A,B,C... etc.. and I want to show the student name and the subject (a,b,c... etc) if the data in it is '1' in the view. I stuck on how to get all those subject A,B,C.. this is the code that I have written, but it is incomplete because I don't know what to add on it to make it as what I have mentioned above. Hopefully, someone can help me. Thanks in advance

if($row->'A'=='1'){i dont know what should i put here so that i cant get the column name 'A' and print it in view.blade.php}

Upvotes: 0

Views: 3115

Answers (2)

Vipertecpro
Vipertecpro

Reputation: 3274

Okay i tried it bit different way it's not proper but it will give you desired output you want :-

Route::get('/test',function(){
    $query = TestTable::all();
    $calculateData = [];
    foreach ($query as $key){
        $subjects = '';
        if($key->A === 1){
            $subjects .= 'A';
        }
        if($key->B === 1){
            $subjects .= 'B';
        }
        if($key->C === 1){
            $subjects .= 'C';
        }
        $calculateData[] = [$key->name,$subjects];
    }
    foreach ($calculateData as $key){
        dump("NAME : " . $key[0]."Subject : " . $key[1]);
    }
    dd("STOP");
})->name('test');

Upvotes: 0

Mihir Bhende
Mihir Bhende

Reputation: 9045

Assuming your table in database is student_details, create an eloquent model StudentDetail inside app/models/StudentDetail.php :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class StudentDetail extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'student_details';

    /**
     * Get subjects in the view
     * 
     * @return string
     */
    public function getSubjects()
    {
        $subjects = [];
        $subjects[] = $this->A == 1 ? 'A' : null;
        $subjects[] = $this->B == 1 ? 'B' : null;
        $subjects[] = $this->C == 1 ? 'C' : null;
        $subjects = array_filter($subjects);

        return implode(',', $subjects);
    }

}

Then you can retrieve data in your controller :

public function view()
{
    $studentDetails = StudentDetail::get();

    return view('view.path', compact('studentDetails'));
}

And inside view you can do :

@foreach($studentDetails as $detail)

    {{ $detail->name }} : {{ $detail->getSubjects() }}
@endforeach

You can use appended property as well, I have not used that because appended property is added every-time when model is instantiated. I believe having it in a function makes it flexible to use as and when needed.

Upvotes: 1

Related Questions