I have a database tables Users, Listings Users table:
id, name, email, password
Listings table:
id, title, seller_id
Listing migration:
public function up()
{
Schema::create('listings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->bigInteger('seller_id')->unsigned();
$table->timestamps();
$table->foreign('seller_id')->references('id')->on('users')->onDelete('cascade');
});
}
User model:
public function listings()
{
return $this->hasMany(Listing::class);
}
Listing model:
public function seller()
{
return $this->belongsTo(User::class, 'seller_id', 'id');
}
ListingResource:
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ListingResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
ListingsController:
public function index()
{
return ListingResource::collection(auth()->user()->listings()->latest());
}
I keep getting this error:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'listings.user_id' in 'where clause' (SQL: select * from listings
where listings
.user_id
= 1 and listings
.user_id
is not null order by created_at
desc limit 1)"
How come it returns the query with user_id as the foreign key, even though I specifically put 'seller_id' as the foreign key inside the User.php model?
I have tried to put:
public function listings()
{
return $this->hasMany(Listing::class, 'seller_id');
}
As I have read that this could work, but this generates the following error:
"Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::mapInto()"