I have controller class in laravel in which i have a function create() and a variable attachment i am calling function by ajax
my class code is.
class AttachmentController extends Controller
{
public $_attachments;
public function create()
{
$this->_attachments[]= 'test';
var_dump($this->_attachments);
}
problem is every time when i call it by ajax it return me "test" at 0 index of attachment array . but i want if i call create function 1st time it give me test on 0 index but when next time when i call it . it give me "test" on both 0 and 1 index and so on ..
how it is possible please help me
To preserve the values between requests you need to store them somewhere, an alternative is to use sessions, as the example below, for more information see https://laravel.com/docs/session
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AttachmentController extends Controller
{
public function create(Request $request)
{
$request->session()->push('attachments', 'test');
var_dump($request->session()->get('attachments'));
}
}
Try something like this.
$_attachments[] = Session::get('test');
$_attachments[] = 'test';
Session::get('test', $_attachments);
dd(Session::get('test'));
let me know if this is what you want.
Since HTTP driven applications are stateless, sessions provide a way
to store information about the user across requests.
class AttachmentController extends Controller
{
public function create()
{
$attachments = Session::get('attachments', array());
$attachments[] = 'test';
Session::put($attachments);
var_dump($this->_attachments);
}
}
Further reading: Laravel Session
Related
I tried to follow this tutorial to make a work on Laravel : https://vegibit.com/how-to-set-up-form-submission-in-laravel/ but I got a strange bug and I can't found a way to fix it, I wanted to make a form to insert something in my database and show it in another page (the showing is right, it's just the form page that get this problem) here is a screenshot :
Code :
namespace Doreas\Http\Controllers;
use Illuminate\Http\Request;
use App\Demand;
class DemandeController extends Controller
{
public function index()
{
$demande = Demand::all();
return view('demande.index', ['demande' => $demande]);
}
public function show($id)
{
$demande = Demande::find($id);
return view('demande.show', ['demande' => $demande]);
}
public function create()
{
return 'it works';
}
}
I don't understand where the problem of Demand come from
<?php
namespace App\Doreas;
use Illuminate\Database\Eloquent\Model;
class Demand extends Model
{
public function scopeTest($query)
{
return $query->where('title', '=', 'test');
}
}
Where I declare the class
The problem is that you have imported the class Demand using use App\Demand;
However its namespace is actually App\Doreas so you should do use App\Doreas\Demand;
I have a base controller that gets certain default properties based on the domain name for every request. These include app_id and language_id.
What would be the recommended way to get these values set in my models, so that for example if I tried to create a new Post model it would have the app_id and language_id set by the values defined in my base_controller.
Kind regards to any response.
You can use sessions to store your app_id and language_id, and get it from session whenever you need it.
Check How to use sessions in Laravel from here
You can use __consruct method as suggested in comment.
Here is another alternative:-
YourController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\YourModel;
class YourController extends Controller
{
public function callModelStaticMethod()
{
$data = ['appId'=>4, 'languageId'=>5];
YourModel::passDataToModelMethod($data);
}
}
YourModel.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class YourModel extends Model
{
public static function passDataToModelMethod($data)
{
var_dump($data);
// Write your business logic here.
// $newObj = new self;
// $newObj->app_id = $data['app_id'];
// $newObj->language_id = $data['languageId'];
// $newObj->save();
}
}
#ravi...just bit correction to your code. you can't call directly your method from model.
YourModel::passDataToModelMethod($data);
above won't work, you need to do as follows
$insertData=new YourModel();
$insertData->passDataToModelMethod($data);
I want a variable to be shared by other controller methods. This variable can be updated by one controller method and the change should be reflected in other methods? any suggestions ? what is the best practice to do that ? this is my code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Session;
class test extends Controller
{
public $global;
public function __construct()
public function a(Request $request){
$this->global="some value"
}
public function b(Request $request){
echo $this->global;
//it always return a null
}
}
Set the variable inside your constructor.
function _construct() { $this->global = "some value";}
So, you don't only want a global variable, you also want that this variable should be changed by other routes as well. The one way to achieve this is using session.
function a() {
session()->put('global_variable', 'set by method a');
//your other logic
}
and from method b...
function b() {
//get the variable set by method a here
dd(session()->get('global_variable'));
}
You can create a new file in config and use
config('your_new_file_name.key')
Check this : https://laracasts.com/discuss/channels/general-discussion/laravel-5-global-variables
I have been facing a problem of not able to use the model inside the controller in the new laravel framework version 5. i created the model using the artisan command
"php artisan make:model Authentication" and it created the model successfully inside the app folder, after that i have created a small function test in it, and my model code looks like this.
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
echo "This is a test function";
}
}
Now i have no idea, that how shall i call the function test() of model to my controller , Any help would be appreciated, Thanks in advance.
A quick and dirty way to run that function and see the output would be to edit app\Http\routes.php and add:
use App\Authentication;
Route::get('authentication/test', function(){
$auth = new Authentication();
return $auth->test();
});
Then visit your site and go to this path: /authentication/test
The first argument to Route::get() sets the path and the second argument says what to do when that path is called.
If you wanted to take this further, I would recommend creating a controller and replacing that anonymous function with a reference to a method on the controller. In this case, you would change app\Http\Routes.php by instead adding:
Route::get('authentication/test', 'AuthenticationController#test');
And then use artisan to make a controller called AuthenticationController or create app\Http\Controllers\AuthenticationController.php and edit it like so:
<?php namespace App\Http\Controllers;
use App\Authentication;
class AuthenticationController extends Controller {
public function test()
{
$auth = new Authentication();
return $auth->test();
}
}
Again, you can see the results by going to /authentication/test on your Laravel site.
Use scope before method name
<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Mainmenu extends Model
{
public function scopeLeftmenu() {
return DB::table('mainmenus')->where(['menu_type'=>'leftmenu', menu_publish'=>1])->orderBy('menu_sort', 'ASC')->get();
}
}
above code i tried to access certain purpose to call databse of left menu
than we can easy call it in Controller
<?php
Mainmenu::Leftmenu();
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function scopeTest(){
echo "This is a test function";
}
}
Just prefix test() with scope. This will become scopeTest().
Now you can call it from anywhere like Authentication::Test().
For me the fix was to set the function as static:
public static function test() {..}
And then call it in the controller directly:
Authentication::test()
You can call your model function in controller like
$value = Authentication::test();
var_dump($value);
simply you can make it static
public static function test(){
....
}
then you can call it like that
Authentication::test();
1) First, make sure your Model is inside a Models Folder
2) Then supposing you have a model called Property inside which you have a method called returnCountries.
public function returnCountries(){
$countries = Property::returnCountries();
}
of course, in your case, replace Property by the name of your Model, and returnCountries by the name if your function, which is Test
and in the Model you write that function requesting the countries
so in your Model, place a:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return $test = "This is a test function";
}
}
and this is what your Controller will be getting
You should create an object of the model in your controller function then you can model functions inside your controller as:
In Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return "This is a test function"; // you should return response of model function not echo on function calling.
}
}
In Controller:
namespace App\Http\Controllers;
class TestController extends Controller
{
// this variable is used to store authenticationModel object
protected $authenticationModel;
public function __construct(Request $request)
{
parent::__construct($request);
$this->authenticationModel= new \App\Authentication();
}
public function demo(){
echo $this->authenticationModel->test();
}
}
Output:
This is a test function
I use subfolder in the Controller 'folder',which works fine..
but when I write the blow code ..php return the error said "Auth is not found ,and the Input'
<?php
namespace website;
use Auth;
use Input;
use View;
use Illuminate\Routing\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return View::make('wcsite.index');
}
public function saveHome()
{
$uid = Auth::user()->id;
$websiteData = Input::get('data');
return $uid;
}
}
but when I add 'use Auth,use Input',everything works fine...so ,anyone who can tell me ...is there any way to to this ,which "need not to use Auth,use Input in my subfolder Controllers' Thank you a lot!
and my route is
Route::post('/wcsite',array('uses' => 'website\HomeController#saveHome'))->before('auth');
Your question is a bit confusing. You're saying that the code above is not working because PHP can't find the Auth and Input global class references but your code clearly shows you're importing them correctly.
PHP can't use the global Auth and Input class references without importing them first (which you're doing in the above code). It's going to assume they're located under the website namespace by default.
If you don't want to import hem with use statements you could always reference the global namespace by using a backslash before the class name like the code below:
<?php
namespace website;
use Illuminate\Routing\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return \View::make('wcsite.index');
}
public function saveHome()
{
$uid = \Auth::user()->id;
$websiteData = \Input::get('data');
return $uid;
}
}
That being said, I prefer importing the classes first instead of using backslashes everywhere. It'll provide for much cleaner code.