Saya mencoba mendapatkan semua alamat Pengguna yang masuk untuk itu saya telah membuat hubungan dalam alamat model dan Pengguna. Setiap Pelanggan/Pengguna yang masuk dapat menyimpan banyak alamat, dan menjadikan satu alamat utama bagian itu berfungsi tetapi setelah menyimpan saya ingin mendapatkan semua alamat pelanggan tertentu itu, jadi saya membuat hubungan. Saya mencoba mendapatkan semua alamat menggunakan

$userAddress=User::addresses();

Tapi aku mengerti

Metode non-statis App\User::addresses() tidak boleh disebut alamat statis

Ini adalah Model Pengguna saya:

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name', 'last_name', 'email', 'password','address_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
    * @return string
    */
    public function getFullNameAttribute()
    {
    return $this->first_name. ' '. $this->last_name;
    }

    public function setPasswordAttribute($pass){

        $this->attributes['password'] = Hash::make($pass);

    }

    public function wishlist(){
        return $this->hasMany(Wishlist::class);
    }

    public function addresses(){
        return $this->hasMany(Addresses::class);
    }
}

Alamat Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Addresses extends Model
{
     /**
     * @var string
     */
    protected $table = 'addresses';

     /**
     * @var array
     */
    protected $fillable = [
        'name', 'address', 'country', 'state', 'city','address_type','user_id','updated_at'
    ];

    public function user(){
        return $this->belongsTo(User::class);
     }
}

Pengontrol Produk:

<?php

namespace App\Http\Controllers\Site;
use App\Contracts\AttributeContract;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Contracts\ProductContract;
use App\Contracts\AddressContract;
use Cart;
use Validator;
use App\User;

class ProductController extends Controller
{ 
    protected $productRepository;
    protected $attributeRepository;
    protected $addressRepository;
    public function __construct(ProductContract $productRepository, AttributeContract $attributeRepository, AddressContract $addressRepository)
{  
    $this->productRepository = $productRepository;
    $this->attributeRepository = $attributeRepository;
    $this->addressRepository = $addressRepository;
}

    public function index()
    {
        $products = $this->productRepository->listFeaturedProduct();
        return view('site.pages.homepage', compact('products'));
    }

public function checkout(Request $request)
{
    $userAddress=User::addresses();
    return view('site.pages.checkout');
}

public function addUserAddress(Request $request)
{
    $customer_name=$request->customer_name;
    $customer_address=$request->customer_address; 
    $country=$request->country; 
    $city=$request->city;
    $zip_code=$request->zip_code;  
    $state=$request->state; 
    $address_type=$request->address_type; 
    $is_primary_address=$request->primary_address; 
    $userID=auth()->user()->id;
    $data=array('name'=>$customer_name,'address'=>$customer_address,'country'=>$country,'state'=>$state,'city'=>$city,'address_type'=>$address_type,'user_id'=>$userID,'is_primary_address'=>$is_primary_address);
    $userAddress  = $this->addressRepository->addAddress($data);
    return redirect()->back()->with('message', 'Address Added');
}

}
0
user3653474 8 Maret 2020, 11:23

1 menjawab

Jawaban Terbaik

Ruang nama pengguna Anda adalah App\User dan ruang nama alamat Anda adalah App\Models\Addresses.

Anda perlu mengedit dua model Anda.

Model pengguna

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;

use App\Models\Addresses; // You are missing this line !!!!

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name', 'last_name', 'email', 'password','address_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
    * @return string
    */
    public function getFullNameAttribute()
    {
    return $this->first_name. ' '. $this->last_name;
    }

    public function setPasswordAttribute($pass){

        $this->attributes['password'] = Hash::make($pass);

    }

    public function wishlist(){
        return $this->hasMany(Wishlist::class);
    }

    public function addresses(){
        return $this->hasMany(Addresses::class);
    }
}

Model Alamat

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\User; // and this line here

class Addresses extends Model
{
     /**
     * @var string
     */
    protected $table = 'addresses';

     /**
     * @var array
     */
    protected $fillable = [
        'name', 'address', 'country', 'state', 'city','address_type','user_id','updated_at'
    ];

    public function user(){
        return $this->belongsTo(User::class);
     }
}

Sekarang Anda dapat menggunakan relasi untuk mendapatkan satu alamat pengguna

$id = 1;
$adresses = User::find($id)->addresses;
\\ or
$adresses = auth()->user()->addresses;

Saya punya dua komentar lain:

  • Model tidak boleh jamak seperti yang Anda lakukan dengan Addresses
  • Anda salah ketik, dalam bahasa Inggris hanya ada satu D di alamat tidak seperti dalam bahasa Prancis.
  • Anda seharusnya memperhatikan, kami tidak menggunakan metode secara langsung, kami menggunakannya sebagai atribut (->addresses alih-alih ->addresses()) jika Anda menggunakan metode tersebut, Anda menggunakan pembuat kueri untuk itu.
$user->addresses()->get();
\\or
$user->addresses()->latest()->first();
0
N69S 8 Maret 2020, 08:45