I have a table called addresses, it has a column name user_ids
which is an array of the users who have that same address.
When I request to /api/addresses
what it returns is {id:1,name:"lorem", user_ids:[1,2]}
. I want it to return the users instead of their ids
this is the addresses model
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Address extends Model
{
use HasFactory;
protected $fillable = [
'coordinates',
'title',
'description',
'user_ids',
];
protected $appends = ['user_ids'];
protected $casts = [
'user_ids' => 'array',
];
public function users()
{
return $this->belongsToMany(User::class,"addresses");
}
}
this is the create_table_addresses
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('addresses', function (Blueprint $table) {
$table->id();
$table->string('coordinates');
$table->string('title');
$table->string('description');
$table->json("user_ids");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('addresses');
}
};
addresses controller
<?php
namespace AppHttpControllers;
use AppHttpRequestsStoreAddressRequest;
use AppHttpRequestsUpdateAddressRequest;
use AppModelsAddress;
class AddressController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
return Address::with('users')->get();
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param AppHttpRequestsStoreAddressRequest $request
* @return IlluminateHttpResponse
*/
public function store(StoreAddressRequest $request)
{
$address = Address::create($request->validated());
return response()->json($address, 201);
}
/**
* Display the specified resource.
*
* @param AppModelsAddress $address
* @return IlluminateHttpResponse
*/
public function show(Address $address)
{
return $address;
}
/**
* Show the form for editing the specified resource.
*
* @param AppModelsAddress $address
* @return IlluminateHttpResponse
*/
public function edit(Address $address)
{
//
}
/**
* Update the specified resource in storage.
*
* @param AppHttpRequestsUpdateAddressRequest $request
* @param AppModelsAddress $address
* @return IlluminateHttpResponse
*/
public function update(UpdateAddressRequest $request, Address $address)
{
$address->update($request->validated());
return response()->json($address, 200);
}
/**
* Remove the specified resource from storage.
*
* @param AppModelsAddress $address
* @return IlluminateHttpResponse
*/
public function destroy(Address $address)
{
$address->delete();
return response()->json(null, 204);
}
}
2
Answers
I am assuming that the response comes from a controller so then where you build you response, you need to append the user information.
Maybe something like
If you post the controller, then I can tell you exactly where to put this or how to do it better.
My suggestion would be to use the database for this (since you’re already using the
with('addresses')
).(Documentation for that relationship starts here)
Migration:
User model:
Address model:
Now you can use the relationship your application: