Answer the question
In order to leave comments, you need to log in
How to correctly request data from the model via json?
There is a User(belongsToMany) and Artist model.
The request User::find(1)->artists returns me a list of artists, everything is fine.
But when requesting this data via json:
return response()->json(['success'=>true,'user'=>User::find($user_id)]);
$appends=['artists'];
$user=User::find($user_id);
$user['artists']=$user->artists;
return response()->json(['success'=>true,'user'=>$user]);
Answer the question
In order to leave comments, you need to log in
You can not dance with a tambourine, but read the documentation about relationships , from which you can learn about methods with()
and load()
.
And if you do not hesitate to open the code of the base model, then with = []
you can find out about the property ...
//Первый
$user = User::find($user_id);
//Второй
$user = User::where('id', $user_id)->first();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question