Gediminas Šukys
Gediminas Šukys

Reputation: 7391

Access model method in controller Laravel

I'm trying to access model method in Ad.php model from the controller in Laravel. I'm trying in the same way as I did in other frameworks, so maybe I'm doing wrong this in Laravel.

My need is to perform some actions before creating or updating the record. These actions are same for both actions, so I want to put logic in model method and call it in controller.

After doing this I just got blank white window with no errors. Why?

My code in Controller:

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdsController extends Controller {
    public function update(Request $request, $id) {
       $input = Request::all();
       \App\Ad::saveMultiFields($input);
    }
}

Model:

<?php

namespace App;

use Barryvdh\Debugbar\Facade as Debugbar;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Ad extends Eloquent {

    public static function saveMultiFields($input) {
        Debugbar::info($input);
        return true;
    }
}

Upvotes: 1

Views: 1471

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

You need to return view:

public function update(Request $request, $id) {
    $input = Request::all();
    \App\Ad::saveMultiFields($input);
    return view('ad.updated');
}

Or redirect:

public function update(Request $request, $id) {
    $input = Request::all();
    \App\Ad::saveMultiFields($input);
    return redirect()->back()->with('message', 'Ad was updated');
}

Upvotes: 1

Related Questions