I'm trying to get all the addresses of a User who is logged in for that i have created a relation in model address and User. Any Customer/User who is logged in can save multiple addresses, and make one address primary that part is working but after saving i want to get all the addresses of that particular customer so i created relation. I was trying to get all the address using
$userAddress=User::addresses();
but i'm getting
Non-static method App\User::addresses() should not be called statically addresses
This is my User Model :
<?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);
}
}
Address 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);
}
}
Product Controller:
<?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');
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire