我有2张表是产品和ps(卖家)。我想制作一个列出所有卖家产品的页面。我使用if else语句,但它不起作用。我是laravel的新人,请帮助我。
PsController.php
public function show(){
$ps = DB::table('ps')->get();
return view('viewps', ['ps' => $ps]);
}
public function view($id){
$ps = Ps::find($id)
return view ('views')->with('ps', $ps);
}
public function sellitem(){
$id = DB::table('ps')->get();
$ps_id = DB::table('product')->get();
return view('sellitem', ['ps_id' => $ps_id,'id'=>$id]);
}
sellitem.blade.php
@if ($ps_id == $id)
<div class="searchable-container">
<div class="items col-xs-12 col-sm-6 col-md-6 col-lg-6 clearfix">
<div class="info-block block-info clearfix">
<div class="square-box pull-left">
<span class="glyphicon glyphicon-user glyphicon-lg"></span>
</div>
<h5>{{$ps_id->name}}</h5>
<h5><img src="{{ asset('images/' . $ps_id->image) }}" height="100" width="100"/> </h5>
<h5>Sold by : {{$ps_id->ps_name}}</h5>
<h4> <div class="panel-body"> </h4>
</div>
</div>
@else
No records found !
@endif
Route::get('/viewps', 'PsController@show');
Route::get('/viewps/{id}', 'PsController@view')->name('view');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/sellitem/{ps_id}', 'PsController@sellitem')->name('sellitem');
更新
PS model
class Ps extends Authenticatable{
use Notifiable;
protected $fillable = [
'name','lastname','email', 'password', 'username','phone','gender'
];
protected $hidden = [
'password', 'remember_token',
];
public function product(){
return $this->hasMany('Product::class'); }
产品型号
class Product extends Model{
public $table = "Product";
protected $fillable =
['name','description','size','image','price','type','ps_id','ps_name'];
protected $hidden = [
'password', 'remember_token',
];
public function ps(){
return $this->belongsTo('Ps::class');
}
新更新 我想要做的是,从viewps页面将有一个到他们的销售项目的链接,这是sellitem。
viewps.blade
<div class="box box-info">
<div class="box-body">
<div class="col-sm-6">
<div align="center"> <img alt="User Pic" src="https://x1.xingassets.com/assets/frontend_minified/img/users/nobody_m.original.jpg" id="profile-image1" class="img-circle img-responsive">
<input id="profile-image-upload" class="hidden" type="file">
</div>
<br>
</div>
<div class="col-sm-6">
<h4 style="color:#00b1b1;">{{ucfirst(trans($ps->name))}} {{ucfirst(trans($ps->lastname))}}</h4></span>
<span><p>Rating</p></span>
<span><p><a href="{{ route('sellitem', $ps->id) }}">Lists of Sell Items</a></p></span>
</div>
Sellitem.blade
@if (count($sellProducts) > 0)
@foreach ($ps as $sellProducts)
<div class="searchable-container">
<div class="items col-xs-12 col-sm-6 col-md-6 col-lg-6 clearfix">
<div class="info-block block-info clearfix">
<div class="square-box pull-left">
<span class="glyphicon glyphicon-user glyphicon-lg"></span>
</div>
<h5>{{$ps->name}}</h5>
<h5><img src="{{ asset('images/' . $ps->image) }}" height="100" width="100"/> </h5>
<h5>Sold by : {{$p->ps_name}}</h5>
<h4> <div class="panel-body"> </h4>
</div>
</div>
@endforeach
@else
No records found !
@endif
</div>
@endsection
答案 0 :(得分:0)
使用以下查询(我们也可以在select语句中使用DB::raw
来计算sum or
count(*)`)
public function sellitem($id){
$ps = Ps::find($id);
$query = DB::table('ps')
->select('product.id as id',
'product.name as name',
'product.description as description',
'product.type as type',
'product.ps_id as ps_id',
'product.ps_name as ps_name',
'product.size as size',
'product.price as price'
))
->join('product', 'product.ps_id', '=', 'ps.id')
->where('product.ps_id','=', $id)
;
$results = $query->get();
return view('sellitem', ['sellProducts' => $results,'ps'=> $ps]);
}
然后在sellitem.blade.php
使用for循环从sellProducts获取值(在这种情况下,不需要if条件)
@if (count($sellProducts) > 0)
@foreach ($sellProducts as $sp )
<div class="searchable-container">
<div class="items col-xs-12 col-sm-6 col-md-6 col-lg-6 clearfix">
<div class="info-block block-info clearfix">
<div class="square-box pull-left">
<span class="glyphicon glyphicon-user glyphicon-lg"></span>
</div>
<h5>{{$sp->name}}</h5>
<h5><img src="{{ asset('images/' . $sp->image) }}" height="100" width="100"/> </h5>
<h5>Sold by : {{$sp->ps_name}}</h5>
<h4> <div class="panel-body"> </h4>
</div>
</div>
@endforeach
@else
No records found !
@endif