Reputation: 17301
I have simple model on laravel
and I would like to use PHP Object Oriented to save or update data inside the Controller
. All methods of my solution work fine but I can't redirect.
class RechargeRangeCard extends Model
{
protected $table = "recharge_range_cards";
protected $guarded = ['id'];
private $request;
public function data(StoreRechargeRangeCard $request)
{
$this->request = $request;
return $this;
}
public function store()
{
$data = RechargeRangeCard::create(
[
'minimum' => $this->request->input('minimum_range'),
'maximum' => $this->request->input('maximum_range'),
'user_id' => Auth::user()->id,
]
);
return $this;
}
public function redirect()
{
return redirect()->to('rechargeRangecards');
}
}
public function store(StoreRechargeRangeCard $request)
{
$class = new RechargeRangeCard();
$class->data($request)->store()->redirect();
}
In this chaining method redirect()
doesn't work.
Upvotes: 0
Views: 184
Reputation: 62238
You have to return the redirect from the Controller method in order for it to work:
public function store(StoreRechargeRangeCard $request)
{
$class = new RechargeRangeCard();
return $class->data($request)->store()->redirect();
}
However, I'd just like to point out that this is mixing your concerns. Redirecting is strictly a concern of the controller; your model shouldn't have any clue about that, or even that it's possible.
Upvotes: 1