在此之前,我想制作一个像Google教室这样的网络应用。 (在本例中为“加入教室”部分)。
我的代码的结果是:
“ Illuminate \ Database \ Eloquent \ Builder类的对象无法 转换为字符串”
查看:
<form action="{{ route('user.classroom.joinclassroom') }}">
@csrf
<label class="sr-only">Classroom Code</label>
<div class="input-group">
<input method="post" type="text" name="classroom_code" class="form-control" placeholder="eg.XIRPL301 | Max 8 Char/Num" aria-label="Recipient's username" aria-describedby="basic-addon2">
<div class="input-group-append">
<button name="submit" class="btn btn-sm btn-gradient-primary" type="submit">Search</button>
</div>
</div>
</form>
@foreach($classrooms->chunk(4) as $items)
<div class="row">
@foreach($items as $p)
<div class="col-md-4 stretch-card grid-margin">
<div class="card bg-gradient-danger card-img-holder text-white">
<div class="card-body">
<img src="{{ asset('classofus_resource/images/dashboard/circle.svg') }}" class="card-img-absolute" alt="circle-image"/>
<h4 class="font-weight-normal mb-3">{{ $p->name }}
<i class="mdi mdi-account-multiple mdi-24px float-right"></i>
</h4>
<h4 class="mb-5" data-toggle="tooltip" title="{{ $p->description }}">{{ str_limit($p->description, 32) }}</h4>
<form method="post" action="{{ route('user.classroom.joinclassroombutton') }}">
@csrf
<button type="submit" name="submit" class="btn btn-gradient-primary btn-sm">Join</button>
</form>
</div>
</div>
</div>
@endforeach
</div>
@endforeach
关系:
多对多
注册控制器:
public function searchclassroom(Request $request)
{
$this->cari = $request -> classroom_code;
$classrooms = Classroom::wherein('classroom_code', [$this->cari]) -> get();
return view('pages.user.classroom.joinclassroom',['classrooms' => $classrooms]);
}
/* --------------------------------------- End Of Search Classroom -------------------------------------------- */
/* --------------------------------------- Join Classroom Button ----------------------------------------- */
public function joinclassroombutton(request $request)
{
$enrollment = Enrollment::create([
"classroom_id" => Classroom::select('id')->where('classroom_code', $this->cari),
'user_id' => Auth::user()->id
]);
}
关键是,如何用教室代码获取带有值的教室ID的语法?请帮助我...
答案 0 :(得分:0)
问题在这里,您在这里获得教室ID。您想获取ID,但是却拥有Builder
实例
"classroom_id" => Classroom::select('id')->where('classroom_code', $this->cari), // Builder
因此,您可以尝试:
"classroom_id" => Classroom::where('classroom_code', $this->cari)->first()->id, // just id of your model