0

Excuse me, I use laravel ORM have two json arrays , like this:

php

$personnels = Personnel::all();
$skills = Languagelv::all();
return view('bs_sidebar/recommend',[
            'personnels' => $personnels,
            'skills' => $skills,
]);

model

class Personnel extends Model
{
    protected $table = 'personnels';

    protected $fillable = ['name', 'sex'];
}

class Personnels_skill extends Model
{
    public $timestamps = false;
    protected $table = 'personnels_skill';
    protected $fillable = ['skill_name','personnels_id'];
}

json result

{personnels:[
    {"id" : 1,"name"  : "bruce","sex"   : 1},
    {"id" : 2,"name" : "peter","sex"  : 0}
    ]
};
{skill:[
        {"id" : 1,"skill_name": 'php', "personnels_id" : 1},
        {"id" : 2,"skill_name": 'jsp',"personnels_id" : 1},
        {"id" : 3,"skill_name": 'asp',"personnels_id" : 2}
       ]
};

I want to merge two json arrays

(personnels id = skill personnels_id )

skill arrays is into personnels arrays

like this result:

{merge:[
        {"id":1,"name":"bruce","sex:1,"skill":[{"id": 1,"skill_name": 'php', "personnels_id" : 1},{"id": 2,"skill_name": 'jsp',"personnels_id" : 1}]},

        {"id":2,"name":"peter","sex":0,"skill":[{"id": 3,"skill_name": 'asp',"personnels_id" : 2}
        ]
};

How can I do , please help me, thank you.

2
  • show your code then i can help you Commented Mar 29, 2017 at 8:39
  • @lakhvirkumar ok! thank you~! Has been showing php Commented Mar 29, 2017 at 8:46

2 Answers 2

3

If you have your relationships correctly configured in your models, you should be able to do the following:

$personnel = Personnel::with('skills')->get();
return view('bs_sidebar/recommend', compact('personnel'));

Edit Given your models, if you add the following relationships you will be able to use the Eloquent query above.

class Personnel extends Model
{
    protected $table = 'personnels';

    protected $fillable = [
        'name', 
        'sex',
    ];

    public function skills() 
    {
        return $this->hasMany(Personnels_skill::class, 'personnel_id', 'id');
    }
}

class Personnels_skill extends Model
{
    protected $table = 'personnels_skill';

    protected $fillable = [
        'personnel_id',
        'skill_name',
    ];

    public $timestamps = false; 

    public function personnel()
    {
        return $this->belongsTo(Personnel::class, 'id', 'personnel_id');
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank your reply! but I don't know , how can configured models.. i'm try this
@Bruce - update your question with your database schema and model code.
@Bruce - your database schema was relevant to know what your foreign keys were. As it happens, you've included them in your models $fillable array, so no matter.
this is Good practice
0

let's try this it will work.

      foreach ($personnels as $key => $value) {
        $skill_list = DB::table('skill')->where('personnels_id','=',$value->id)->get();
        $personnels[$key]->skill = $skill_list;   
                    }  

return view('bs_sidebar/recommend',['personnels' => $personnels]);

8 Comments

thank you replay , i try this it, but is error [Creating default object from empty value] in $personnels[$key]->skill = $skill_list;
sorry! ok , i'll try this code , but is get error message [Creating default object from empty value] error is in the $personnels[$key]->skill = $skill_list;
i try delete 「->skill」, is error disappear ,but don't get any message
please use this , line no 2 $value['id'] instead of $value->id
can you please print the $value['id'] inside of foreach ,if it is value coming or not
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.