我想在1个视图中显示来自2个表的数据。
我的用户模型:
public function profildosen()
{
return $this->belongsTo('App\Dosen');
}
Dosen模型:
public function user()
{
return $this->hasOne('App\User');
}
观点:
@foreach($dosen as $key => $value)
<strong>Kode Dosen :</strong> {{ $value->profildosen->iddosen }}<br>
<strong>Nama :</strong> {{ $value->profildosen->namedosen}}<br>
<strong>Alamat :</strong> {{ $value->profildosen->address}}<br>
<strong>No HP :</strong> {{ $value->phonenumber}} <br>
<strong>Email :</strong> {{ $value->email }}<br>
@endforeach
方法:
$dosen = User::paginate(5);
return view('admin/dosen.index', compact('dosen'));
并收到错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'dosen.id' in 'where clause' (SQL: select * from `dosen` where `dosen`.`id` is null limit 1) (View: D:\XAMPP\htdocs\infodosenku\resources\views\admin\dosen\index.blade.php)
什么是正确的方法?
更新
方案数据库
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username');
$table->string('email')->unique();
$table->string('password', 60);
$table->boolean('admin')->default(0);
$table->rememberToken();
$table->timestamps();
});
Schema::create('dosen', function (Blueprint $table) {
$table->string('id');
$table->string('user_id');
$table->string('nipy');
$table->string('namadosen');
$table->string('alamatdosen');
$table->integer('notelpdosen');
$table->timestamps();
});
路线:
Route::resource('/admin/dosen', 'AdminController',
['except' => ['show']]);
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Request;
use App\Http\Requests;
use App\Http\Requests\CreateDosenRequest;
use App\Dosen;
use App\User;
use Illuminate\Support\Facades\Input;
use DB;
class AdminController extends Controller
{
public function index()
{
// ambil semua data dosen
$dosen = User::paginate(5);
return view('admin/dosen.index', compact('dosen'));
}
public function create()
{
return view('admin/dosen.create');
}
public function store(CreateDosenRequest $request)
{
$user = User::create([
'name' => $request->input('name'),
'username' => $request->input('username'),
'email' => $request->input('email'),
'password' => bcrypt($request->input['password']),
'admin' => $request->input('admin')
]);
$dosen = Dosen::create([
'id' => $request->input('iddosen'),
'nipy' => $request->input('nipy'),
'namadosen' => $user->name,
'user_id' => $user->id,
'alamatdosen' => $request->input('alamatdosen'),
'notelpdosen' => $request->input('notelpdosen'),
]);
return redirect('admin/dosen')->with('message', 'Data berhasil ditambahkan!');
}
public function show($id)
{
$dosen = User::find($id);
return view('admin/dosen/show', compact('dosen'));
}
public function edit($id)
{
$dosen = User::find($id);
return view('admin.dosen.edit', compact('dosen'));
}
public function update($id)
{
$dosenUpdate = Request::all();
$dosen = User::find($id);
$dosen->update($dosenUpdate);
return redirect('admin.dosen')->with('message', 'Data berhasil diubah!');
}
public function destroy($id)
{
User::find($id)->delete();
return redirect('admin.dosen')->with('message', 'Data berhasil dihapus!');
}
}
我的观点:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Daftar Dosen</div>
<div class="panel-body">
<form class ="form-inline" action="{{ URL('/dosen/hasil/') }}" action="GET">
<div class="form-group">
<label for="cari">Cari Dosen </label>
<input type="text" class="form-control" id="cari" name="cari" placeholder="Masukan Nama Dosen">
</div>
<input class="btn btn-primary" type="submit" value="Cari">
</form><br>
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Nama</td>
<td>username</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
@foreach($dosen as $key => $value)
<tr>
<td>{{ $value->name }}</td>
<td>{{ $value->username}}</td>
<td>
{!! Form::open(['url' => 'dosen/' . $value->id . '/edit', 'style'=>'display:inline-block']) !!}
{!! Form::hidden('_method', 'GET') !!}
{{ Form::button('<i class="fa fa-pencil-square-o"></i>', ['type' => 'submit', 'class' => 'btn btn-warning', 'title' => 'Ubah'] ) }}
{!! Form::close() !!}
<button title="Tampilkan" type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal-{{ $value->id }}"><i class="fa fa-share"></i></button>
<!-- Modal -->
<div class="modal fade" id="myModal-{{ $value->id }}" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">{{ $value->namadosen }}</h4>
</div>
<div class="modal-body" style="overflow:auto;">
<strong>Kode Dosen :</strong> {{ $value->dosen->id }}<br>
<strong>NIP/NIPY :</strong> {{ $value->nipy }}<br>
<strong>Nama :</strong> {{ $value->namadosen }}<br>
<strong>Alamat :</strong> {{ $value->alamatdosen }}<br>
<strong>No HP :</strong> {{ $value->notelpdosen }} <br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
{!! Form::open(['url' => 'dosen/' . $value->id, 'style'=>'display:inline-block']) !!}
{!! Form::hidden('_method', 'DELETE') !!}
{{ Form::button('<i class="fa fa-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger', 'title' => 'Hapus'] ) }}
{!! Form::close() !!}
{!! Form::model($value, ['route' => ['admin.dosen.update', $value->id], 'method' => 'PUT']) !!}
</td>
</tr>
@endforeach
</tbody>
</table>
<h5><span class="label label-default">
Showing {!! $dosen->count() !!} results from total {!! $dosen->total() !!} results.
</span></h5>
<div> {!! $dosen->links() !!} </div>
</div>
</div>
</div>
</div>
</div>
@endsection
Dosen Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Dosen extends Model
{
protected $table = 'dosen';
protected $fillable = ['iddosen', 'namadosen', 'user_id', 'nipy', 'kodeprogdidosen','alamatdosen', 'notelpdosen', 'tempatlahirdosen', 'tanggallahirdosen', 'agamadosen', 'emaildosen', 'sandidosen', 'jkldosen', 'fotodosen'];
protected $casts = [
'iddosen' => 'varchar',
];
public function dosen()
{
return $this->belongsTo('App\Dosen');
}
}
用户模型:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'username', 'email', 'password', 'admin',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function profildosen()
{
return $this->belongsTo('App\Dosen');
}
}
答案 0 :(得分:0)
原因是您严重识别了您的表格ID,而不是 dosen.id 您应该使用 dosen.iddosen
所以查询应该是这样的:select * from dosen where iddosen is null limit 1
或者您可以使用Laravel Query Builder More
但最重要的是你需要知道你将加入你的牌桌的哪些栏目
希望它会有所帮助
答案 1 :(得分:0)
Eloquent通过检查名称来确定默认外键名称 关系方法和后缀方法名称与_id。
您的函数名称为profiledosen
,因此Laravel将假设您在Dosen Table中的id
字段作为主要标识符。
您只能将其从iddosen
更改为id
,然后将您的方法名称更改为
public function dosen()
{
return $this->belongsTo('App\Dosen');
}
或者,您可以为方法提供自定义主键,例如
public function profildosen()
{
return $this->belongsTo('App\Dosen', 'iddosen');
}