Home > Software engineering >  How do I make a Save button for my project in laravel
How do I make a Save button for my project in laravel

Time:01-03

I am making a site for drawing in laravel and I don't know how to make a save button for my drawings,all I need to save for now are the name of the canvas and the size of it,one problem is that the name is stored in an input and I don't know how to access it,the other what do I return after saving it

<a href="{{route('canvas.save',['size' =>$size])}}">

This is how I transfer the size,but the name I don't know yet how to transfer

this is the input where I store it

<input oninput="ChangeInput(this.value)" value="Canvas" type="text" name="title" id="title">

this is how I add the data to the table

    public function createDrawing($name,$size,$id){
        return Drawing::create([
            'name' => $name, //the name of the canvas
            'canvas_size' => $size,
            'users_id' => $id //this is a foreign key
        ]);
    }

the structure of the route is

Route::get('canvasSave/{size}',[CustomAuthController::class,'SaveDrawing'])->name('canvas.save');

    public function SaveDrawing($size){
        $check = $this->createDrawing(...,$size,1); 

//how do I get the name here from the input
      
}

What do I return after creating the drawing was stored in the table,my idea was to return a Partial view like a Popup but still don't know how to do it, I just dont understand how to save it via routes and I'm confused,your help would help a lot

CodePudding user response:

So I managed to do the saving like this in the controller

public function createDrawing(array $data){
    return Drawing::create([
        'name' => $data['title'],
        'canvas_size' => $data['size'],
        'users_id' => $data['users_id']
    ]);
}
public function SaveDrawing(Request $request){
    $data = $request->all();
    $data['users_id'] = Auth::id();
    $check = $this->createDrawing($data);
    return View('main.introduction');
}

I did the route into the post like you said and things got clearer after I made the html in form

route:

Route::post('canvasSave',[CustomAuthController::class,'SaveDrawing'])->name('canvas.save');

HTML:

            <form action="{{route('canvas.save')}}" method="POST">
                @csrf
                <input oninput="ChangeInput(this.value)" value="Canvas" type="text" name="title" id="title">
                <input type="hidden" name="size" value="{{$size}}">
                <button type="submit">Save</button>
            </form>

after the save it just returns back to the main page

CodePudding user response:

Use Route::post and pass all the data as a payload (body) on the request. Τhe easiest way is to add them in a form and the size can be a hidden input with the value or use a javascript ajax method to pass them as you like.

  • Related