I have 2 tables clients and maintenance_packages and I'm trying to list all clients and their maintenance package but keep getting the following error.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'maintenance_packages.client_id' in 'where clause' (SQL: select * from `maintenance_packages` where `maintenance_packages`.`client_id` = 2 and `maintenance_packages`.`client_id` is not null limit 1)
I'm guessing it the way I've set up the relationships? Below is my DB Migrations, Models & Controller.
Clients DB
public function up()
{
Schema::create('clients', function(Blueprint $table) {
$table->increments('id');
$table->string('name', 55);
$table->string('email', 100);
$table->integer('maintenance_id')->unsigned();
});
}
Maintenance DB
public function up()
{
Schema::create('maintenance_packages', function(Blueprint $table) {
$table->increments('id');
$table->string('name', 55);
$table->integer('hours');
$table->decimal('price');
});
}
Client Model
namespace App;
use App\MaintenancePackage;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
protected $table = 'clients';
public $timestamps = false;
public function maintenance()
{
return $this->hasOne(MaintenancePackage::class);
}
}
Maintenance Model
namespace App;
use App\Client;
use Illuminate\Database\Eloquent\Model;
class MaintenancePackage extends Model
{
protected $table = 'maintenance_packages';
public $timestamps = false;
public function client()
{
return $this->belongsTo(Client::class);
}
}
Controller
public function index()
{
$clients = Client::with('maintenance')->get();
return view('clients.index', compact('clients', 'maintenance'));
}
Any help would be great.
client
belongsToOnepackage
... – tuytoosh Jan 5 '18 at 21:12