Ich arbeite an einem Hobby-Projekt (Shop-Management) mit Laravel. Ich versuche DB :: beginTransaction() und DB :: rollback() zu verwenden, aber es funktioniert nicht. Nach meinem Code glaube ich, dass keine Einträge in der DB eingetragen sein sollten.Ich kann keine Transaktionen in Laravel verwenden
Ich habe bereits nach Möglichkeiten gegoogelt, konnte aber keine Lösung finden. Und meine MySQL-Tabellen sind InnoDB
Hier ist meine Shop-Controller-Datei.
class shopController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function getView()
{
if(Auth::user()->shop_status==0)
return View::make('shop')->with('name',json_encode(Auth::user()->Vendor_Detail()->get(['first_name', 'last_name'])));
else
return redirect('/home');
}
public function addShop(ShopDataRequest $request){
//get the logged in vendor's model instance. Auth uses Vendor model
$vendor = Auth::user();
//create new Shop Model
$shop = new Shop;
$shop->name = $request['shop_name'];
$shop->address = $request->addressLine1;
$shop->pincode = $request->pincode;
$shop->phone = $request->phone;
$shop->shop_type = $request->shop_type;
DB::beginTransaction();
try{
//save shop details
$vendor->Shops()->save($shop);
//throw custom Exception
throw new \Exception('User not created for account');
}
catch (Exception $e){
//catch exception and rollback
DB::rollback();
}
}
}
Modelle:
1) Shop:
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Shop extends Authenticatable
{
protected $connection = 'vendor';
protected $fillable = [
'shop_id','vendor_id','name','address','pincode','phone','shop_type'
];
public function Vendor(){
return $this->belongsTo('App\Models\Vendor','vendor_id','vendor_id');
}
public function Products(){
return $this->belongsToMany('App\Models\Product')->withPivot('mfg_date', 'exp_date','active','quantity');
}
}
2) Der Anbieter
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Vendor extends Authenticatable
{
protected $connection = 'vendor';
protected $primaryKey = 'vendor_id';
protected $fillable = [
'email','phone','password',
];
protected $hidden = [
'password', 'remember_token',
];
public function Vendor_Detail(){
return $this->hasOne('App\Models\Vendor_Detail','vendor_id','vendor_id');
}
public function Shops(){
return $this->hasMany('App\Models\Shop','vendor_id','vendor_id');
}
public function Documents(){
return $this->hasMany('App\Models\Vendor_document','vendor_id','vendor_id');
}
}
MySQL-Tabelle Details DB-Engine zeigt.
mysql> SHOW TABLE STATUS WHERE
Name
= 'shops'; + ------- + -------- + --------- + ------------ + ------ + - --------------- + ------------- + ----------------- + - ------------ + ----------- + ---------------- + -------- ------------- + ------------- + ------------ + --------- -------- + ---------- + ---------------- + --------- + | Name | Motor | Version | Row_format | Zeilen | Avg_row_length | Datenlänge | Max_data_length | Index_Länge | Daten_frei | Auto_increment | Create_time | Aktualisierungszeit | Check_time | Sortierung | Prüfsumme | Create_options | Kommentar | + ------- + -------- + --------- + ------------ + ------ + - --------------- + ------------- + ----------------- + - ------------ + ----------- + ---------------- + -------- ------------- + ------------- + ------------ + --------- -------- + ---------- + ---------------- + --------- + | Geschäfte | InnoDB | 10 | Kompakt | 1 | 16384 |
16384 | 0 | 16384 | 0 | 17 | 2016-07-03 04:56:27 | NULL | NULL | utf8_general_ci |
NULL | | | + ------- + -------- + --------- + ------------ + ------ + - --------------- + ------------- + ----------------- + - ------------ + ----------- + ---------------- + -------- ------------- + ------------- + ------------ + --------- -------- + ---------- + ---------------- + --------- + 1 Reihe einstellen (0.00 Sek.)mysql> SHOW TABLE STATUS WHERE
Name
= 'Verkäufer'; + --------- + -------- + --------- + ------------ + ------ + ---------------- + ------------- + ----------------- + -------------- + ----------- + ---------------- + ------ --------------- + ------------- + ------------ + ------- ---------- + ---------- + ---------------- + --------- + | Name | Motor | Version | Row_format | Zeilen | Avg_row_length | Datenlänge | Max_data_length | Index_Länge | Daten_frei | Auto_increment | Create_time | Aktualisierungszeit | Check_time | Sortierung | Prüfsumme | Create_options | Kommentar | + --------- + -------- + --------- + ------------ + ------ + ---------------- + ------------- + ----------------- + -------------- + ----------- + ---------------- + ------ --------------- + ------------- + ------------ + ------- ---------- + ---------- + ---------------- + --------- + | Anbieter | InnoDB | 10 | Kompakt | 1 | 16384 |
16384 | 0 | 0 | 0 | 6 | 2016-07-07 00:46:08 | NULL | NULL | utf8_general_ci |
NULL | | | + --------- + -------- + --------- + ------------ + ------ + ---------------- + ------------- + ----------------- + -------------- + ----------- + ---------------- + ------ --------------- + ------------- + ------------ + ------- ---------- + ---------- + ---------------- + --------- + 1 Reihe im Satz (0.00 Sek.)
Bitte helfen Sie.
Muss ich Commit auch nach dem Rollback() verwenden? – jaysingkar
Ich versuchte mit commit(). Immer noch das gleiche :-( – jaysingkar
Sie müssen nach dem Speichern verwenden. Vor dem Wurf Ausnahme – LONGMAN