I want to be able to pass the value of my related table to my views, I am making use of a One to Many relation. also I am trying to do the connection without using a foreign key, well I have been informed that using it for mysql database is not efficient and it would probably brings up error or not do anything. I actually tested it, I mean the foreign key, it did not work.
Post model:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//Post model
protected $table = 'posts';
public function staff()
{
return $this->belongsTo('App\Models\Staff');
}
}
Staff model:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Staff extends Model
{
protected $table = 'staff';
public function posts()
{
return $this->hasMany('App\Models\Post');
}
}
Create posts table migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{ //my post table
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('name');
$table->string('phone_number');
$table->mediumText('fault_explained');
$table->string('repair_type');
$table->string('repair_part');
$table->integer('repair_total');
$table->mediumtext('justify');
$table->string('vendor_name');
$table->string('vendor_number');
$table->string('status_option');
$table->integer('status_value');
$table->string('vehicle_name');
$table->string('vehicle_number');
$table->integer('staff_id')->nullable()->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Create staff table migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStaffTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{ //Staff table
Schema::create('staff', function (Blueprint $table) {
$table->increments('id');
$table->string('senders_name');
$table->string('senders_number');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('staff');
}
}
View:
<div >
<main >
<div >
<div style="margin-left:14px; margin-top:5px;">
<span > <em><b>SUBJECT:</b> {{ $posts->title }}</span></em>
</div>
<div style="margin-left:14px; margin-bottom:5px;">
<em><b>Sent on</b> {{ $posts->created_at }}</em> {{ $posts->staff->senders_name}}
</div>
CodePudding user response:
In the Post model you should use -
public function staff()
{
return $this->belongsTo(Staff::class);
}
In the Staff model you should use -
public function posts()
{
return $this->hasMany(Post::class);
}
In CreatePostsTable migration you need to add one more line -
$table->foreign('staff_id')->references('id')->on('staff');
CodePudding user response:
You must replace App\Models\Staff with Staff::class in your code.
...
return $this->belongsTo(Staff::class);
...
