0

this is all my codes after cliking on button doesn't save to database i try to change method of saving but not work

    use App\makereservations;
use Illuminate\Http\Request;

class reservationController extends Controller
{
    public function hreservation()
    {
        return view('hreservation');
    }
    public function reserve (Request $request)
    {
        $this->validate($request ,[
            'name' => 'required',
            'email' => 'required',
            'phone' => 'required|email',
            'date' => 'required',
            'time' => 'required',

        ]);
        $reservation  = new makereservations();
        $reservation  =  $request->input('name');
        $reservation  =  $request->input('email');
        $reservation  =  $request->input('phone');
        $reservation  =  $request->input('date');
        $reservation  =  $request->input('time');
        $reservation  =  $request->input('personne');
        $reservation -> statu =  false;
        $reservation -> save();
        return redirect()->back();
    }
}

this is the roote route::post('reserve', 'reservationController@reserve' )->name('reserv.sent');

1
  • what is the error Commented May 31, 2020 at 16:37

3 Answers 3

1

You missed the -> arrow sign.

$reservation  = new makereservations();
$reservation->name  =  $request->input('name');
$reservation->email  =  $request->input('email');
$reservation->phone  =  $request->input('phone');
$reservation->date  =  $request->input('date');
$reservation->time  =  $request->input('time');
$reservation->personne  =  $request->input('personne');
$reservation-> statu =  false;
$reservation-> save();
return redirect()->back();
Sign up to request clarification or add additional context in comments.

2 Comments

i changed name of table but does'nt work how i can make old table works $inputs = $request->all(); $inputs['statu'] = false; $reservation = reservation::create($inputs); <=========== focus here return redirect()->back();
In your reservation table, add this line, protected $table = 'new_table_name'; Now, reservation::create() will save in your new table.
1

If in the MakeReservations model you have massively assignable fields

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class makereservations extends Model
{
    protected $table = "your_table_name"
    ...
    protected $fillable = [
        'name', 'email', 'phone', 'date', 'time', 'personne', 'status'
    ];
    ...
}

Your reserve method in reservationController it may be so:

<?php

namespace App\Http\Controllers;

use App\makereservations;
use Illuminate\Http\Request;

class reservationController extends Controller
{
    ...
    public function reserve (Request $request)
    {
        $this->validate($request ,[
            'name' => 'required',
            'email' => 'required|email',
            'phone' => 'required',
            'date' => 'required',
            'time' => 'required',

        ]);
        $inputs = $request->all();
        $inputs['status'] = false;
        $reservation = makereservations::create($inputs);
        return redirect()->back();
    }
}

I have detected that in your validation method you are trying to validate that the phone is an email

3 Comments

do i must create new table with the name makereservations becouse i allready have table named reservation
i changed name of table but does'nt work how i can make old table works $inputs = $request->all(); $inputs['statu'] = false; $reservation = reservation::create($inputs); <=========== focus here return redirect()->back();
Oh yes, varname is statu, you must change it in $fillable array in your model
0

You can do it more shortly and in a clean way, to optimize your code.

public function reserve (Request $request)
{
    $attributes = $this->validate($request ,[
        'name' => 'required',
        'email' => 'required|email',
        'phone' => 'required',
        'date' => 'required',
        'time' => 'required',
        'personne' => ''
    ]);

    $attributes['statu'] = false;

    makereservations::create($attributes);
    return redirect()->back();
}

In your model make sure these things

class makereservations extends Model
{
    protected $table = 'reservation';
    protected $guarded = []; //it means all data from request will be mass assignable
}

5 Comments

I tried but show me this message and the table name reservation if SQLSTATE[42S02]: Base table or view not found: 1146 Table 'restau_app.makereservations' doesn't exist (SQL: insert into makereservations (name, email, phone, date, time, personne, statu, updated_at, created_at) values (test, [email protected], 12345678, 5/7/2020, 12:30am, 1, 0, 2020-05-31 17:33:40, 2020-05-31 17:33:40))
i changed name of table but does'nt work how i can make old table works $inputs = $request->all(); $inputs['statu'] = false; $reservation = reservation::create($inputs); <=========== focus here return redirect()->back();
can you tell me what is your table name in mysql and what is your model name for that table ? And you don't need to change your table name, look very simple, you have a table name reservation and you might have made a model for this table named makereservations, right ?
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into contacts (name, email, subject, message, status, updated_at, created_at) values (test, [email protected], mytest, text text text text, 0, 2020-06-01 19:54:14, 2020-06-01 19:54:14))
So you are having problem in another table now ?

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.