Laravel. Two tables in relationship, selection of many photos
I have two tables:
Calendar, and Photo in relationship one calendar's event to many photos.
I would like to add many photos when creating the event. Currently, I can only have one, on a one-to-one basis.
My code:
Calendar model relation with Photo:
public function photos()
{
return $this->hasMany(Photo::class);
}
Photo model relation with Calendar:
public function calendar()
{
return $this->belongsTo(Calendar::class, 'calendar_id');
}
it's ok and it works.
Fragment of create.blade.php view:
<div class="card-body">
<form action="{{ action ('CalendarController@store')}}" method="POST"
enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<div class="form-group">
<label for="photo">Photo:</label> <select class="form-control" name="photo">
@foreach($photos as $photo)
<option value="{{ $photo->id }}">{{ $photo->path }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="header">Header</label> <input type="text" class="form-control" name="header"/></div>
<div class="form-group">
<label for="description">Description</label><input type="text" class="form-control" name="description"/></div>
<div class="form-group">
<label for="date">Date</label> <input type="date" class="form-control" name="date"/></div>
<input type="submit" value="Create" class="btn btn-primary"/>
</form>
</div>
For this moment I have classic select list where can I choose only one option.
Calendar controller code (function create and store):
public function create()
{
$photos = Photo::all();
return view('pages.calendar.create', [
"photos" => $photos
]);
}
public function store(Request $request)
{
$request->validate([
'header' => 'required|max:255',
'description' => 'required',
'date' => 'required',
]);
$calendar = new Calendar();
$calendar->header = $request->input('header');
$calendar->description = $request->input('description');
$calendar->date = $request->input('date');
$calendar->save();
$photo = Photo::find($request->input('photo'));
if($photo !== null) {
$photo->calendar_id = $calendar->id;
$photo->save();
}
return redirect()->action('CalendarController@clist');
}
How can I change this fragment of code to multistore photos:
$photo = Photo::find($request->input('photo'));
if($photo !== null) {
$photo->calendar_id = $calendar->id;
$photo->save();
}
I have calendar_id in Photo table (fereign key).
Thanks for any help!
laravel laravel-5
add a comment |
I have two tables:
Calendar, and Photo in relationship one calendar's event to many photos.
I would like to add many photos when creating the event. Currently, I can only have one, on a one-to-one basis.
My code:
Calendar model relation with Photo:
public function photos()
{
return $this->hasMany(Photo::class);
}
Photo model relation with Calendar:
public function calendar()
{
return $this->belongsTo(Calendar::class, 'calendar_id');
}
it's ok and it works.
Fragment of create.blade.php view:
<div class="card-body">
<form action="{{ action ('CalendarController@store')}}" method="POST"
enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<div class="form-group">
<label for="photo">Photo:</label> <select class="form-control" name="photo">
@foreach($photos as $photo)
<option value="{{ $photo->id }}">{{ $photo->path }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="header">Header</label> <input type="text" class="form-control" name="header"/></div>
<div class="form-group">
<label for="description">Description</label><input type="text" class="form-control" name="description"/></div>
<div class="form-group">
<label for="date">Date</label> <input type="date" class="form-control" name="date"/></div>
<input type="submit" value="Create" class="btn btn-primary"/>
</form>
</div>
For this moment I have classic select list where can I choose only one option.
Calendar controller code (function create and store):
public function create()
{
$photos = Photo::all();
return view('pages.calendar.create', [
"photos" => $photos
]);
}
public function store(Request $request)
{
$request->validate([
'header' => 'required|max:255',
'description' => 'required',
'date' => 'required',
]);
$calendar = new Calendar();
$calendar->header = $request->input('header');
$calendar->description = $request->input('description');
$calendar->date = $request->input('date');
$calendar->save();
$photo = Photo::find($request->input('photo'));
if($photo !== null) {
$photo->calendar_id = $calendar->id;
$photo->save();
}
return redirect()->action('CalendarController@clist');
}
How can I change this fragment of code to multistore photos:
$photo = Photo::find($request->input('photo'));
if($photo !== null) {
$photo->calendar_id = $calendar->id;
$photo->save();
}
I have calendar_id in Photo table (fereign key).
Thanks for any help!
laravel laravel-5
add a comment |
I have two tables:
Calendar, and Photo in relationship one calendar's event to many photos.
I would like to add many photos when creating the event. Currently, I can only have one, on a one-to-one basis.
My code:
Calendar model relation with Photo:
public function photos()
{
return $this->hasMany(Photo::class);
}
Photo model relation with Calendar:
public function calendar()
{
return $this->belongsTo(Calendar::class, 'calendar_id');
}
it's ok and it works.
Fragment of create.blade.php view:
<div class="card-body">
<form action="{{ action ('CalendarController@store')}}" method="POST"
enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<div class="form-group">
<label for="photo">Photo:</label> <select class="form-control" name="photo">
@foreach($photos as $photo)
<option value="{{ $photo->id }}">{{ $photo->path }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="header">Header</label> <input type="text" class="form-control" name="header"/></div>
<div class="form-group">
<label for="description">Description</label><input type="text" class="form-control" name="description"/></div>
<div class="form-group">
<label for="date">Date</label> <input type="date" class="form-control" name="date"/></div>
<input type="submit" value="Create" class="btn btn-primary"/>
</form>
</div>
For this moment I have classic select list where can I choose only one option.
Calendar controller code (function create and store):
public function create()
{
$photos = Photo::all();
return view('pages.calendar.create', [
"photos" => $photos
]);
}
public function store(Request $request)
{
$request->validate([
'header' => 'required|max:255',
'description' => 'required',
'date' => 'required',
]);
$calendar = new Calendar();
$calendar->header = $request->input('header');
$calendar->description = $request->input('description');
$calendar->date = $request->input('date');
$calendar->save();
$photo = Photo::find($request->input('photo'));
if($photo !== null) {
$photo->calendar_id = $calendar->id;
$photo->save();
}
return redirect()->action('CalendarController@clist');
}
How can I change this fragment of code to multistore photos:
$photo = Photo::find($request->input('photo'));
if($photo !== null) {
$photo->calendar_id = $calendar->id;
$photo->save();
}
I have calendar_id in Photo table (fereign key).
Thanks for any help!
laravel laravel-5
I have two tables:
Calendar, and Photo in relationship one calendar's event to many photos.
I would like to add many photos when creating the event. Currently, I can only have one, on a one-to-one basis.
My code:
Calendar model relation with Photo:
public function photos()
{
return $this->hasMany(Photo::class);
}
Photo model relation with Calendar:
public function calendar()
{
return $this->belongsTo(Calendar::class, 'calendar_id');
}
it's ok and it works.
Fragment of create.blade.php view:
<div class="card-body">
<form action="{{ action ('CalendarController@store')}}" method="POST"
enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<div class="form-group">
<label for="photo">Photo:</label> <select class="form-control" name="photo">
@foreach($photos as $photo)
<option value="{{ $photo->id }}">{{ $photo->path }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="header">Header</label> <input type="text" class="form-control" name="header"/></div>
<div class="form-group">
<label for="description">Description</label><input type="text" class="form-control" name="description"/></div>
<div class="form-group">
<label for="date">Date</label> <input type="date" class="form-control" name="date"/></div>
<input type="submit" value="Create" class="btn btn-primary"/>
</form>
</div>
For this moment I have classic select list where can I choose only one option.
Calendar controller code (function create and store):
public function create()
{
$photos = Photo::all();
return view('pages.calendar.create', [
"photos" => $photos
]);
}
public function store(Request $request)
{
$request->validate([
'header' => 'required|max:255',
'description' => 'required',
'date' => 'required',
]);
$calendar = new Calendar();
$calendar->header = $request->input('header');
$calendar->description = $request->input('description');
$calendar->date = $request->input('date');
$calendar->save();
$photo = Photo::find($request->input('photo'));
if($photo !== null) {
$photo->calendar_id = $calendar->id;
$photo->save();
}
return redirect()->action('CalendarController@clist');
}
How can I change this fragment of code to multistore photos:
$photo = Photo::find($request->input('photo'));
if($photo !== null) {
$photo->calendar_id = $calendar->id;
$photo->save();
}
I have calendar_id in Photo table (fereign key).
Thanks for any help!
laravel laravel-5
laravel laravel-5
asked Nov 24 '18 at 13:15
GiacomoGiacomo
104215
104215
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
1- Change your select to a multiple input
<input type="file" name="photos" class="form-control">
1- Make a foreach in controller to iterate over every image
if($photos=$request->file('photos')){
foreach($photos as $image){
$name = $photo->getClientOriginalName();
$photo = Photo::where('name',$name)->first();
if(is_null($photo)) {
$photo = new Photo();
}
$photo->name = $name;
$photo->calendar_id = $calendar->id;
$photo->save();
}
}
3- Maybe you can use some PHP package for images like http://image.intervention.io/
4- Look at this answer..
https://stackoverflow.com/questions/42643265/how-to-upload-multiple-image-in-laravel
Thanks I will try.
– Giacomo
Nov 24 '18 at 15:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458525%2flaravel-two-tables-in-relationship-selection-of-many-photos%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
1- Change your select to a multiple input
<input type="file" name="photos" class="form-control">
1- Make a foreach in controller to iterate over every image
if($photos=$request->file('photos')){
foreach($photos as $image){
$name = $photo->getClientOriginalName();
$photo = Photo::where('name',$name)->first();
if(is_null($photo)) {
$photo = new Photo();
}
$photo->name = $name;
$photo->calendar_id = $calendar->id;
$photo->save();
}
}
3- Maybe you can use some PHP package for images like http://image.intervention.io/
4- Look at this answer..
https://stackoverflow.com/questions/42643265/how-to-upload-multiple-image-in-laravel
Thanks I will try.
– Giacomo
Nov 24 '18 at 15:25
add a comment |
1- Change your select to a multiple input
<input type="file" name="photos" class="form-control">
1- Make a foreach in controller to iterate over every image
if($photos=$request->file('photos')){
foreach($photos as $image){
$name = $photo->getClientOriginalName();
$photo = Photo::where('name',$name)->first();
if(is_null($photo)) {
$photo = new Photo();
}
$photo->name = $name;
$photo->calendar_id = $calendar->id;
$photo->save();
}
}
3- Maybe you can use some PHP package for images like http://image.intervention.io/
4- Look at this answer..
https://stackoverflow.com/questions/42643265/how-to-upload-multiple-image-in-laravel
Thanks I will try.
– Giacomo
Nov 24 '18 at 15:25
add a comment |
1- Change your select to a multiple input
<input type="file" name="photos" class="form-control">
1- Make a foreach in controller to iterate over every image
if($photos=$request->file('photos')){
foreach($photos as $image){
$name = $photo->getClientOriginalName();
$photo = Photo::where('name',$name)->first();
if(is_null($photo)) {
$photo = new Photo();
}
$photo->name = $name;
$photo->calendar_id = $calendar->id;
$photo->save();
}
}
3- Maybe you can use some PHP package for images like http://image.intervention.io/
4- Look at this answer..
https://stackoverflow.com/questions/42643265/how-to-upload-multiple-image-in-laravel
1- Change your select to a multiple input
<input type="file" name="photos" class="form-control">
1- Make a foreach in controller to iterate over every image
if($photos=$request->file('photos')){
foreach($photos as $image){
$name = $photo->getClientOriginalName();
$photo = Photo::where('name',$name)->first();
if(is_null($photo)) {
$photo = new Photo();
}
$photo->name = $name;
$photo->calendar_id = $calendar->id;
$photo->save();
}
}
3- Maybe you can use some PHP package for images like http://image.intervention.io/
4- Look at this answer..
https://stackoverflow.com/questions/42643265/how-to-upload-multiple-image-in-laravel
answered Nov 24 '18 at 14:37
Walter CejasWalter Cejas
42039
42039
Thanks I will try.
– Giacomo
Nov 24 '18 at 15:25
add a comment |
Thanks I will try.
– Giacomo
Nov 24 '18 at 15:25
Thanks I will try.
– Giacomo
Nov 24 '18 at 15:25
Thanks I will try.
– Giacomo
Nov 24 '18 at 15:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458525%2flaravel-two-tables-in-relationship-selection-of-many-photos%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown