Это моя функция магазина внутри моего контроллера AlbumController:
Код: Выделить всё
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$user_id = Auth::id();
$data = [
'album_name' => $request->get('album_name'),
'album_description' => $request->get('album_description'),
'user_id' => $user_id // I still hasnt figured out how assign the user_id value to the album using Eloquent ORM relationships either
];
//album::create($data); // creates the album
$photos = ['photos' => $request->get('photo_checkbox')]; // insert photos checklisted in the create album form
foreach ($photos as $photo){
$album_query = Photo::where('photo_id',$photo); // assign the photos checklisted album_id according to the album just created
$result = $album_query->albums()->save(); // tried to use Laravel's Eloquent ORM relationships
/** $album = Album::where('album_name',$data['album_name']);
* $result = $album_query->update(['album_id' => $album->album_id]); // alternative without Eloquent ORM relationships
*/
}
dd($result);
session()->now('data','album_name');
return redirect(route('album.album'))->with('success','album "'.$data['album_name'].' '.'" has been registered.');
}
Код: Выделить всё
class Album extends Model
{
use HasFactory;
protected $table = "albums";
protected $primaryKey = "album_id";
const CREATED_AT = 'data_created';
const UPDATED_AT = 'data_modified';
protected $fillable = [
'album_name',
'album_description',
'user_id'
];
public function photos() : HasMany {
return $this->hasMany(Photo::class,'album_id','album_id');
}
public function users() : BelongsTo {
return $this->belongsTo(GalleryUser::class,'user_id','gallery_user_id');
}
}
Код: Выделить всё
class Photo extends Model
{
/** @use HasFactory */
use HasFactory;
protected $table = "photos";
protected $primaryKey = "photo_id";
const CREATED_AT = 'data_created';
const UPDATED_AT = 'data_modified';
protected $fillable = [
'photo_title',
'photo_description',
'photo_location',
'user_id',
'album_id'
];
public function comments() : HasMany {
return $this->hasMany(PhotoComment::class,'comment_id','comment_id');
}
public function albums() : BelongsTo {
return $this->belongsTo(Album::class,'album_id','album_id');
}
public function users() : BelongsTo {
return $this->belongsTo(GalleryUser::class,'user_id','gallery_user_id');
}
}
Код: Выделить всё
...
foreach ($photos as $photo){
$album_query = Photo::where('photo_id',$photo); // assign the photos checklisted album_id according to the album just created
$result = $album_query->albums()->save(); // tried to use Laravel's Eloquent ORM relationships
/** $album = Album::where('album_name',$data['album_name']);
* $result = $album_query->update(['album_id' => $album->album_id]); // alternative without Eloquent ORM relationships
*/
}
...
Между тем, альтернатива тоже выглядит не очень хорошо, хотя я думаю, что это достаточно правдоподобный код, я чувствую, что красноречивый ORM не используется очень часто, и я мог бы с таким же успехом не использовать его если это так.
Подробнее здесь: https://stackoverflow.com/questions/790 ... value-usin