Override default store function of a controller in Laravel 7 - laravel

I am working on a Laravel application that has some modules implemented.
Now, the one of the modules extends a default controller and should be able to override the default store() function
I tried the following:
namespace App\Http\Controllers\Admin;
class OriginalController extends AdminBaseController
{
public function store(Request $request)
{
$model = new Model();
$model->name = $request->name;
$model->save();
}
}
In the module:
namespace Modules\CustomModule\Http\Controllers\Admin;
class ExtendedController extends OriginalController
{
public function store(Request $request)
{
$model = new Model();
$model->name = $request->name;
$model->newInfo = $request->newInfo;
$model->save();
}
}
Even if I set the web.php routes for the new controller, the store() function will only look at the original one
Could someone tell me what am I missing?
Thank you!

Related

How to update pivot table records using Eloquent in Laravel

Here is the source code of model and controller.
My Role.php
class Role extends Model
{
public function permissions()
{
return $this->belongsToMany('App\Models\Permission');
}
}
My Permission.php
class Permission extends Model
{
public function roles()
{
return $this->belongsToMany('App\Models\Role');
}
}
My Controller
class RoleController extends Controller
{
public function update(Request $request, $id) {
$role = Role::findOrfail($id);
$roleid = $role->id;
$permissions = $request->permissions;
foreach ($role as $roles) {
$roles->permissions()->updateExistingPivot($roleid, $permissions);
}
}
}
i will appreciate your help.Thanks
Assuming you want to 'sync' the permissions for the role, make this role only have the permissions that have been passed via the Request input:
public function update(Request $request, $id)
{
// validate the permissions array
...
$role = Role::findOrFail($id);
$role->permissions()->sync($request->input('permissions', []));
...
}
Laravel 6.x Docs - Eloquent - Relationships - Updating Many to Many Relationship - Syncing Associations sync
you can use laravel builtin functions
Add = attach
Delete = detach
Update = sync
more info
https://laravel.com/docs/5.8/eloquent-relationships#updating-many-to-many-relationships
Pass Array data while update data
class RoleController extends Controller
{
public function update(Request $request, $id) {
$input = $request->all();
$role = Role::findOrfail($id);
$roleid = $role->id;
$permissions = $input['permissions'];
foreach ($role as $roles) {
$roles->permissions()->updateExistingPivot($roleid, $permissions);
}
}
}
Create a controller with resource and assign the controller to web.php
Your form's action should be as follows and use
action="{{route('GustPosts.update', $DataEdit->id)}}"
#csrf
#method('put')
Form as follows
<form action="{{route('GustPosts.update', $DataEdit->id)}}" method="post">
#csrf
#method('put')
</form>
On Your controllers
Edit Function
public function edit($id)
{
$data = Post::find($id);
return view ('gustUpdateForm',['DataEdit'=> $data]);
}
Update Function
public function update(Request $request, $id)
{
$objProductsUpdate = Post::find($id);
$objProductsUpdate -> ProductName = $request -> get('PNameTxt');
$objProductsUpdate -> ProductModel = $request -> get('PModelTxt');
$objProductsUpdate -> ProductPrice = $request -> get('PPriceTxt');
$objProductsUpdate -> OriginalFileName = $file->getClientOriginalName();
$objProductsUpdate -> FileName = $file->getFilename().'.'.$extension;
$objProductsUpdate ->save();
return redirect()->route('products.index');
}

Passing values from one controller to another

Been at this for ages, can't find a solution. I've searched and none of the posts help, possibly me not getting it as I'm new to Laravel (5.4)
I want to be able to be able to access $site_settings from one controller to another. Also $site_settings need to be accessible on all controllers and views. I also need to get variable from URL (hence Request in __construct( Request $request ) ) but it doesn't work either.
Would really appreciate any assistance.
Thanks.
See code below:
//BaseController
class BaseController extends Controller {
public function __construct( Request $request )
{
$slug = $request->slug;
$site_settings = \DB::table('sites_settings')->where('slug', $slug)->first();
View::share( 'site_settings', ['slug' => $slug, 'color' => $color] );
}
}
class SettingsController extends BaseController
{
// SettingsController
public function index( )
{
//how do I access $site_settings here and pass it on to the view?
// return view('settings.index');
}
}
---- UPDATE with Session ----
//BaseController
class BaseController extends Controller {
public function __construct()
{
$slug = Route::input('slug');
if(Session::get('slug') == $slug)
{
dd(Session::get('slug'));
}
else
{
$site_settings = \DB::table('sites_settings')->where('slug', $slug)->first();
Session::put('slug', $site_settings->slug);
}
}
}

FatalErrorException Laravel 5.3 Post Controllers

hi i have task title social network application using post and timeline
i have some problem
use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function postCreatePost(Request $request)
{
$post = new Post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->('home');
}
}
this is my Post modle please check this code
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user (){
return $this->belongsTo('App\User');
}
}
Try to change
$post = new Post();
To
$post = new Post;
1) Change:
$post->body = $request['body'];
to:
$post->body = $request->get('body');
2) This line: $request->user()->posts()->save($post); also seems all wrong.
In your User model you need to tell eloquent that a user has many posts
public function posts()
{
return $this->hasMany('App\Post');
}
Then that line in your controller for a user with id = 1; $user = User::find(1) becomes:
$user->posts()->save($post)
3)return redirect()->('home'); has to be return redirect()->route('home');
4) Lastly, take some time to read the laravel documentation

I have an error in laravel Class 'App\post' not found

When calling the store method,following error displays:
Class 'App\post' not found
My post controller:
use App\post;
class postController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$post=new post;
$post->title =$request->title;
$post->body =$request->body;
$post->save();
dd("Hi");
return Redirect::to('/')->with('success','You have been successfully subscribe to us.');
}
}
First you should change
$post=new post;
to
$post = new post();
Since you are trying to create a new class.
Seeing you are using laravel, this can also be done like such:
$post = Post::firstOrCreate(['title' => $request->title, 'body' => $request->body])
If that does not change your error,
Check in your App\post class if the namespace and class name are correct.

Passing a variable to a master template in laravel

In laravel i have defined a route like this
Route::get('/', array(){
'as' => 'index',
'uses' => 'HomeController#index'
});
The function index() in the HomeController contains
public function index(){
$index = new ExampleModel;
$data = $index->getExampleList();
return View::make('public.index');
}
Now the problem is i have a master layout called happypath inside layouts folder in my views which yields this public.index content and i need to pass this $data to layouts.happypath. How do i do this ?
You can use a view composer for example:
namespace App\Providers;
use App\ExampleModel;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
protected $exampleModel;
public function __construct(ExampleModel $exampleModel)
{
$this->exampleModel = $exampleModel;
}
public function boot()
{
view()->composer('layouts.happypath', function ($view) {
$view->with('publicIndex', $this->exampleModel->getExampleList());
});
}
public function register()
{
//
}
}
So, every time you use/render the layouts.happypath the $publicIndex variable will be attached within the layout. Also you need to add the ComposerServiceProvider class in your config/app.php file in the providers array. You may access/reference the data using $publicIndex variable in your layout. There are other ways like global shared $information using view()->share(...) method to share a peace of data all over the views but this may help you.
I could not figure out the ComposerServiceProvider View::composer thing. So i basically solved it like this in Laravel 4.2. Added this code to the BaseController.php
protected $menuList;
public function __construct() {
$response = API::pool([
['GET', API::url('level')],
]);
$index = new Index();
$index->setCourseList($response[0]->json()['Category']);
$result = $index->getCourseList();
View::share('result', $result); //This line shares the $result globally across all the views in laravel 4.2
}
This can be done with a Service Provider. You can either use an existing one or create a new one.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\ExampleModel;
class ViewServiceProvider extends ServiceProvider
{
public function boot()
{
$index = new ExampleModel;
$data = $index->getExampleList();
view()->share('public.index', $data);
}
public function register()
{
}
}
Source: EasyLaravel.com

Resources