我尝试在laravel上创建我的第一个crud

时间:2015-04-27 01:38:53

标签: php laravel crud

我是Laravel框架中的新手,本周我尝试使用laravel 4.x创建CRUD应用程序我完成了代码,但我的应用程序可以保存输入。你能帮忙打破这个吗? 以下是我的模块

class ModuleAdd extends BaseModel  {

protected $table = 'tab_tes';
protected $primaryKey = 'id';

public function __construct() {
    parent::__construct();

}

public static function querySelect(  ){


    return "  SELECT tab_tes.* FROM tab_tes  ";
}
public static function queryWhere(  ){

    return " WHERE tab_tes.id IS NOT NULL   ";
}

public static function queryGroup(){
    return "  ";
}

这比我的控制器

class ModuleAddController extends BaseController {

protected $layout = "layouts.main";
protected $data = array();  
public $module = 'ModuleAdd';
static $per_page    = '10';

public function __construct() {
    parent::__construct();
    $this->beforeFilter('csrf', array('on'=>'post'));
    $this->model = new ModuleAdd();
    $this->info = $this->model->makeInfo( $this->module);
    $this->access = $this->model->validAccess($this->info['id']);

    $this->data = array(
        'pageTitle' =>  $this->info['title'],
        'pageNote'  =>  $this->info['note'],
        'pageModule'=> 'ModuleAdd',
        'trackUri'  => $this->trackUriSegmented()   
    );


} 


public function getIndex()
{
    if($this->access['is_view'] ==0) 
        return Redirect::to('')
            ->with('message', SiteHelpers::alert('error',Lang::get('core.note_restric')));

    // Filter sort and order for query 
    $sort = (!is_null(Input::get('sort')) ? Input::get('sort') : 'id'); 
    $order = (!is_null(Input::get('order')) ? Input::get('order') : 'asc');
    // End Filter sort and order for query 
    // Filter Search for query      
    $filter = (!is_null(Input::get('search')) ? $this->buildSearch() : '');
    // End Filter Search for query 

    // Take param master detail if any
    $master  = $this->buildMasterDetail(); 
    // append to current $filter
    $filter .=  $master['masterFilter'];


    $page = Input::get('page', 1);
    $params = array(
        'page'      => $page ,
        'limit'     => (!is_null(Input::get('rows')) ? filter_var(Input::get('rows'),FILTER_VALIDATE_INT) : static::$per_page ) ,
        'sort'      => $sort ,
        'order'     => $order,
        'params'    => $filter,
        'global'    => (isset($this->access['is_global']) ? $this->access['is_global'] : 0 )
    );
    // Get Query 
    $results = $this->model->getRows( $params );        

    // Build pagination setting
    $page = $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false ? $page : 1; 
    $pagination = Paginator::make($results['rows'], $results['total'],$params['limit']);        


    $this->data['rowData']      = $results['rows'];
    // Build Pagination 
    $this->data['pagination']   = $pagination;
    // Build pager number and append current param GET
    $this->data['pager']        = $this->injectPaginate();  
    // Row grid Number 
    $this->data['i']            = ($page * $params['limit'])- $params['limit']; 
    // Grid Configuration 
    $this->data['tableGrid']    = $this->info['config']['grid'];
    $this->data['tableForm']    = $this->info['config']['forms'];
    $this->data['colspan']      = SiteHelpers::viewColSpan($this->info['config']['grid']);      
    // Group users permission
    $this->data['access']       = $this->access;
    // Detail from master if any
    $this->data['masterdetail']  = $this->masterDetailParam(); 
    $this->data['details']      = $master['masterView'];
    // Master detail link if any 
    $this->data['subgrid']  = (isset($this->info['config']['subgrid']) ? $this->info['config']['subgrid'] : array()); 
    // Render into template
    $this->layout->nest('content','ModuleAdd.index',$this->data)
                    ->with('menus', SiteHelpers::menus());
}       


function getAdd( $id = null)
{

    if($id =='')
    {
        if($this->access['is_add'] ==0 )
        return Redirect::to('')->with('message', SiteHelpers::alert('error',Lang::get('core.note_restric')));
    }   

    if($id !='')
    {
        if($this->access['is_edit'] ==0 )
        return Redirect::to('')->with('message', SiteHelpers::alert('error',Lang::get('core.note_restric')));
    }               

    $id = ($id == null ? '' : SiteHelpers::encryptID($id,true)) ;

    $row = $this->model->find($id);
    if($row)
    {
        $this->data['row'] =  $row;
    } else {
        $this->data['row'] = $this->model->getColumnTable('tab_tes'); 
    }
    /* Master detail lock key and value */
    if(!is_null(Input::get('md')) && Input::get('md') !='')
    {
        $filters = explode(" ", Input::get('md') );
        $this->data['row'][$filters[3]] = SiteHelpers::encryptID($filters[4],true);     
    }
    /* End Master detail lock key and value */
    $this->data['masterdetail']  = $this->masterDetailParam(); 
    $this->data['filtermd'] = str_replace(" ","+",Input::get('md'));        
    $this->data['id'] = $id;
    $this->layout->nest('content','ModuleAdd.form',$this->data)->with('menus', $this->menus );  
}

function getShow( $id = null)
{

    if($this->access['is_detail'] ==0) 
        return Redirect::to('')
            ->with('message', SiteHelpers::alert('error',Lang::get('core.note_restric')));

    $ids = (is_numeric($id) ? $id : SiteHelpers::encryptID($id,true)  );
    $row = $this->model->getRow($ids);
    if($row)
    {
        $this->data['row'] =  $row;
    } else {
        $this->data['row'] = $this->model->getColumnTable('tab_tes'); 
    }
    $this->data['masterdetail']  = $this->masterDetailParam(); 
    $this->data['id'] = $id;
    $this->data['access']       = $this->access;
    $this->layout->nest('content','ModuleAdd.view',$this->data)->with('menus', $this->menus );  
}   

function postSave( $id =0)
{
    $trackUri = $this->data['trackUri'];
    $rules = $this->validateForm();
    $validator = Validator::make(Input::all(), $rules); 
    if ($validator->passes()) {
        $data = $this->validatePost('tab_tes');
        $ID = $this->model->insertRow($data , Input::get('id'));
        // Input logs
        if( Input::get('id') =='')
        {
            $this->inputLogs("New Entry row with ID : $ID  , Has Been Save Successfull");
            $id = SiteHelpers::encryptID($ID);
        } else {
            $this->inputLogs(" ID : $ID  , Has Been Changed Successfull");
        }
        // Redirect after save  
        $md = str_replace(" ","+",Input::get('md'));
        $redirect = (!is_null(Input::get('apply')) ? 'ModuleAdd/add/'.$id.'?md='.$md.$trackUri :  'ModuleAdd?md='.$md.$trackUri );
        return Redirect::to($redirect)->with('message', SiteHelpers::alert('success',Lang::get('core.note_success')));
    } else {
        return Redirect::to('ModuleAdd/add/'.$id.'?md='.$md)->with('message', SiteHelpers::alert('error',Lang::get('core.note_error')))
        ->withErrors($validator)->withInput();
    }   

}

public function postDestroy()
{

    if($this->access['is_remove'] ==0) 
        return Redirect::to('')
            ->with('message', SiteHelpers::alert('error',Lang::get('core.note_restric')));      
    // delete multipe rows 
    $this->model->destroy(Input::get('id'));
    $this->inputLogs("ID : ".implode(",",Input::get('id'))."  , Has Been Removed Successfull");
    // redirect
    Session::flash('message', SiteHelpers::alert('success',Lang::get('core.note_success_delete')));
    return Redirect::to('ModuleAdd?md='.Input::get('md'));
}   

我正在使用php 5.x和Apache2和mysql 5.x. 请帮忙。

1 个答案:

答案 0 :(得分:1)

如果你是新手,那么我建议放弃Laravel 4并改为使用Laravel 5.

你还需要做一些阅读,特别是关于Eloquent ORM http://laravel.com/docs/5.0/eloquent,然后是来自http://laravel.com/docs/5.0/controllers#restful-resource-controllers的RESTful控制器