0

I am currently trying to make a CRUD of Web Banner (Image) and the creating of image already works and it is already saving in my database but I need a function that will make the image be changeable on the postman when I look for the ID of that webinar.

I am in need of help in the UpdateBanner function. It is the one who will update the image of the given webinar id. This is currently my code:

Banner Upload Controller:

    <?php

namespace App\Http\Controllers;
use App\Models\Webinar;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BannerUploadController extends Controller
{
    public function FileUpload(Request $request)
    {
        $validator = Validator::make($request->all(),[ 

            'file' => 'file|required'
        ]);
    
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }

        $uploaded_files = $request->file->store('public/uploads/');

        $webinar = new Webinar();
        $webinar->id = $request->id;
        $webinar->web_title = $request->web_title;
        $webinar->web_description = $request->web_description;
        $webinar->status = $request->status;
        $webinar->remarks = $request->remarks;
        $webinar->web_banner_profile = $request->file->hashName();
        $webinar->created_by = $request->created_by; 
        $webinar->updated_by = $request->updated_by; 
        $webinar->web_link = $request->web_link; 

        $results = $webinar->save();
        if($results){
            return ["result"=>"Image Added"];
        }else{
            return ["result"=>"Image Not Added"];
        }

        return ["result"=>"$uploaded_files"];

    }

    public function UpdateBanner(Request $request, $id)
    {
    
        $webinar = Webinar::find($request->id);
        if(is_null($webinar)){
            return response()->json('Record not found!', 401);
        }
        $webinar->update($request->web_banner_profile);
        return $webinar;

    }

public function DeleteBanner(Request $request, $id)
{

    $webinar = Webinar::find($id);
    if(is_null($webinar)){
        return response()->json('Record not found!', 401);
    }

    $webinar->web_banner_profile->delete();
    return response('Webinar Banner Deleted', 200);

}



    }

This is my Webinar Model if it helps:

    <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Webinar extends Model
{
    public $table = "webinar";
    use HasFactory;

    // protected $fillable = [
    //     'web_title',
    //     'web_description',
    // ];

    protected $guarded = [];
}

And also my Webinar CRUD Controller:

    <?php

namespace App\Http\Controllers\Api;

use App\Models\Webinar;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class WebinarCRUDController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $webinars = Webinar::all();
        return response($webinars,200);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->validate([
            'web_title'=>'required',
            'web_description'=>'required',
        ]);

        $webinars = Webinar::create($data);

        return response ($webinars, 200);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $webinars = Webinar::find($id);
        if(is_null($webinars)){
            return response()->json('Record not found!', 401);
        }
        return response()->json(Webinar::find($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $webinars = Webinar::find($id);
        if(is_null($webinars)){
            return response()->json('Record not found!', 401);
        }
        $webinars->update($request->all());

        return $webinars;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $webinars = Webinar::find($id);
        if(is_null($webinars)){
            return response()->json('Record not found!', 401);
        }
        $webinars->delete();
        return response('Webinar Deleted', 200);
    }
}

Any suggestions and opinion on where I got it wrong on the UpdateBanner function, would be a big big help. Thank you so much in advance.

1 Answer 1

1

What error are you getting?

As far as I can see you are not using the update method correctly. You need to pass an array to it.

https://laravel.com/docs/8.x/eloquent#updates

The update method expects an array of column and value pairs representing the columns that should be updated.

Like:

$webinar->update(['web_banner_profile' => $request->web_banner_profile]);
Sign up to request clarification or add additional context in comments.

4 Comments

404 not found. Actually I only need now on how to delete that specific column on the table. Do u know how? I already put my DeleteBanner function above. thanks
404 means you are probably calling a wrong route (show your routes and where you call them). You don't delete a column, you just update it to null.
Do you know how I can make it NULL? the web_banner_profile? thank you
$webinar->update(['web_banner_profile' => null]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.