2016-09-23 5 views
-1

그러면 나를위한 새 프로젝트가 만들어집니다. 그러나 내가하고 싶은 일은 projects_air라는 다른 데이터베이스 테이블에 속한 추가 필드를 데이터베이스에 추가하고 해당 프로젝트에 대한 세부 정보를 추가하는 것입니다. 데이터베이스에 삽입 할데이터를 데이터베이스에 삽입 - LARAVEL

public function newProject(Request $request) 
    { 
     $data = $request->all(); 

     $attributes = []; 
     $attributes['title'] = $data['title']; 
     $attributes['start_date'] = date("Y-m-d h:i:s", strtotime($data['start_date'])); 
     $attributes['end_date'] = date("Y-m-d h:i:s", strtotime($data['end_date'])); 
     $attributes['created_by'] = Auth::user()->id; 
     $attributes['description'] = $data['description']; 
     $attributes['air'] = '10'; 
     $attributes['water'] = '19'; 
     $attributes['lat'] = $data['lat']; 
     $attributes['lng'] = $data['lng']; 



//  var_dump($attributes); 
//  return; 

     $project = Projects::create($attributes); 
     $air = Projects_air::create($airattributes); 

     if($project) 
      return redirect('home')->with('success', 'Project added successfully'); 

     var_dump($data); 
     return; 
    } 

추가 데이터 :

 $airattributes['dust'] = $data['dust']; 
    $airattributes['noise'] = $data['noise']; 
    $airattributes['temperature'] = $data['temperature']; 
    $airattributes['radiation'] = $data['radiation']; 

나는 이러한 데이터는 또한 그들이 모두 (프로젝트 테이블과 projects_air 테이블) 열이 "PROJECT_ID"

라는 한 프로젝트에 속해 있음을 원하는

나는이 $air = $project->air()->create($airattributes);을 사용했지만 열의 PROJECTS_ID가 존재하지 않는다는 오류 메시지를 받았습니다. IT는 PROJECT_ID해야하지만 오류가 여기

어디 인터페이스 볼 수 있습니다 잘 모릅니다 : http://188.166.166.143/projects/add

UPDATE : 에어 모델 :

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Air extends Model 
{ 
    protected $table = 'projects_air'; 
    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'project_id', 'temperature', 'radiation', 'dust', 'noise' 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'id', 
    ]; 
} 

프로젝트 컨트롤러

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Projects; 
use App\Enroll; 
use View; 
use Auth; 
use App\Air; 

class ProjectsController extends Controller 
{ 
    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct(Projects $projects) 
    { 
     $this->middleware('auth'); 
     $this->projects = $projects; 
    } 

    // Add new project 
    public function addProject() 
    { 
     return View::make('projects/add_project'); 
    } 


    // Process new project 
    public function newProject(Request $request) 
    { 
     $data = $request->all(); 

     $attributes = []; 
     $attributes['title'] = $data['title']; 
     $attributes['start_date'] = date("Y-m-d h:i:s", strtotime($data['start_date'])); 
     $attributes['end_date'] = date("Y-m-d h:i:s", strtotime($data['end_date'])); 
     $attributes['created_by'] = Auth::user()->id; 
     $attributes['description'] = $data['description']; 
     $attributes['air'] = '10'; 
     $attributes['water'] = '19'; 
     $attributes['lat'] = $data['lat']; 
     $attributes['lng'] = $data['lng']; 
     $airattributes['dust'] = $data['dust']; 
     $airattributes['noise'] = $data['noise']; 
     $airattributes['temperature'] = $data['temperature']; 
     $airattributes['radiation'] = $data['radiation']; 
     $airattributes['project_id'] = $data['project_id']; 


//  var_dump($attributes); 
//  return; 

     $project = Projects::create($attributes); 
     $air = $project->air()->create($airattributes); 

     var_dump($air); 
     return; 


     if($project) 
      return redirect('home')->with('success', 'Project added successfully'); 

     var_dump($data); 
     return; 
    } 

    // Show all projects 
    public function showProjects() 
    { 

     $data = Projects::get(); 
     return View::make('projects/list_projects')->with('projects', $data); 

    } 

    // Show single project 
    public function showSingleProject($id) 
    { 
     if(is_null($id)) 
      return back()->with('error', 'Invalid project'); 


     $project = Projects::where('id', $id)->first(); 

     if(is_null($project)) 
      return back()->with('error', 'Project not found'); 


     return View::make('projects/single_project2')->with('project', $project); 

    } 

    // Show single project 
    public function showEditProject($id) 
    { 
     if(is_null($id)) 
      return back()->with('error', 'Invalid project'); 


     $project = Projects::where('id', $id)->first(); 

     if(is_null($project)) 
      return back()->with('error', 'Project not found'); 


     $hasAccess = 0; 


     if(!empty($project->enrolls)) 
     { 
      foreach($project->enrolls as $enroll) 
      { 

       if($enroll->user_id == Auth::user()->id) 
       { 
        $hasAccess = 1; 
        break; 
       } 


      } 
     } 


     if($project->created_by == Auth::user()->id) 
      $hasAccess = 1; 



     if($hasAccess != 1) 
      return back()->with('error', 'You are not allowed to edit this project'); 


     return View::make('projects/edit_project')->with('project', $project); 

    } 




    // Show single project 
    public function showDeleteProject($id) 
    { 
     if(is_null($id)) 
      return back()->with('error', 'Invalid project'); 


     $project = Projects::where('id', $id)->first(); 

     if(is_null($project)) 
      return back()->with('error', 'Project not found'); 


     if($project->created_by != Auth::user()->id) 
      return back()->with('error', 'You are not the owner of this project'); 


     return View::make('projects/delete_project')->with('project', $project); 

    } 


    public function processDeleteProject(Request $request) 
    { 
     $data = $request->all(); 

     if(!is_null($data['pk'])) 
      Projects::where('id', $data['pk'])->delete(); 


     return redirect('home')->with('success', 'Project deleted successfully'); 

    } 

    public function enrollToProject($id) 
    { 
     if(is_null($id)) 
      return back()->with('error', 'Invalid project'); 

     $userId = Auth::user()->id; 
     $attributes = []; 
     $attributes['user_id'] = $userId; 
     $attributes['project_id'] = $id; 

     $enrolled = Enroll::create($attributes); 

     if($enrolled) 
      return back()->with('success', 'You have successfully enrolled to this project'); 

    } 


    public function showImportView() 
    { 

     return View::make('projects/import_project'); 
    } 



    public function processImport(Request $request) 
    { 

     $data = $request->all(); 

     if($data['file']) 
     { 
      $csvData = file_get_contents($data['file']); 
      $lines = explode(PHP_EOL, $csvData); 
      $csv = array_map('str_getcsv', $lines); 
      $csv = array_shift($csv); 

      if(is_null($csv)) 
       return back()->with('error', 'Its Empty'); 



      $attributes = []; 
      $attributes['title'] = $csv[0]; 
      $attributes['start_date'] = date("Y-m-d h:i:s", strtotime($csv[1])); 
      $attributes['end_date'] = date("Y-m-d h:i:s", strtotime($csv[2])); 
      $attributes['created_by'] = Auth::user()->id; 
      $attributes['description'] = $csv[3]; 
      $attributes['air'] = $csv[4]; 
      $attributes['water'] = $csv[5]; 


      $project = Projects::create($attributes); 

      if($project) 
       return redirect('home')->with('success', 'Project imported successfully'); 

//   var_dump($attributes); 
//   echo '<pre />'; 
//   return; 
     } 


    } 


} 
+0

를? 'airattributes'를 생성하고 projects_air 모델에 적절한'fillable' 속성이 있는지 확인하십시오 –

+0

모델 코드를 추가 할 수 있습니까? 문제가 정확히 무엇인지 잘 모르겠습니다. – ollieread

+0

질문이 업데이트되었습니다. – dailyadd

답변

0

을 모델의 관계를 정의하십시오.

컨트롤러에서

: 그리고 위의 코드에서 배열을 생성 그래서 어디야

$project = Projects::create($attributes); 
$air = $project->air()->create($airattributes); 
+0

어디에서 $ air를 정의해야합니까? – dailyadd

+0

당신은 그것을 정의 할 필요가 없습니다, 당신은 새로운 공기 프로젝트를 만들었습니다 – Sherif

+0

그들은 서로 다른 테이블을 가지고 있습니다 : 공기가 다른 테이블과 프로젝트는 다른 테이블을 가지고 있습니다. – dailyadd