PIVOT TABLE
| post_id | food_id | post_gram |
|:---- |:------: | -----: |
| 1 | 5 | NULL |
| 1 | 6 | NULL |
| 1 | 20 | NULL |
Ahoj, mám tabulku, Post a Jídlo a kontingenční tabulky food_post, když chci uložit data do kontingenční tabulky uloží pouze id příspěvku a jídlo id ale post_gram dont(nastaví hodnotu na NULL).. v blade mám vybrat vstup vyberte jídlo select=jméno"select2[]" a vstupní napsat, kolik gramů vyberte=název"select3[]" a pak tlačítko, které kopíruje to tak, že uživatel může přidat, kolik chce , já třeba to, že při ukládání dat do kontingenční tabulky hodnota g je uložen a není null v db, v modelu budu definovat "withPivot" já jsem se také zkusit připojit() a to hází chybu, že říct "Pole na řetězec konverze"
---------------**BLADE** ----------------
<div class="flex my-5" id="duplicater">
<select name="select2[]" id="select">
<option disabled selected>Vyberte prosím potravinu</option>
@foreach ($food as $f )
<option value="{{$f->id}}">{{$f->nazev}}</option>
@endforeach
</select>
<div>
<input id="food" type="text" name="select3[]" value="" placeholder="Počet gramů">
</div>
</div>
</div>
<button type="submit" class="uppercase mt-15 bg-gray-900 text-gray-100 text-lg font-bold py-4 px-20 rounded-2xl ">Submit Post</button>
</form>
<button id="button" class="p-3 my-6 border-green-400 border text-green-400" onclick="duplicate()">Přidat další potravinu</button>
--------------**Post Controller**----------------
public function store(Request $request)
{
//dd($request->select2);
$data = [];
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'required|mimes:jpg,png,jpeg|max:5048'
]);
$newImageName = uniqid() . '-' . $request->title . '.' . $request->image->extension();
$request->image->move(public_path('images'), $newImageName);
$post = new Post();
$post->title = $request->input('title');
$post->description = $request->input('description');
$post->slug = SlugService::createSlug(Post::class, 'slug', $request->title);
$post->image_path =$newImageName;
$post->user_id = auth()->user()->id;
$post->save();
-----------------------------------------------------------------------------
-----------here is the attach to the pivot table -------------
-----------------------------------------------------------------------------
$post->food()->sync($request->select2,[
'post_gram' => $request->select3
]);
return redirect('/blog')
->with('message', 'Your post has been added!');
}
-------------------------příspěvek model----------------------------
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
use Laravelista\Comments\Commentable;
class Post extends Model
{
use HasFactory;
use Sluggable;
use Commentable;
protected $fillable = ['title','slug' ,'description' , 'image_path', 'user_id'];
public function user(){
return $this->belongsTo(User::class);
}
public function food(){
return $this->belongsToMany(Food::class)->withPivot('post_gram');//->withPivot(['post_kalorie','post_bilkoviny','post_sacharidy','post_tuky','post_objem']);
}
public function sluggable():array{
return[
'slug'=>[
'source' => 'title'
]
];
}
}