1 year ago

Video upload | one to many relationship

Posted 1 year ago by ziben69

Hello guys,

I did upload video files, but I have some problem in the controller with the file saved.

Relation on models: Video:

 protected $fillable = [
     public $timestamps = false;

  public function business()
        return $this->belongsTo(Business::class, 'business_id');


    public function videos()
        return $this->hasMany(Video::class);   

foreign key:

public function up()
Schema::table('videos', function(Blueprint $table){

BusinessController (addVideo):

  public function addVideo(Request $request)
        foreach ($request->videos as $video) {
            $filename = $video->store('videos');
                'business_id' => $business->id,
                'filename' => $filename,
                'title' => $title,
                'description' => $description,
                'layout' => $layout
        return redirect()->action('[email protected]');

Function addVideo creates a record in the Business table, but the problem appears at:

foreach ($request->videos as $video) {


"Invalid argument supplied for foreach()"

Part of routes web.php:

Route::get('business/media','[email protected]')->middleware('auth');

Route::post('business/', '[email protected]')->middleware('auth');

Fragment of form post:

 <form action="{{ action ('[email protected]')}}" method="POST"
<div class="form-group">
    <input type="file" id="upload" onchange="ValidateSize(this)" name="file" multiple aria-describedby="fileHelp"/>
<input type="submit" value="Create" class="btn btn-primary"/>

I dont know why it doesn't work. Can anyone help me? Thanks

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