2
votes

I am very new in Laravel.I am trying to insert data in one of my table and i am using mysql.Now this is my model,

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class BloodDonner extends Eloquent implements UserInterface, RemindableInterface {


    protected $fillable=array
    (   'name',
        'email',
        'blood_group',
        'phone_number',
        'location',
        'date_of_birth',
        'last_date_of_donation'
    );


}

this is my migration file

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class BloodDonner extends Migration {


public function up()
{
    Schema::create('bloodDonners', function(Blueprint $table)
    {
        $table->increments('id');
        $table->unsignedInteger('donner_id');
        $table->foreign('donner_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
        $table->string('donner_name');
        $table->string('email')->unique();
        $table->string('blood_group');
        $table->string('phone_number')->unique();
        $table->string('location');
        $table->date('date_of_birth');
        $table->date('last_date_of_donation');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('bloodDonners');
}

}

and this is my controller

<?php

class BloodController extends \BaseController {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */

public function index()
{
    //return View::make('bloodDonation.donners');
}


public function indexBloodDonation()
{
    return View::make('bloodDonation.donners');
}

public function getBloodDonation()
{
    return View::make('bloodDonation.blood-donation-form');
}

public function postBloodDonation()
{
    $validator = Validator::make(Input::all(),array
        (
            'donner_name' => 'required|min:3|max:255',
            'date_of_birth' => 'required',
            'blood_group' => 'required',
            'location' => 'required',
            'email'=> 'required|email|max:255|unique:blooddonners',
            'phone_number' => 'required|unique:blooddonners',
            'last_date_of_donation' => 'required'
        ));
    if($validator->fails())
    {
        return Redirect::route('donation-form')
             ->withErrors($validator)
             ->withInput();


    }
    else
    {
        $donner_name = Input::get('donne_name');
        $date_of_birth = Input::get('date_of_birth');
        $blood_group = Input::get('blood_group');
        $location = Input::get('location');
        $email = Input::get('email');
        $phone_number = Input::get('phone_number');
        $last_date_of_donation = Input::get('last_date_of_donation');

        $bloodDonner = BloodDonner::create(array(
                'donner_name' => $donner_name,
                'date_of_birth' => $date_of_birth,
                'blood_group' => $blood_group,
                'location' => $location,
                'email' => $email,
                'phone_number' => $phone_number,
                'last_date_of_donation'=> $last_date_of_donation
            )
        );

        if($bloodDonner){
            return Redirect::route('donation-form')->with('global','You have Successfully registered as Blood Donner!');
        }
    }
}

and this is the route

<?php

Route::get('/donners',array
(
 'as' => 'donners',
 'uses' => 'BloodController@indexBloodDonation'
));


Route::get('/blood-donation-form',array(
    'as' => 'donation-form',
    'uses' => 'BloodController@getBloodDonation'    
   ));



/* 
/Authenticated Group
*/
 Route::group(array('before' => 'auth'),function(){
   /* 
   /Sign Out(GET)
   */

    Route::get('/signout',array
        (
            'as' => 'signout',
            'uses' => 'AccountController@signoutGet'
        ));


    /*
    /csrf protection
    */
    Route::group(array('before' => 'csrf'),function()
     {
        /*
        / Blood Donation (POST)
        */
        Route::post('/donner-registration',array
            (
                'as' => 'blood-donation-post',
                'uses' => 'BloodController@postBloodDonation'
            ));

    });

        /*
        / Blood Donation (GET)
        */
        Route::get('/blood-donation-form',array
            (
              'as' => 'donation-form',
              'uses' => 'BloodController@getBloodDonation'
            ));



 });

and this is my view

@extends('layouts.base')

@section('content')
    @if(Session::has('global'))
        <p>{{Session::get('global')}}</p>
    @endif
<!--==========END OF BLOOD DONNER=============-->
    <div class="container" style="margin-bottom:30px; margin-top:5px">
        <ul class="nav nav-tabs">
          <li class="active"><a href="{{URL::route('donners')}}" ><strong>DONNERS</strong></a></li>
          <li><a href="{{URL::route('donation-form')}}" ><strong>DONATE BLOOD</strong></a></li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">     
      <!--<div class="tab-pane fade" id="donate"> --><!--==========BLOOD   DONATION=============-->
        <div class="container">
            <div class="col-md-5 col-sm-5 hidden-xs"  style="margin-top:20px;">
                <img class="img-responsive" src="img/save-life-1.jpg" alt="blood-donation">
            </div>
            <div class="col-md-7 col-sm-7 col-xs-12" style="margin-top:20px;">

                <form class="form-horizontal" role="form" action="{{URL::route('blood-donation-post')}}" method="post">

                  <div class="form-group">
                    <label for="inputEmail3" class="col-sm-4 control-label">Name</label>
                    <div class="col-sm-8">
                      <input type="text" class="form-control" id="inputEmail3" placeholder="Name" name='donner_name'{{(Input::old('donner_name')) ? ' value="'.e(Input::old('donner_name')).'"' : ''}}>
                      @if($errors->has('donner_name'))
                       <span style="color:red">{{$errors->first('donner_name')}}</span>
                      @endif

                    </div>
                  </div>

                  <div class="form-group">
                    <label for="inputEmail3" class="col-sm-4 control-label">Date of Birth</label>
                    <div class="col-sm-8">
                      <input type="date" class="form-control" id="inputEmail3" placeholder="Age" name='date_of_birth'{{(Input::old('age')) ? ' value="'.e(Input::old('age')).'"' : ''}}>
                       @if($errors->has('date_of_birth'))
                        <span style="color:red">{{$errors->first('date_of_birth')}}</span>
                       @endif
                    </div>
                  </div>

                  <div class="form-group">
                    <label for="inputEmail3" class="col-sm-4 control-label">Blood Group</label>

                    <div class="col-sm-8">
                      <select class="form-control" name='blood_group'>

                          <option>You Must Select your Blood Group</option>

                          <option>O-</option>
                          <option>O+</option>
                          <option>A-</option>
                          <option>A+</option>
                          <option>B-</option>
                          <option>B+</option>
                          <option>AB-</option>
                          <option>AB+</option>
                      </select>
                      @if($errors->has('blood_group'))
                         <span style="color:red">{{$errors->first('blood_group')}}</span>
                        @endif


                    </div>


                  </div>


                  <div class="form-group">
                    <label for="inputEmail3" class="col-sm-4 control-label">Location</label>
                    <div class="col-sm-8">
                      <select class="form-control" name='location'> 
                          <option>GAIBANDHA</option>
                          <option>KURIGRAM</option>
                          <option>LALMONIRHAT</option>
                          <option>NILPHAMARI</option>
                          <option>PANCHAGARH</option>

                      </select>
                       @if($errors->has('location'))
                         <span style="color:red">{{$errors->first('location')}}</span>
                       @endif
                    </div>
                  </div>

                  <div class="form-group">
                    <label for="inputEmail3" class="col-sm-4 control-label">Email</label>
                    <div class="col-sm-8">
                      <input type="email" class="form-control" id="inputEmail3" placeholder="Email" name='email'{{(Input::old('email')) ? ' value="'.e(Input::old('email')).'"' : ''}}>
                       @if($errors->has('email'))
                        <span style="color:red">{{$errors->first('email')}}</span>
                       @endif
                    </div>
                  </div>

                  <div class="form-group">
                    <label for="inputEmail3" class="col-sm-4 control-label">Phone Number</label>
                    <div class="col-sm-8">
                      <input type="text" class="form-control" id="inputEmail3" placeholder="Phone Number" name='phone_number'{{(Input::old('phone_number')) ? ' value="'.e(Input::old('phone_number')).'"' : ''}}>
                       @if($errors->has('phone_number'))
                        <span style="color:red">{{$errors->first('phone_number')}}</span>
                       @endif
                    </div>
                  </div>

                  <div class="form-group">
                    <label for="inputEmail3" class="col-sm-4 control-label">Last Date of Blood Donation</label>
                    <div class="col-sm-8">
                      <input type="date" class="form-control" id="inputEmail3" placeholder="Last Date of Blood Donation" name='last_date_of_donation'{{(Input::old('donate_date')) ? ' value="'.e(Input::old('donate_date')).'"' : ''}}>
                    @if($errors->has('last_date_of_donation'))
                      <span style="color:red">{{$errors->first('last_date_of_donation')}}</span>
                    @endif
                    </div>
                  </div>
                  <div style="padding-left:20%">
                    <button type="submit" class="btn btn-default ">Submit</button>
                    {{ Form::token() }}
                </div>

                </form>

            </div>
        </div>
      <!-- </div> --><!--==========END OF BLOOD DONATION=============-->

    </div>
 </div>

@stop

and this is my laravel.log file(most last portion)

#3 C:\xampp\htdocs\Needa\app\controllers\BloodController.php(41):     Illuminate\Support\Facades\Redirect::route('blood-donation-...')
#4 [internal function]: BloodController->postBloodDonation()
#5     C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(231):   call_user_func_array(Array, Array)
#6   C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.   php(93): Illuminate\Routing\Controller->callAction('postBloodDonati...', Array)

#7 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher. php(62): Illuminate\Routing\ControllerDispatcher->call(Object(BloodController), Object(Illuminate\Routing\Route), 'postBloodDonati...') #8 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\Router.php(930): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'BloodController', 'postBloodDonati...') #9 [internal function]: Illuminate\Routing\Router->Illuminate\Routing{closure}() #10 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\Route.php(105): call_user_func_array(Object(Closure), Array) #11 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\Router.php(996): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request)) #12 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\Router.php(964): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) #13 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(73 8): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) #14 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(70 8): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request)) #15 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Session\Middleware.php(72): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true) #16 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Cookie\Queue.php(47): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true) #17 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Cookie\Guard.php(51): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true) #18 C:\xampp\htdocs\Needa\vendor\stack\builder\src\Stack\StackedHttpKernel.php(23): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true) #19 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(60 6): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request)) #20 C:\xampp\htdocs\Needa\public\index.php(49): Illuminate\Foundation\Application->run() #21 C:\xampp\htdocs\Needa\server.php(19): require_once('C:\xampp\htdocs...') #22 {main} [] [] [2014-08-08 14:57:19] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'syntax error, unexpected '}', expecting variable (T_VARIABLE) or '$'' in C:\xampp\htdocs\Needa\app\controllers\BloodController.php:51 Stack trace: #0 [internal function]: Illuminate\Exception\Handler->handleShutdown() #1 {main} [] [] [2014-08-08 15:21:15] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'syntax error, unexpected ';', expecting ')'' in C:\xampp\htdocs\Needa\app\controllers\BloodController.php:58 Stack trace: #0 [internal function]: Illuminate\Exception\Handler->handleShutdown() #1 {main} [] [] [2014-08-10 16:12:32] production.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:1 46 Stack trace: #0 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1017): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request)) #1 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\Router.php(985): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request)) #2 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\Router.php(964): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) #3 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(73 8): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) #4 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(70 8): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request)) #5 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Session\Middleware.php(72): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true) #6 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Cookie\Queue.php(47): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true) #7 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Cookie\Guard.php(51): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true) #8 C:\xampp\htdocs\Needa\vendor\stack\builder\src\Stack\StackedHttpKernel.php(23): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true) #9 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(60 6): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request)) #10 C:\xampp\htdocs\Needa\public\index.php(49): Illuminate\Foundation\Application- >run() #11 C:\xampp\htdocs\Needa\server.php(19): require_once('C:\xampp\htdocs...') #12 {main} [] []

if you make a look in my Controller,then you will notice that i have added a Validation In my postBloodDonation function and this Validation is working well if validation fails,but if validation success , then posted data is not inserted in my database .In controller, the else statement is not executing,Why its happening,whats the problem?

5
Please update to include your view.bishop
i have updated my post.Tanveer
Off-hand, everything looks correct. What happens when you input all valid data? Anything in app/storage/logs/laravel.log? Anything on screen? Are you certain the else branch is even being called?bishop
after submit the validated data,it redirect to empty pageTanveer
my migration contain a foreign key,am i place it right?,if not,is that the issue,what do you think?Tanveer

5 Answers

1
votes

try mass Assignment . and set fillable array in model, BloodDonner::save(Request::all());

and if gives error than prepend 'Rquest and BloodDoner' with respective namespaces... and also update ur project with composer.

0
votes

What if you put

Session::flash('global','it is ok');
//then redirect to home 

immediately after that else statement, will you see it?

0
votes

Try this approach

$b = new BloodDonner();

$b->donner_name = $donner_name;
$b->date_of_birth = $date_of_birth;
$b->blood_group = $blood_group;
$b->location = $location;
$b->email = $email;
$b->phone_number = $phone_number;
$b->last_date_of_donation =

$last_date_of_donation;

        if($b->save()){
            return Redirect::route

('donation-form')->with('global','You have Successfully registered as Blood Donner!');

0
votes

In your controller, try replacing the folowing :

$donner_name = Input::get('donne_name');

with

$donner_name = Input::get('donner_name');
0
votes

The problem could be that the date fields are not being formatted correctly to fit the MySQL Date type field, luckily Laravel 5.1 has mutators built into Eloquent that can convert these into the correct format for you when inserts and updates are performed.

You can find out how to use this here: https://laravel.com/docs/5.1/eloquent-mutators#date-mutators