2016-11-12 82 views
0

我想根據用戶標識刪除所有關聯。我有這樣根據laravel中的用戶標識刪除所有關聯5.2

enter code here 
Array 
(
[id] => 1 
[title] => Mr 
[firstname] => kunal 
[lastname] => mahajan 
[business_email] => [email protected] 
[username] => kunal 
[website] => 
[email] => [email protected] 
[address] => 
[image] => 
[password] => $2y$10$nk1O1jD38dLN4xlBpkj1feJYIj9nH17.v3jMTO1jdTn9V7Mv0SUBW 
[decrypt_password] => 123456 
[phonenumber] => 01610524545 
[mobile] => 7837329321 
[remember_token] => 
[email_verify] => 0 
[status] => 0 
[activation_key] => 
[created_at] => 2016-11-12 06:25:48 
[updated_at] => 2016-11-12 06:25:48 
[listings] => Array 
    (
     [id] => 1 
     [user_id] => 1 
     [property] => 
     [type] => premimum 
     [service_name] => Courses 
     [business_name] => kunal 
     [about] => ore ipsum is dummy text. 
     [address] => 
     [instagram] => www.instagram.com 
     [facebook] => www.facebook.com 
     [pinterest] => www.pinterest.com 
     [twitter] => www.twitter.com 
     [youtube] => www.youtube.com 
     [certifications] => Lorem ipsum is dummy text 
     [days] => 
     [from] => 
     [to] => 
     [location] => Ludhiana, Punjab, India 
     [postcode] => 
     [borough] => 
     [town] => 
     [latitude] => 30.9058885 
     [longitude] => 75.8359645 
     [squarefeet] => 
     [pricerange] => 
     [turnover] => 
     [netprofit] => 
     [information] => Lorem ipsum is dmmy text 
     [other] => 
     [facilities] => 
     [premises] => 
     [expansion] => 
     [reason] => 
     [competition] => 
     [established] => 
     [support] => 
     [employees] => 0 
     [furniture] => 
     [inventory] => 
     [created_at] => 2016-11-12 06:25:48 
     [updated_at] => 2016-11-12 06:25:48 
     [listingimage] => Array 
      (
       [0] => Array 
        (
         [id] => 1 
         [list_id] => 1 
         [image] => 602758083.jpg 
        ) 

       [1] => Array 
        (
         [id] => 2 
         [list_id] => 1 
         [image] => 631874072.jpg 
        ) 

       [2] => Array 
        (
         [id] => 3 
         [list_id] => 1 
         [image] => 572143726.jpg 
        ) 

       [3] => Array 
        (
         [id] => 4 
         [list_id] => 1 
         [image] => 845492008.jpg 
        ) 

       [4] => Array 
        (
         [id] => 5 
         [list_id] => 1 
         [image] => 22020276.jpeg 
        ) 

       [5] => Array 
        (
         [id] => 6 
         [list_id] => 1 
         [image] => 337797369.jpg 
        ) 

      ) 

     [courses] => Array 
      (
       [0] => Array 
        (
         [id] => 1 
         [list_id] => 1 
         [user_id] => 1 
         [course] => Nails Course 
         [fees] => 150 
         [weblink] => www.nails.com 
         [details] => Lorem ipsum is dummy text 
         [coursetype] => Part Time 
         [duration] => 6months 
         [requirement] => 15 Students 
         [certificate] => Lorem ipsum is dummy text.Lorem ipsum is dummy text.Lorem ipsum is dummy text. 
         [created_at] => 2016-11-12 06:25:48 
         [updated_at] => 2016-11-12 06:25:48 
        ) 

       [1] => Array 
        (
         [id] => 2 
         [list_id] => 1 
         [user_id] => 1 
         [course] => Skin Course 
         [fees] => 1500 
         [weblink] => www.skin.com 
         [details] => Lorem ipsum is dummy text 
         [coursetype] => Full Time 
         [duration] => 2 months 
         [requirement] => 10 students 
         [certificate] => Lorem ip[sum is dummy text 
         [created_at] => 2016-11-12 06:25:48 
         [updated_at] => 2016-11-12 06:25:48 
        ) 

      ) 

     [fitness_services] => Array 
      (
      ) 

    ) 
) 

我運行此查詢的數據,但我給我的成員函數列表錯誤消息()調用 我有代碼: -

enter code here 
$alldata = SessionUser::with('listings')->findOrFail($userid); 
$alldata = json_decode(json_encode($alldata),true); 
echo "<pre>"; print_r($alldata); die; // This query fetch all data 
$deletedata = SessionUser::find($userid); 
$deletedata =json_decode(json_encode($deleteSessiondata),true); 
$deletedata->listings()->delete();// This query doesn't work 

誰能幫我出這一點。感謝提前:)

回答

1

您可以使用Laravel's event handlers的 - deleting事件解決你的問題:

class User extends Eloquent 
{ 
    public function listings() 
    { 
     return $this->hasMany('App\Listing'); 
    } 

    // this is a recommended way to declare event handlers 
    protected static function boot() { 
     parent::boot(); 

     static::deleting(function($user) { // before delete() method call this 
      $user->listings()->delete(); 
      // do the rest of the cleanup... 
     }); 
    } 
} 

class Listing extends Eloquent 
{ 
    public function photo() 
    { 
     return $this->hasOne('App\Photo'); 
    } 

    // this is a recommended way to declare event handlers 
    protected static function boot() { 
     parent::boot(); 

     static::deleting(function($listing) { // before delete() method call this 
      $listing->photo()->delete(); 
      // do the rest of the cleanup... 
     }); 
    } 
} 

雄辯車型火的幾個事件,讓你掛接到各種 點模型的使用以下方法的生命週期:creatingcreatedupdatingupdatedsavingsaveddeletingdeletedrestoringrestored。事件允許您每次輕鬆執行代碼 在數據庫中保存或更新特定的模型類。

希望這會有所幫助!

+0

感謝您的回覆我想刪除這個數據在我有SessionUserModel的特定功能。當用戶完成註冊所有從會話表中刪除的數據並將其傳輸到實際表格時,所有數據都存儲在臨時表中。希望你瞭解我的概念 – kunal