How to invoke controller methods in construct method in Laravel? - laravel

I'm trying to invoke multiple methods in __construct function of laravel controller so that all page partials can get their data before loading the whole page. Here is some code demonstration.
web.php
Route::get('/','HomeController#index');
HomeController.php
class HomeController extends controller
{
public function __construct()
{
$this->middleware("auth");
$this->featuredNews();
}
public function index()
{
return view('pages.home');
}
public function featuredNews()
{
$news = News::select('id', 'heading', 'body', 'category', 'image', 'created_at', 'featured')->where('featured', 1)->first();
return view('pages.home_partials.featured_news')->with('news', $news);
}
}
home.blade.php
#include("pages.home_partials.featured_news");
Here I'm expecting the the home.blade.php data along with the featured_news.blade.php partial's data. But this code throwing an error
ErrorException
Undefined variable: news (View: D:\portal\resources\views\pages\home_partials\featured_news.blade.php)
How can I add multiple partials data along with the blade data in Laravel ?
Laravel version: 7.30

You need to set the controller property for this instead of set the view into the methods so please find the below codes which help:
HomeController.php
class HomeController extends controller
{
private $news = [];
public function __construct()
{
$this->middleware("auth");
$this->featuredNews();
}
public function index()
{
return view('pages.home')->with('news', $this->news);
}
public function featuredNews()
{
$this->news = News::select('id', 'heading', 'body', 'category', 'image', 'created_at', 'featured')->where('featured', 1)->first();
}
}

Related

Override default store function of a controller in Laravel 7

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!

Laravel Resource Routing not showing anything when directly call method

I am stuck in resource routing
when I enter url netbilling.test/customer it goes to customer index file but when I enter url netbilling.test/customer/index nothing is returned. Also guide me if I have to route different method than in resource what is the method for that.
here is my web.php,
Route::get('/dashboard', function () {
return view('dashboard/index');
});
Route::resource('/customer','CustomerController');
here is my customer controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Customer;
use App\Package;
use Redirect,Response;
class CustomerController extends Controller
{
public function index()
{
$packages = Package::get();
$customers = Customer::orderBy('id', 'DESC')->get();
return view('customer/index', compact('customers','packages'));
}
public function create()
{
//
}
public function store(Request $request)
{
//
}
public function show($id)
{
//
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
}
}
Without custom route specification, this is how the index route maps to a Resource Controller, taken from Actions Handled By Resource Controller:
Verb
URI
Action
Route Name
GET
/photos
index
photos.index
So if you want URI /customer/index to work, then you need to specify this explicitly in your Controller:
use App\Http\Controllers\CustomerController;
Route::resource('customer', CustomerController::class);
Route::get('customer/index', [CustomerController::class, 'index'])->name(customer.index);

Laravel: How to use the same post form for multiple sections

I want to build something like facebook and I have a form where the user can post content but I want to use the same form on different sections, for example: groups, pages, profile.
I have PostController that is a resource that receives the post requests but I need a way to differentiate between sections in order to store the data with the correct section_type and section_id.
// Post Model Post.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = ['content', 'user_id'];
public function section()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
public function group()
{
return $this->belongsTo(Group::class);
}
public function page()
{
return $this->belongsTo(Page::class);
}
}
The User.php Model
class User extends Authenticatable
{
public function posts()
{
return $this->morphMany(Post::class, 'section');
}
}
and this is the PostController and store() function where I have only one situation at this moment where a post is stored in section_type App\User, but I need a way to store it in App\Group or App\Page also.
public function store(Request $request)
{
$this->validate(request(),
[
'content' => 'required|min:5',
]);
$user = User::find(Auth::id());
$user->posts()->create([
'content' => $request->content,
'user_id' => Auth::id()
]);
return redirect('/');
}
What do I need to do in PostController.php?
I thought maybe I could use a post request with parameters in the form, like this for posting in groups
<form method="post" action="/?group=1">
or this for posts in pages
<form method="post" action="/?page=1">
And after that use $request->query() to get the section and id. Do you have other ideas?

Laravel 5 - How to passing request data from route to controller

I am new in laravel,
How to passed request data into controller ? Just like happen on view ?
Route::get('/kelihatan', function (Request $request) {
return view( 'pages' , [ 'page' => 'index' , '_request' => $request->all() ] );
});
How to passing request data to controller before passed into view or model ? Just like this ??
Route::get( '/{page}', 'UserController#show' );
Mention the path in route first depending upon the type of request i.e. Get or Post
Route::get('/invite/{code}', 'MyController#get_my_action');
Route::post('/invite/{code}', 'MyController#post_my_action');
Then in controller named as MyController create a function like
public function get_my_action($code){
//your code goes here
}
or
public function post_my_action(Request $request, $code)
{
//your code goes here
}
With that route:
Route::get('/{page}', 'UserController#show');
In the controller you can do that:
public function show()
{
$page = request()->route('page');
or that:
public function show($page)
or that:
public function show(Request $request, $page)
Try this:
Routes.php
Route::get( '/{page}', 'UserController#show' );
UserController.php
public function show($page) {
dd($page);
}
Inside your controller define your show function as below:
function show(Request $request)
{
echo $request['page'];//or whatever data you want to pass
}
///add follwoing line before you define controller
use Illuminate\Http\Request;

Codeigniter get specific record in table

I practicing CI and loop through all records in a view and produces each link as below:
sitename.com/products/2
where 2 is id of certain product, I want to click on the link and get this product information in product page, how can I create a model to retrieve specific record?
Model:
class Db_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function getProduct($id) {
$query = $this->db->get_where('tbl_product', array('id' => $id));
return $query->result();
}
}
Controller:
class Products extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('db_model');
}
public function index()
{
$data['rows'] = $this->db_model->getProduct($id);
$this->load->view('product', $data);
}
}
Thanks for help.
Your index function doesn't take $id as a parameter, So you need to modify your index function, Use the following index function, which I've edited.
public function index($id)
{
$data['rows'] = $this->db_model->getProduct($id);
$this->load->view('product', $data);
}

Resources