【问题标题】:How can I get the list sell item of the seller?我怎样才能得到卖家的清单出售物品?
【发布时间】:2017-09-09 04:18:37
【问题描述】:

我有 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]);
  }

selitem.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');

Product databaseps database

更新

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>

Selitem.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

【问题讨论】:

  • @if ($ps_id == $id) 你为什么要比较对象?使用$ps_id-&gt;id == $id-&gt;id 也请分享您的模型。
  • 在 if 条件 $ps_id-&gt;ps_id == $id-&gt;id 中使用它并告诉我它是否有效
  • 你还缺少一个结束 &lt;/div&gt; 那里。 searchable-container 没有结束标签。
  • “它不起作用”是什么意思?请阅读How to Ask 并清楚说明预期的行为以及它如何偏离。
  • 是的,我需要大量学习.. @Rakib 我遇到了错误:使用未定义的常量结果 - 假设为“结果”

标签: php html laravel laravel-5


【解决方案1】:

使用如下查询(我们也可以在select语句中使用DB::raw来计算sum orcount(*)`)

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

【讨论】:

    猜你喜欢
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 2023-02-17
    • 2023-02-22
    • 2018-03-16
    • 1970-01-01
    相关资源
    最近更新 更多