jeudi 11 octobre 2018

How to retrieve only one record with eager loading in Laravel 5?

I have Eloquent model User with relation languages

class User extends Model {

  // ... some code 

  public function languages()
  {
     return $this->belongsToMany(Language::class);
  }
}

My migration pivot table for ManyToMany relation

public function up()
{
    Schema::create('language_user', function (Blueprint $table) {
        $table->unsignedInteger('user_id');
        $table->unsignedInteger('language_id');
        $table->primary(['user_id', 'language_id']);
    });
}

In the controller I try to get my User

class UserController extends Controller
{
     public function show(User $user)
     {
        return response()->success($user->with('languages')->get());
     }
}

But unfortunately instead of one user, I gets as result array of Users How can I query only one user record with his languages?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire