i am new to laravel and coding in general. I would like to know how do you store data to an empty table using a form.
and also if it is possible how to show a single column of the table i stored in the data. I ve tried multiple things but i just dont get how routes work and connect with controllers.
this is my view home.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header"style="text-align:center;font-size:20px;">Teams</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
<div class=col-md-12>
<a class="pl-5" style="font-size:25px;"href="createteams">Create a Team</a>
<a class="pl-5"style="font-size:25px;"href="">View Teams</a>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
i want the user to click on create team and redirect him to my other form called createteams.blade.php, which is this:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header"><h2 style="text-align:center;">Create Your Own Team</h2></div>
<div class="card-body">
<form method="POST" action="home">
@csrf
<div class="form-group row">
<label for="team_name" class="col-md-4 col-form-label text-md-right">Team Name</label>
<div class="col-md-6">
<input id="team_name" type="text" class="form-control @error('team_name') is-invalid @enderror" name="team_name" value="{{ old('team_name') }}" required autocomplete="team_name" autofocus>
@error('team_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="team_desc" class="col-md-4 col-form-label text-md-right">{{ __('Team Description') }}</label>
<div class="col-md-6">
<input id="team_desc" type="text" name="team_desc" class="form-control @error('team_desc') is-invalid @enderror"value="{{ old('team_desc') }}">
@error('team_desc')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="org_select" class="col-md-4 col-form-label text-md-right">{{ __('Select up to 4 Orgs') }}<br>Hold Ctrl For multiple selection</label>
<div class="col-md-6">
<select id="org_select" multiple class="form-control" name="org_select" value="{{ old('org_select') }}" autocomplete="org_select">
<option value= "frederick">Frederick</option>
<option value="I dont know">Cyprus Uni</option>
</select>
@error('org_select')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Confirm
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
And then fill out the form and store it in the database
this is my migration of the table
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTeamsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('teams', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('team_name');
$table->text('desc')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('teams');
}
}
this is my controller
<?php
namespace App\Http\Controllers;
use App\Team;
use Illuminate\Http\Request;
class CreateTeamController extends Controller
{
public function store(Request $request)
{
return view('createteams');
$team=Team::create($request->all());
return redirect()->route("home");
}
}
This is my model of the teams table
namespace App;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
protected $fillable = [
'team_name', 'desc',
];
protected $table='teams';
public function assembled(){
return $this->hasMany(User::class);
}
}
this is my route
Route::get('/createteams',['as'=>'/createteams','uses'=>'CreateTeamController@store']);
You have to create a new function to store data,
because in store
function you return view before inserting data in table.
public function store(Request $request)
{
return view('createteams');
}
public function storeData(Request $request){ //you can name function
$team=Team::create([
'team_name'=>$request->team_name,
'desc'=>$request->desc
]); // if fillable fields in model have same name as data coming in request
return redirect()->route("home");
}
also you will need to define two routes one to show form and one to store data
you have defined route to show form now define another route with post
method to store data and redirect.