Reputation: 1
I have a form with two pivot tables. One of them works just fine but I can't seem to be making the second one work despite them being quite similar. The one not working is for an image table called 'photos' and the form upload in called 'releases'. I called the pivot table 'photo_releases' with the 'photo_id' and a 'release_id' field.
here is the release Modal
class Release extends Model { public function photos() { return $this->belongsToMany('App\Photo', 'photo_releases', 'release_id', 'photo_id'); } }
and the photo modal
class Photo extends Model { public function releases() { return $this->belongsToMany('App\Release', 'photo_releases', 'photo_id', 'release_id'); } }
and the ReleaseController
public function store(ReleasesCreateRequest $request) { $input = $request->all(); $user = Auth::user(); if ($file = $request->file('photo_01')) { $file_name = preg_replace("/[^a-zA-Z0-9.]/", "", $file->getClientOriginalName()); $name = time() . 'photo_01' . $file_name; $file->move('images', $name); $input['photo_01'] = $name; $photo = new Photo(); $photo->url = $input['photo_01']; $photo->save(); } $release = Release::create($request->except('release_id')); dd($request->except('release_id'), $request->get('photo_id', []), $request->get('artiste_id', [])); $release->photos()->attach($request->get('photo_id', [])); $release->artistes()->attach($request->get('artiste_id', [])); return redirect('/admin06000/releases'); }
There is two pivot tables being used in this function. the one using
"$release->artistes()->attach($request->get('artiste_id', []));"
is working correctly but the photos is not. The url is being logged in the correct DB and the image is uploading fine, but the pivot table is not being updated. If anyone could help it would be greatly appriciated.
Upvotes: 0
Views: 404
Reputation: 41
try This if you need some select in relation ship change
with('photos')
to
with(['photos'=>function($query){$query->where(....)->get();}])...
use Image;
use Illuminate\Support\Facades\Input;
...
public function store(ReleasesCreateRequest $request)
{
$input = $request->all();
$user = Auth::user();
if ($file = $request->file('photo_01'))
{
$image= Input::file('photo_01');
$name = time().'photo_01'.'.'.$image->getClientOriginalExtension();
$path=public_path('/YourPath/'.$name);
Image::make($image->getRealPath())->save($path);
$photo = new Photo();
$photo->url = '/YourPath/'.$name;
$photo->save();
}
$release = Release::create
([
'release_field'=>$amount,
'release_field2'=>$amount2,
....
]);
$release->with('photos')->with(artistes)->get();
}```
Upvotes: 0