melx's avatar
Level 4

Display Data in a blade

I have two table which are related with FK

Tb1 has contain this data

                            id           UnitName
                            1              7676768
                            2              6768788
                            3              5657780
                            4              6724353

Tb2 has contain this data

                      unitId           Unit_tag_id
                       2                       1,3
                       4                       null

How can i display such a data 1,3 in a blade, assume i have made relation in model of Tb2 that means unitId has belongs to

how my controller will be like and how blade also be like

0 likes
5 replies
tykus's avatar

You are not describing very well exactly how these two tables are related. Is the comma-separated data in unit_tag_id referencing the id in tbl 1?

tykus's avatar

Ouch! Any reason that you cannot use a pivot table and a belongsToMany relationship?

melx's avatar
Level 4

ok, I have two different items/device and those items there is the master and slave so i decided to make one table which records the items type(brand) and device table records all device number but i differentiate by unit type example 1-master and 2-slave but brand name of this master or slave you can choose.

     class Device extends Model
    {
    protected $fillable = [ 'Devicenumber','devicetype','devicebrand'];

           public function deviceType() {

	return $this->belongsTo('App\DeviceType', 'devicebrand', 'id');
      }



          class DeviceType extends Model
        {

           protected $fillable = [ 'deviceTypeName','deviceModel'];
          }



              class Issue_to_sale extends Model
         {
         //

      protected $fillable=['unit_id','unit_tag_id','team_id','issued_by','req_by','purpose','ack_status'];
    public function team() {

	return $this->belongsTo('App\Team', 'team_id', 'id');
  }

     public function deviceNumb() {

	return $this->belongsTo('App\Device', 'unit_id', 'id');
   }

I put three model above ,

In case your what more i can explain to you via zoom and get me the advice as well

melx's avatar
melx
OP
Best Answer
Level 4

i get the answer

   $receiveDevice=Issue_to_sale::with('deviceNumb')->where([
               ['team_id',$userId],
               ['ack_status','=',0]
                ]) 
      //starting here
          ->leftJoin("devices as devices",\DB::raw("FIND_IN_SET(devices.id,issue_to_sales.unit_tag_id)"),">",\DB::raw("'0'"))
            ->groupBy('issue_to_sales.unit_id')
            ->get(['issue_to_sales.*',
              \DB::raw("group_concat(devices.Devicenumber SEPARATOR ', ') as name")
             ]);

Please or to participate in this conversation.