A
A
Alexander Shvedov2019-09-18 20:11:56
Laravel
Alexander Shvedov, 2019-09-18 20:11:56

Laravel connection data insertion?

There are 2 tables, they are connected to each other.

// table book:
$table->bigIncrements('id');
$table->bigInteger('book_language_id')->unsigned()->nullable();
$table->foreign('book_language_id')->references('id')->on('book_languages');

// table:book_languages
$table->bigIncrements('id');
$table->char('name',255);

And also one to one communication
class BookLanguage extends Model
{
    public function book(){
        return $this->belongsTo(Book::class);
    }
}

class Book extends Model
{
    protected $guarded = [];
    public function language(){
        return $this->hasOne(BookLanguage::class,'id','book_language_id');
    }
}

The question is in the following field, I create a book object and want to bind a relationship, but I have from the book object only its name, let's say 'Rus' in Laravel, I tried to bind only by key passes
$book=Book::create(['name'=>'books','ISBN'=>'123123','book_language_id'=>1]);

How can I implement a binding by name in phpmyadmin quietly passes why in laravel I can’t be grateful for any
help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maddog670, 2019-09-18
@constintmid

$lang = BookLanguage::find($request->book_language_id);

$book = Book::make(['name'=>'books','ISBN'=>'123123']);
$book->language()->associate($lang);

$book->saveOrFail();

https://laravel.com/docs/6.x/eloquent-relationship...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question