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);
Related
Laravel version is 7.0:
I have setup model relationships like this.
<?php
namespace App;
class Template extends Model
{
protected $fillable = ['header_id', 'content', 'name'];
public function header()
{
return $this->belongsTo('App\Header', 'header_id');
}
}
In controller I can get template object with header.
<?php
namespace App\Http\Controllers;
use App\Template;
class TemplateController extends Controller
{
public function show($id)
{
$template = Template::find($id);
}
}
Now I can use $template->header in view.
How can I pass different header_id and get header relationship object?
I would like to do as following:
<?php
namespace App\Http\Controllers;
use App\Template;
class TemplateController extends Controller
{
public function show($id, $temp_header_id)
{
$template = Template::find($id);
$template->header_id = $temp_header_id;
}
}
I want to get new header relationship in view:
Is there any way to return new header relationship when I do $template->header in view.
Thank you
Yes you can do what you are looking to do, but kinda defeats the relationship in the database. You can assign any id to $template->header_id and then load the relationship using that new value:
$template->header_id = 897;
// load the relationship, will use the new value
// just in case the relationship was already loaded we make sure
// to load it again, since we have a different value for the key
$template->load('header');
$template->header; // should be header with id = 897
Here is the Model which simply inserts data into the table:
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserHandle extends Model {
public $timestamps=false;
}
Here is the Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\UserHandle;
class UserHandleCntrl extends Controller {
public function index(){
$data = new UserHandle;
$data->name='Laravel';
$data->email='dbtcbd#gmail.com';
$data->username='bdlaravel';
$data->password='laravel12345';
$data->save();
echo 'Data Inserted';
}
}
Here is the route :
Route::get('/user', 'UserHandleCntrl#index');
But when I am trying to insert data into the database I am getting error that model class not found. Why I am getting this and what is the solution. Please help. Bellow is the error screenshot.
Keep the a of App capital while you are including that model.
use App\UserHandle;
including model use App instead of app in your controller file.
use App\UserHanle;
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class testing extends Model
{
}
should i write my all queries in this model class,even if i just want to use query builder no eloquent?
what are the good practices to write raw query or using query builder?
You can write your queries or say eloquent in your model differenciating from other logical code
Say for example you have AlbumController
namespace App\Http\Controllers;
use App\Album;
use App\Http\Controllers\Controller;
class AlbumController extends Controller
{
public function index()
{
$albums = Album::get_albums();
// other logical code
}
}
And in Album.php which is model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Album extends Model
{
public static function get_albums() {
$albums = Album::get();
return $albums;
}
}
And in your route file
Route::post('album', 'AlbumController#index');
Hope you get idea!
No that is not only the method to communicate with your database it is one possible solution only. You can use DB for query like
<?php
Namespace App\Http\Controllers;
use DB;
Class AbcController extends Controller{
Public function functionName(){
$data=DB::table(‘tableName’)->get();
return view(‘desiredPage’)->with(‘data’, $data);
}
}
go to the link for more laravel database query information
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
I am new to Laravel, I am trying things around while going through a tutorial. This is where I am facing an unexpected behaviour.
I have a model tweet and a controller named tweetsController; when I call tweet::find() or any similar method I found this:
FatalErrorException in tweetsController.php line 13:
Class 'App\Http\Controllers\tweet' not found
I have also tried App\tweet::find().
Everything seems fine through tinker.
Please explain.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class tweetsController extends Controller
{
public function show(){
$data = tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
public function delete($id){
return "here we dele the tweet ".$id;
}
public function add(){
return "i add your tweet to database then show you all the tweets";
}
}
tweet.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tweet extends Model
{
protected $fillable = array(
'tweetHead',
'tweetBody'
);
}
?>
A few options the maybe generating this error:
The model/controller namespace is incorrect;
The name of the file and the class name for the model needs to be "Tweet" with the first letter in uppercase;
If you set the right namespace on the model "Tweet.php" and import that on your "TweetController.php"
I hope that helps :)
UPDATE:
In the TweetController.php add this
use App\Tweet;
Before the class declaration, like this
use App\Tweet;
class tweetsController extends Controller
{
And remember to change the controller name in the class declaration like this
class TweetsController extends Controller
{
And the controller filename will become "TweetsController.php"
The Model also has to be named "Tweet" and not "tweet" in the class declaration and the filename
class tweet extends Model
will become
class Tweet extends Model
and the file will be named "Tweet.php"
and everytime you need to call the model you will do this
public function show(){
$data = App\Tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
Add use Add\User below namespace in your controller