i working on laravel v5.8 projec. i have some issue at make one to many relationship.
i have two table one is user which is provide some service and other table services. i want to make one to many relation between them. for user must have many services. for service must belong To on user. my code :
User.php
public function services()
{
return $this->hasMany(Service::class);
}
Service.php
public function user()
{
return $this->belongsTo(User::class);
}
Controller.php
public function index()
{
//
return view('vendor.services',['services'=>Service::with("user")->get()]);
}
blade file
@foreach ($services as $service )
<tr>
<td></td>
<td></td>
</tr>
@endforeach
migrations 1.Users
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
2.services
Schema::create('services', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string("short_desc", 250);
$table->string("long_desc", 1500);
$table->double("price", 10);
$table->bigInteger('type_id')->unsigned();
$table->foreign('type_id')->references('id')->on('types');
$table->string("img");
$table->bigInteger("city_id")->unsigned()->nullable();
$table->foreign('city_id')->references('id')->on('cities');
$table->bigInteger("region_id")->unsigned()->nullable();
$table->foreign('region_id')->references('id')->on('regions');
$table->bigInteger("user_id")->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
error: Trying to get property 'name' of non-object (View: /home/nazeeh/Desktop/petVet/resources/views/vendor/services.blade.php)
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire