ziben69
125
5
Laravel

Save calendar's event with photo (two tables)

Posted 5 months ago by ziben69

Hello guys, I have a problem saving the photo when creating a calendar event. I have two tables in relationship calls: model Calendar

class Calendar extends Model
{
    protected $fillable = [
        'header','description','date'
    ];
    
      public function photos()
    {
        return $this->hasMany(Photo::class);   
    }

and model Photo: class Photo extends Model { protected $fillable = [ 'photoable_type','photoable_id','path','calendar_id' ];

public function calendar()
{
    return $this->belongsTo(Calendar::class, 'calendar_id');
}

}

my photos have calendar_id, i.e. one event can have many photos.

Here is CalendarController (create and store functions):

  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->calendar_id = $request->input('photo'); <--- calendar_id is in the photo model
        $calendar->header = $request->input('header');
        $calendar->description = $request->input('description');
        $calendar->date = $request->input('date');
        $calendar->save();
    
        return redirect()->action('[email protected]');
    }

and here is a fragment of create.blade.php view:

<form action="{{ action ('[email protected]')}}" 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 }}"><img class="img-responsive" src="{{ $photo->path }}"  style="width:100px; height:auto;"/></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>

Error:

Column not found: 1054 Unknown column 'calendar_id' in 'field list' (SQL: insert into `calendars` (`calendar_id`, `header`, `description`, `date`, `updated_at`, `created_at`) values (204, asdsa, dsadsaasd, 1998-12-12, 2018-11-23 11:21:09, 2018-11-23 11:21:09))

I know that calendar_id is a column in Photo table, but how can I solve this?? I need to add photos from calendar's create view. Thanks for help.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.