I have an app where a user can buy courses.
So there's a Users table, Ecourses table and a Payment table which is a pivot table.
For now, an admin can delete a user even if he/she has paid and this gives an error
Trying to get property 'name' of non-object
on this line of the users blade file <td class="font-w600 font-size-sm"></td>
I'm looking for a simple way to disable the delete button if the user has paid for one or more course(s)
Since I don't really know what code I can show, I paste here what I've got.
Checkout Controller :
public function charge(Request $request)
{
\Stripe\Stripe::setApiKey('sk_test_hidden');
try {
$charge = \Stripe\Charge::create([
'amount' => Cart::total() * 100,
'currency' => 'EUR',
'description' => 'Paiement reçu sur la plateforme LMS - Cours Thalès',
'source' => $request->input('stripeToken'),
'receipt_email' => Auth::user()->email,
]);
foreach (Cart::content() as $item) {
Payment::create([
'ecourse_id' => $item->model->id,
'amount' => $item->model->price,
'email' => Auth::user()->email,
'name' => Auth::user()->name,
'user_id' => Auth::user()->id,
]);
EcourseUser::create([
'user_id' => Auth::user()->id,
'ecourse_id' => $item->model->id
]);
}
event(new UserHasPurchasedEvent(Auth::user()));
return redirect()->route('checkout.success')->with('success', 'Paiement validé.');
} catch (\Stripe\Exception\CardException $error) {
throw $error;
}
}
Payment Model :
public function ecourse()
{
return $this->belongsTo('App\Ecourse');
}
public function user()
{
return $this->belongsTo('App\User');
}
User Model :
public function ecourses()
{
return $this->hasMany('App\Ecourse');
}
public function payments()
{
return $this->hasMany('App\Payment');
}
public function paidEcourses()
{
return $this->belongsToMany('App\Ecourse');
}
Ecourse model :
public function user()
{
return $this->belongsTo('App\User');
}
public function inscrits()
{
return $this->belongsToMany('App\User');
}
EcourseUser model :
class EcourseUser extends Model
{ protected $table = 'ecourse_user';
protected $fillable = ['user_id', 'ecourse_id'];
public function ecourse()
{
return $this->belongsTo('App\Ecourse');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
One question aside : I see that when you post a question it takes less than 5 seconds for some people to click the unvote check mark.
Are those guys paid for that or what?
The reason why noobies like me are here si to learn form pros like you right?
Any help would be appreciated.
Thank you all,
Aucun commentaire:
Enregistrer un commentaire