简体   繁体   中英

Create and/or update table in database in Laravel 4 from server

I am mobile app(Android/iOS) developer, and in the absence of our php developer, I want to add another table in our database and POST and GET some data and store in new table.

Laravel 4 is installed in our server, and with some basic cPanel, phpMyAdmin and php information I tried to create a new table in our existing database.

first I tired and created a table using phpMyAdmin, then I added Model file like this

<?php

class Record extends Eloquent {

protected $table = 'requestRecords';
protected $guarded = array();
public $timestamps = true;

}

But, when i tried add data to requestRecords in my route.php file (which i created manually using phpMyAdmin) using this syntax, nothing happened:

Route::post('api/v1/FLRequest', function()
 {

//check right inputs sent to us.
if( !Request::has('userId')     or
    !Request::has('userName')   or
    !Request::has('timeStamp')  or
    !Request::has('requestFor') or
    !Request::has('nKey'))
    return Response::json(array(
        'status' => "error",
        'message' => "Missing Parameter"),
        500
    );


$inputs = Request::all();
if($inputs['nKey']!= "nKey_Goes_Here")
    return Response::json(array(
        'status' => "error",
        'message' => "Wrong Request"),
        500
    );


    $addRecord = Record::create(array(
                                'userName' => $inputs['userName']));

if($addRecord!==FALSE)
{
    return Response::json(array(
        'status' => "success",
        'message' => "Record Updated"),
        200
    );
}
else
{
    return Response::json(array(
        'status' => "error",
        'message' => "Failed Updating Record"),
        401
    );
}

Actually I did not get neither 200 nor 401, but I got HTTP 500 Internal Server Error .

Then I tried adding table using schema, so I created a php file in database/migrations folder, named: 2015_01_02_104814_create_requestRecords_table.php and add these codes there :

<?php

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

class CreateRequestRecordsTable extends Migration {

public function up()
{
    Schema::create('requestRecords', function($table) {
        $table->increments('id');
        $table->string('userId',255);
        $table->string('userName', 255);
        $table->string('timeStamp', 255);
        $table->string('payLoad',255);
        $table->string('requestFor',255);
        $table->string('bToken',255);
        $table->string('bSituation',255);

    });
}

public function down()
{
    Schema::drop('requestRecords');
}
}

and again after trying to add data, I did not get neither 200 nor 401, but I got HTTP 500 Internal Server Error .

For more details here is my Controller and Model :

Controller in file /app/controllers/RequestRecordsController.php :

<?php 
            class RequestRecordsController extends BaseController {

              /**
               * Display a listing of the resource.
               *
               * @return Response
               */
              public function index()
              {

              }

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

              }

              /**
               * Store a newly created resource in storage.
               *
               * @return Response
               */
              public function store()
              {

              }

              /**
               * Display the specified resource.
               *
               * @param  int  $id
               * @return Response
               */
              public function show($id)
              {

              }

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

              }

              /**
               * Update the specified resource in storage.
               *
               * @param  int  $id
               * @return Response
               */
              public function update($id)
              {

              }

              /**
               * Remove the specified resource from storage.
               *
               * @param  int  $id
               * @return Response
               */
              public function destroy($id)
              {

              }

            }

            ?>

and Model in /app/models/Record.php :

<?php
class Record extends Eloquent {
protected $table = 'requestRecords';
protected $guarded = array();
public $timestamps = true;

}

What am I doing wrong?

Thanks :)

You are adding lots of validation and always throwing a 500 error yourself on the application.

A best practice would be for you to remove all of that, try to make things work first, and after that. When you see that the code works, then you add validation to make sure that every field you want is on the form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM