I want to enter some data from another table and save it in a table. I try to use relationship one to many, but I'm confused to save data
This is my database
public function up()
{
Schema::create('jenis_analisas', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('jenis_analisa', 100);
$table->unsignedBigInteger('kategori_id')->nullable();
$table->integer('harga');
$table->timestamps();
$table->foreign('kategori_id')->references('id')->on('kategoris');
});
}
public function up()
{
Schema::create('permintaan_analisas', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('pelanggan_id');
$table->string('judul_penelitian', 100);
$table->string('jenis_contoh', 50);
$table->string('jumlah_contoh');
$table->string('asal_contoh', 200);
$table->unsignedBigInteger('jenisanalisa_id')->nullable();
$table->timestamps();
$table->foreign('pelanggan_id')->references('id')->on('pelanggans');
$table->foreign('jenisanalisa_id')->references('id')->on('jenis_analisas');
});
}
This is my model
class JenisAnalisa extends Model
{
public function kategori()
{
return $this->belongsTo('App\Kategori');
}
public function permintaananalisa()
{
return $this->hasMany('App\PermintaanAnalisa');
}
}
class PermintaanAnalisa extends Model
{
public function jenisanalisa()
{
return $this->belongsTo('App\JenisAnalisa');
}
}
I want to insert multiple data from jenis_analisas table into permintaan_analisas table.
Related
Laravel version: 7.0 Here is my table.
Schema::create('model_email_form', function (Blueprint $table) {
$table->id();
$table->string('model_type');
$table->unsignedBigInteger('model_id');
$table->unsignedBigInteger('email_id');
$table->unsignedBigInteger('form_id');
$table->timestamps();
});
Here is my Service model.
public function forms()
{
return $this->morphToMany(
Form::class,
'model',
'model_email_form',
'model_id',
'form_id'
);
}
public function emails()
{
return $this->morphToMany(
Email::class,
'model',
'model_email_form',
'model_id',
'email_id'
);
}
I inserted data in model_email_form table but when I get service model object, emails and forms have null object.
Can anyone help me?
From your question and comments:
There are Form, Email and Service. Forms can be associated with any number of different types of models. Emails can be associated with any number of different types of models. A Service can have many Forms and a Service can have many Emails.
Using that as the basis, this would be our schema:
Schema::create('forms', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name'); // as an example
...
$table->timestamps();
});
Schema::create('formables', function (Blueprint $table) {
$table->unsignedBigInteger('form_id'); // the id of the form
$table->unsignedBigInteger('formable_id'); // the associated model's id
$table->string('formable_type'); // The associated model's class name
});
Schema::create('emails', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('subject'); // as an example
...
$table->timestamps();
});
Schema::create('emailables', function (Blueprint $table) {
$table->unsignedBigInteger('email_id'); // the id of the email
$table->unsignedBigInteger('emailable_id'); // the associated model's id
$table->string('emailable_type'); // The associated model's class name
});
Schema::create('services', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name'); // as an example
...
$table->timestamps();
});
With that schema, we can create the following models with the following relationships:
class Form extends Model
{
public function services()
{
return $this->morphedByMany(Service::class, 'formable');
}
// Add the other morphedByMany relationships of forms
}
class Email extends Model
{
public function services()
{
return $this->morphedByMany(Service::class, 'emailable');
}
// Add the other morphedByMany relationships of emails
}
class Service extends Model
{
public function forms()
{
return $this->morphedToMany(Form::class, 'formable');
}
public function emails()
{
return $this->morphedToMany(Email::class, 'emailable');
}
}
I have a table has many relation to other tables but it seperated with entity value please look at this :
i have this schema
public function up()
{
Schema::create('cards', function(Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('user_id')->unsigned()->nullable();
$table->integer('entity_id');
$table->string('entity');
$table->integer('qty')->nullable()->default('1');
});
}
public function up()
{
Schema::create('tickets', function(Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('title');
$table->string('summary');
$table->integer('amount');
$table->integer('stock')->default('0');
$table->integer('discount')->default('0');
});
}
public function up()
{
Schema::create('products', function(Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('title');
$table->integer('amount');
$table->integer('discount');
$table->text('description');
$table->integer('stock')->default('0');
});
}
and this relation in models
class Card extends Model
{
protected $table = 'cards';
public $timestamps = true;
public function ticket()
{
return $this->belongsTo('App\Models\Ticket', 'entity_id');
}
public function product()
{
return $this->belongsTo('App\Models\Product', 'entity_id');
}
}
i need to set where entity = 'ticket' before use belongsTo i mean is a table hase relation to many table base entity_id and i seperated it by entity column and base same vlue most have realation just.
You can do simply in your eloquent model file. do like this :
public function ticketWithCondition()
{
return $this->belongsTo('App\Models\Ticket', 'entity_id')->where('entity' , 'ticket');
}
public function ticket()
{
return $this->belongsTo('App\Models\Ticket', 'entity_id');
}
call like this :
// for show Card with EntityCondition
$comments = Card::find(123)->with('ticketWithCondition');
// for show comments without EntityCondition
$comments = Card::find(123)->with('ticket');
this should be quite simple but i don't know what wrong . i have the following migrations .
create_players_table
public function up()
{
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->integer('club_id');
$table->string('first_name');
$table->string('last_name');
$table->double('price', 3, 2);
$table->enum('position', ['goalkeeper','defender','Midfielder','Striker']);
$table->timestamps();
});
}
create_clubs_table
public function up()
{
Schema::create('clubs', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('location');
$table->timestamps();
});
}
The relationship is straight forward as one player has one team (One to one) and one team can have many players (One to Many ) . The problem is i want to list all the players with their respective teams but somehow i end up with errors . This is my player model .
class Player extends Model
{
public function club()
{
return $this->belongsTo('App\Club');
}
}
And here is the controller :
public function index()
{
$players=Player::find(1);
echo $players->club()->location;
}
I get this error
ErrorException in TeamController.php line 15: Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name
Any help will be appreciated...
Your relation is not really a database relation try to make you're relation like this:
public function up()
{
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('club_id');
$table->foreign('club_id')->references('id')->on('clubs');
$table->string('first_name');
$table->string('last_name');
$table->double('price', 3, 2);
$table->enum('position', ['goalkeeper','defender','Midfielder','Striker']);
$table->timestamps();
});
}
https://laravel.com/docs/5.4/migrations#foreign-key-constraints
Player Model
class Player extends Model
{
public function club()
{
return $this->belongsTo('App\Club', 'club_id', 'id');
}
}
Player controller
public function index()
{
$players=Player::find(1);
echo $players->club->location;
}
it should be like this :
$players=Player::with('club')->find(1);
echo $players->club->location;
Try without () in ->club(),
public function index()
{
$players=Player::find(1);
echo $players->club->location;
}
As the error says, it refers to the "BelongsTo" relationship. i.e. Here ->club() returns the relationship object, not the club object. If you want to get the club object call as $players->club->location; .
So, I'm trying to create a relationship where users can follow other users or follow categories.
My intuition says that what I've done so far is not the right way of doing things. I'm especially confounded by how to create the follower - followee relationship.
TABLES:
Users
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email');
$table->string('password');
$table->string('first_name');
});
}
Categories
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('category');
});
}
Follows
public function up()
{
Schema::create('follows', function (Blueprint $table) {
$table->increments('id');
$table->integer('follower_id');
$table->integer('followee_id')->nullable();
$table->integer('category_id')->nullable();
});
}
MODELS:
User
class User extends Model implements Authenticatable
{
public function follows()
{
return $this->hasMany('App\Follow');
}
}
Category
class Category extends Model
{
public function follows()
{
return $this->hasMany('App\Follow');
}
}
Follow
class Follow extends Model
{
public function post()
{
return $this->belongsTo('App\User');
}
public function source()
{
return $this->belongsTo('App\Category');
}
}
According to your scenario, it is recommended that you use Polymorphic Many To Many relationship.
Schema:
users
id - integer
...
categories
id - integer
...
followables
user_id - integer
followable_id - integer
followable_type - string
Models:
User:
public function followers()
{
return $this->morphToMany(User::class, 'followables');
}
public function following()
{
return $this->morphedByMany(User::class, 'followables');
}
Category:
public function followers()
{
return $this->morphToMany(User::class, 'followables');
}
Then you can create the relationship like:
When following an User:
$user->followers()->create(['user_id' => 12])
When following a Category:
$category->followers()->create(['user_id' => 25])
Hope it helps.
I have a many-to-many relationship between my client and tag tables. A client can have many tags, and each tag can be related to multiple clients.
On the client show view, I'm trying to display the client info plus all tags associated to this client.
How do I change the query below to retrieve the client rows with all its related tags?
public function show($id)
{
$client = Client::findOrFail($id);
return view('clients.show')->with(['client' => $client]);
}
Client model
public function clienttag()
{
return $this->belongsToMany('App\Clienttag');
}
Clienttag model
public function client()
{
return $this->belongsToMany('App\Client');
}
Client_clientags table migration
public function up()
{
Schema::create('client_clienttag', function(Blueprint $table)
{
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->integer('clienttag_id')->unsigned();
$table->foreign('clienttag_id')->references('id')->on('clienttags')->onDelete('cascade');
$table->timestamps();
});
}
Clients table migration
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->rememberToken();
$table->timestamps();
});
}
Clienttags table migration
public function up()
{
Schema::create('clienttags', function(Blueprint $table)
{
$table->increments('id');
$table->string('tag');
$table->text('description');
$table->rememberToken();
$table->timestamps();
});
}
You can use "Eager Loading" methods like the following
public function show($id)
{
$client = Client::with('clienttag')->findOrFail($id);
return view('clients.show')->with(['client' => $client]);
}
Check documentation at http://laravel.com/docs/5.1/eloquent-relationships#eager-loading
Then at your view you can print your tags
#foreach ($client->clienttag as $tag)
{!! $tag->tagname!!} (or whatever your field in clienttags table name is)
#endforeach