我有一个带有两个数据透视表的表单。其中一个工作正常,但尽管它们非常相似,但我似乎无法使第二个工作正常。一个不起作用的是一张名为“照片”的图像表,以及一张名为“发行”的表格上载。我使用“ photo_id”和“ release_id”字段将数据透视表称为“ photo_releases”。
这是版本Modal
class Release extends Model { public function photos() { return $this->belongsToMany('App\Photo', 'photo_releases', 'release_id', 'photo_id'); } }
和照片模态
class Photo extends Model { public function releases() { return $this->belongsToMany('App\Release', 'photo_releases', 'photo_id', 'release_id'); } }
和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'); }
此功能中使用了两个数据透视表。一个使用
“ $ release-> artistes()-> attach($ request-> get('artiste_id',[]));”“
可以正常工作,但照片不能正常工作。网址已记录在正确的数据库中,并且图像可以正常上传,但数据透视表未更新。如果有人可以帮助,那就大有助益了。
答案 0 :(得分:0)
尝试此操作,如果您需要在关系更改中进行一些选择
with('photos')
至
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();
}```