ziben69
168
5
Laravel

Video upload | one to many relationship

Posted 4 months 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 = [
        'filename',
        'title',
        'layout',
        'description',
        'business_id',
    ];
     public $timestamps = false;

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

Business:

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

foreign key:

public function up()
    {
Schema::table('videos', function(Blueprint $table){
 $table->integer('business_id')->unsigned()->change();
 $table->foreign('business_id','videos_business_id_foreign')->references('id')->on('businesses');
});
    }

BusinessController (addVideo):

  public function addVideo(Request $request)
    {
        foreach ($request->videos as $video) {
            $filename = $video->store('videos');
            Video::create([
                '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) {

Error:

"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"
                          enctype="multipart/form-data">
.
.
.
<div class="form-group">
    <input type="file" id="upload" onchange="ValidateSize(this)" name="file" multiple aria-describedby="fileHelp"/>
</div>
<input type="submit" value="Create" class="btn btn-primary"/>
</form>

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.

Reply to

Use Markdown with GitHub-flavored code blocks.