2016-09-23 10 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);を使用して試しましたが、COLUMN PROJECTS_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', 
    ]; 
} 

PROJECTSコントローラ

<?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

ですから、上記のコードで配列を作成where're? 'airattributes'を作成し、projects_airモデルに適切な' fillable'属性があることを確認してください。 –

+0

モデルコードを追加できますか?私は問題が何であるか正確にはわかっていないのではないかと心配しています。 – ollieread

+0

質問が更新されました – dailyadd

答えて

0

をここにあなたがインターフェイスを表示することができ、ITはPROJECT_IDしなければならないが、私はエラーが

IS WHERE知りませんモデル内のリレーションを定義します。

class Projects extends Model { 
    public function air() { 
     return $this->hasMany('App\Projects_air'); 
    } 
} 
あなたのコントローラで

そして:

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

どこで$ airを定義する必要がありますか? – dailyadd

+0

あなたはそれを定義する必要はありません、新しい空気プロジェクトを作成した – Sherif

+0

彼らは両方が異なるテーブルを持っている:空気は異なるテーブルとプロジェクトは異なるテーブルを持っています。 – dailyadd

関連する問題