Laravel: Passing data to default.blade.php from base controller - laravel

I have a base controller with a method to return a twitter feed to my view.
I want to move this in the view from the page view to the default blade to reduce redundancy as it will be appearing site wide. How do I pass data from the base controller to blade?
I can send it to my view from the page controller like so:
public function get_index()
{
..................
$this->layout->nest('content', 'home.index', array(
'tweets' => $this->get_tweet()
));
}
and in the view, output it like this:
if ($tweets)
{
foreach ($tweets as $tweet)
{
..............
I want to do all this from within default.blade.php and my Base_Contoller:
<?php
class Base_Controller extends Controller {
/**
* Catch-all method for requests that can't be matched.
*
* #param string $method
* #param array $parameters
* #return Response
*/
public function __call($method, $parameters)
{
return Response::error('404');
}
public function get_tweet()
{
...........
return $tweets;
}
}
How is this possible?
//////////////////////UPDATE/////////////////////////////
application/models/tweets.php
<?php
class Tweets {
public static function get($count = 3)
{
Autoloader::map(array(
'tmhOAuth' => path('app').
'libraries/tmhOAuth-master/tmhOAuth.php',
'tmhUtilities' => path('app').
'libraries/tmhOAuth-master/tmhUtilities.php'
));
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'xxx',
'consumer_secret' => 'xxx',
'user_token' => 'xxxxx',
'user_secret' => 'xxxxx',
'curl_ssl_verifypeer' => false
));
$code = $tmhOAuth->request('GET',
$tmhOAuth->url('1.1/statuses/user_timeline'), array(
'screen_name' => 'xxx',
'count' => $count
));
$response = $tmhOAuth->response['response'];
$tweets = json_decode($response, true);
return $tweets;
}
}
application/views/widgets/tweets.blade.php
#foreach ($tweets)
test
#endforeach
application/views/layouts/default.blade.php
....
{{ $tweets }}
....
application/composers.php
<?php
View::composer('widgets.tweets', function($view)
{
$view->tweets = Tweets::get();
});
View::composer('layouts.default', function($view)
{
$view->nest('tweets', 'widgets.tweets');
});
application/controllers/base.php
<?php
class Base_Controller extends Controller {
/**
* Catch-all method for requests that can't be matched.
*
* #param string $method
* #param array $parameters
* #return Response
*/
public $layout = 'layouts.default';
public function __call($method, $parameters)
{
return Response::error('404');
}
}
application/controllers/home.php
<?php
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public $restful = true;
public function get_index()
{
Asset::add('modernizr', 'js/thirdparty/modernizr.js');
Asset::add('jquery',
'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
Asset::add('scripts', 'js/scripts.js');
$this->layout->title = 'title';
$this->layout->nest('content', 'home.index', array(
//'data' => $some_data
));
}
}
Is giving me an
Undefined variable: tweets
error

Step 1 - Make a view just for your tweets, let's call it widgets/tweets.blade.php, that will accept your $tweets data. This makes it very easy to cache the tweets view in the future if you want a little more performance. We also want a model that will generate the tweet data for you.
Step 2 - Pass the tweet data into your tweets view, let's use a View Composer for this so the logic is kept with (but outside) the view.
Step 3 - Create your default layout, let's call this layout/default.blade.php. This will accept $content and $tweets. We'll nest the tweets view with another View Composer. You can nest the $content in your controller actions.
Step 4 - Set the $layout on your Base_Controller.
Step 5 - Profit!
Note - If these are your first view composers then you'll need to include them in application/start.php
// application/models/tweets.php
class Tweets {
public static function get($count = 5)
{
// get your tweets and return them
}
}
// application/views/widgets/tweets.blade.php
#foreach ($tweets)
{{-- do something with your tweets --}}
#endforeach
// application/views/layouts/default.blade.php
<section class="main">{{ isset($content) ? $content : '' }}</section>
<aside class="widget widget-tweets">{{ $tweets }}</aside>
// application/composers.php
View::composer('widgets.tweets', function($view)
{
$view->tweets = Tweets::get();
});
View::composer('layouts.default', function($view)
{
$view->nest('tweets', 'widgets.tweets');
});
// application/start.php (at the bottom)
include path('app').'composers.php';
// application/controllers/base.php
class Base_Controller extends Controller {
public $layout = 'layouts.default';
}
// application/controllers/home.php
class Home_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
$this->layout->nest('content', 'home.welcome');
}
}

View::share('key', 'value');
in you view use (Blade syntax)
{{$key}}
or (PHP syntax)
echo $key;

Related

Does Laravel support "typed" requests?

To transform a database entity to an API response Laravel support resources, eg. UserResource extends JsonResource. The resource allows me to cleanly define which fields from the entity should be included in the response, how to transform them etc.
Is there a similar functionality for requests? My requests typically look like this:
public function create(JsonRequest $request): UserResource
{
$data = $request->json()->all();
/* Remove, transform, add request fields etc. */
$user = User::create($data);
$user->save();
return new UserResource($user);
}
In our case we have a legacy database behind a modern API so there are a number of fields that need to transformed, renamed etc. before pushing them into the entity class. The fields differ from request to request but the steps are very similar. Is there a less boilerplate-y way to do this, something similar to how resources transform entities to responses?
Something like:
class UserRequest extends JsonRequest {
public function fromArray(JsonRequest $request) {
…
}
}
Then the request could look like this:
public function create(UserRequest $request): UserResource
{
$user = User::create($request);
$user->save();
return new UserResource($user);
}
I suppose, that most of your problems can solve form request. See example below
Form request class:
namespace App\Http\Requests;
use Carbon\Carbon;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
class TestRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'date' => 'required|date_format:Y-m-d H:i:s',
'name' => 'required|string',
];
}
// here you can specify custom error messages
public function messages()
{
return [
'date.required' => 'No date specified',
'date.date_format' => 'Invalid date format',
'name.required' => 'No name specified',
'name.string' => 'Invalid name format',
];
}
// here you can implement some data mapping before validation
protected function validationData()
{
return $this->transform($this->all());
}
// some data transformation logic
// You can place it anywhere in your applciation services
protected function transform($input)
{
$transformed = [];
foreach ($input as $field => $value) {
if ($field == 'name') {
$value = strtoupper($value);
} elseif ($field == 'date') {
$value = Carbon::parse($value)->toDateTimeString();
}
$transformed[$field] = $value;
}
return $transformed;
}
public function failedValidation(Validator $validator)
{
// here you can implement custom validation failure
parent::failedValidation($validator);
}
}
Here is my test route: Route::get('/test', 'TestController#index');
And controller:
use App\Http\Requests\TestRequest;
class TestController extends Controller
{
public function index(TestRequest $request)
{
return response()->json($request->validated());
}
}
So, then requesting route: curl -H 'Accept: application/json' 'http://localhost:8000/test?date=01.01.2019&name=petya'
And getting response: {"date":"2019-01-01 00:00:00","name":"PETYA"}
And dont be shy to see source code of request and form request, cause of not all methods you wish are described in docs. Hope this helps

Undefined property in laravel framework accessor calling belongsTo

I need your help.
I'm working with Laravel Framework and I have a trouble with a belongsTo relationship.
My project have to tables, address book and delivery types, the columns in tables are:
address_book
id
name
mail
delivery_1
deliverytype_id_1
delivery_2
deliverytype_id_2
...
delivery_types
id
name
...
The code of delivery types model is this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class DeliveryType extends Model
{
protected $table = 'delivery_types';
protected $guarded = ['id'];
}
This is the address book model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AddressBook extends Model
{
protected $table = 'address_book'; // table
protected $guarded = ['id']; // primary key
protected $appends = ['delivery', 'actions']; // accessors
protected $delivery = '';
public function del1() {
return $this->belongsTo('App\DeliveryType', 'deliverytype_id_1', 'id')->withDefault();
}
public function del2() {
return $this->belongsTo('App\DeliveryType', 'deliverytype_id_2', 'id');
}
/**
* Accessor: get the actions column information.
*
* #return string
*/
public function getActionsAttribute() {
$actions = '<a href='. route('admin.addressbook.show', $this->id) .'>'.
'show<i class="livicon" data-name="info" data-size="18" data-loop="true" data-c="#428BCA" data-hc="#428BCA" title="view contact"></i></a>';
return $actions;
}
/**
* Accessor: get the deliveries information.
*
* #return string
*/
public function getDeliveryAttribute () {
$deliveries = [
['val' => $this->delivery_1, 'type' => $this->del1()->name], //row error
['val' => $this->delivery_2, 'type' => $this->del2()->name]
];
foreach($deliveries as $delivery) {
$this->delivery = (strlen($delivery['val']) > 0) ?
$this->appendString($this->delivery, '<strong>'.$delivery['type'].'</strong> '.$delivery['val']) :
$this->delivery;
}
return $this->delivery;
}
protected function appendString(string $str, string $val) {
return (strlen($str) > 0) ? $str.'<br>'.$val : $val;
}
In the html page the data is loaded through ajax call to the controller function. This is the code of function:
public function data(Request $request) {
// Init data
$this->addressbooks = AddressBook::get(
['address_book.id',
'address_book.name',
'address_book.email',
'address_book.delivery_1',
'address_book.delivery_2');
// Return json array
header("Content-Type: application/json");
return $this->addressbooks;
}
When the page call the function through ajax, the framework return the error "Undefined property" in the accessor getDeliveryAttribute, where I try to call relationship belongs to about delivery type ID and its reference in the delivery types table.
What I'm doing wrong? Thanks in advance to those who can help me.
Here is how I would write the AddressBook model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
//use Illuminate\Support\Facades\Log;
class AddressBook extends Model
{
protected $table = 'address_book'; // table
//protected $guarded = ['id']; // primary key
// always load these accessors
protected $appends = [
'delivery',
'actions',
];
protected $mail = '';
// No need for $delInfo, use $this->delivery (same data)
// protected $delInfo = '';
public function category() {
/*
You can use `AddressBookCategory::class` instead of `'App\DeliveryType'`
http://php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name
Because models are in the same napespace, we don't need to write \App\AddressBookCategory
*/
return $this->belongsTo(AddressBookCategory::class, 'address_book_category_id', 'id');
}
public function deliveryType1() {
return $this->belongsTo(DeliveryType::class, 'deliverytype_id_1', 'id')
/*
Specify empty string for the name, so that when we access
$this->deliveryType1->name it's equal ''
*/
->withDefault(['name' => '']);
}
public function deliveryType2() {
return $this->belongsTo(DeliveryType::class, 'deliverytype_id_2', 'id')
->withDefault(['name' => '']);
}
/**
* Accessor: get the actions column information.
*
* Access by using: $this->action
*
* #return string
*/
public function getActionsAttribute() {
// moved this into multi line, easier to read
return '<a href='. route('admin.addressbook.show', $this->id) .'>'
.'show'
.'<i class="livicon" data-name="info" data-size="18" data-loop="true" data-c="#428BCA" data-hc="#428BCA" title="view contact"></i>'
.'</a>';
}
/**
* Accessor: get the deliveries information
*
* Access by using: $this->delivery
*
* #return string
*/
public function getDeliveryAttribute () {
// I've updated logic here, it should be easier to read...
$delivery = [];
if ( ! empty($this->delivery_1) ) {
$delivery[] = '<strong>'.$this->deliveryType1->name.'</strong> '.$this->delivery_1;
}
if ( ! empty($this->delivery_2) ) {
$delivery[] = '<strong>'.$this->deliveryType2->name.'</strong> '.$this->delivery_2;
}
// join array elements with a string `<br>`
return implode('<br>', $delivery);
}
}

How injecting a array in my view with laravel 5 using a facade and the App:make method in the view

I created a facade that works well, a using service container :
My class:
namespace App\Classes;
class Administration {
public function message_success()
{
$message = "success";
return $message;
}
}
My view:
{{ App::make('administration')->message_success() }}
Resultat: Ok
But when I want to work with a array, I can not retrieve a value in my view with App::make.
My class:
namespace App\Classes;
class Administration {
public function message_test($message, array $type = array())
{
$type = ['message1' => 'valeur1', 'message2' => 'valeur2'];
return $message;
}
}
How to build the syntax in my view?
In your view
{{ App::make('administration')->message_test($message) }}
In your class
namespace App\Classes;
class Administration {
public function message_test($message)
{
$type = ['message1' => 'valeur1', 'message2' => 'valeur2'];
return $type[$message];
}
}
Instead of
namespace App\Classes;
class Administration {
public function message_test($message, array $type = array())
{
$type = ['message1' => 'valeur1', 'message2' => 'valeur2'];
return $message;
}
}
As you can see I dropped the $type parameter, you are defining/overwriting it within your method. Also, to return the value of message1 you need to access $type with the key being message1.

How load() works in EAV model in Magento 1.9.2?

$product = Mage::getModel('catalog/product')->load($this->getProductId());
load() will go Mage_Core_Model_Abstract inside load() function,
public function load($id, $field=null)
{
$this->_beforeLoad($id, $field);
$this->_getResource()->load($this, $id, $field);
$this->_afterLoad();
$this->setOrigData();
$this->_hasDataChanges = false;
return $this;
}
Now this should go to abstract class Mage_Catalog_Model_Resource_Abstract extends Mage_Eav_Model_Entity_Abstract where load process is defined:
public function load($object, $entityId, $attributes = array())
{
Varien_Profiler::start('__EAV_LOAD_MODEL__');
/**
* Load object base row data
*/
$select = $this->_getLoadRowSelect($object, $entityId);
$row = $this->_getReadAdapter()->fetchRow($select);
...
if (empty($attributes)) {
$this->loadAllAttributes($object);
} else {
foreach ($attributes as $attrCode) {
$this->getAttribute($attrCode);
}
}
$this->_loadModelAttributes($object);
...
}
But I can't understand how
$this->_getResource()->load($this, $id, $field); in Mage_Core_Model_Abstract
connects to abstract class Mage_Catalog_Model_Resource_Abstract???
Because load($this, $id, $field) is going to abstract class Mage_Core_Model_Resource_Db_Abstract extends Mage_Core_Model_Resource_Abstract :
public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
{
if (is_null($field)) {
$field = $this->getIdFieldName();
}
$read = $this->_getReadAdapter();
if ($read && !is_null($value)) {
$select = $this->_getLoadSelect($field, $value, $object);
$data = $read->fetchRow($select);
if ($data) {
$object->setData($data);
}
}
$this->unserializeFields($object);
Please help me out.
There is the _construct method in the product model (catalog/product). This method is triggered when one creates an object. It shows which resource model to use:
$this->_init('catalog/product');
Next, in the Mage_Core_Model_Abstract in the load method, there is an expression $this->_getResource(). It can bring back the object of the class
Mage_Catalog_Model_Resource_Product (which, in turn, is inherited from Mage_Catalog_Model_Resource_Abstract).
Basically, every model can have its own resource model with its own class.

Getting url data to show in the url bar

I'm using Laravel 4 and I'm trying to get the url bar to display the text url that is saved in the database instead of using the id.
This is my routes.php
Route::get('/{id}', function($id = 1){
if(is_numeric($id))
{
$page = Menu::find($id);
$action = 'content';
return App::make('HomeController')->$action($id);
} else {
$column = 'url';
$url = Seo::where($column, '=', $id)->get();
$action = 'show';
return App::make('HomeController')->$action($url[0]->id);
}
});
I'm also using a pivot table to link the menu to the seo.
Seo model
<?php
class Seo extends \Eloquent {
protected $fillable = array('url', 'meta_title', 'meta_description', 'keywords');
protected $guarded = array('id');
protected $table = 'seo';
public static $rules = array(
'title' => '',
'content' => '',
'image' => ''
);
public function menu(){
return $this->belongsToMany('Menu', 'menu_seo', 'seo_id', 'menu_id');
}
}
Menu model
<?php
class Menu extends \Eloquent {
protected $fillable = array('title', 'menu_id', 'image');
protected $guarded = array('id');
protected $table = 'menus';
public static $rules = array(
);
public function seo(){
return $this->belongsToMany('Seo', 'menu_seo', 'menu_id', 'seo_id');
}
}
HomeController
public function content($id)
{
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$menu = Menu::where('id', $id)->firstOrFail();
//dd($menu->frames);
return View::make('index', compact('menus_child'))->with('menu', $menu);
}
and then I call my views that references the menu like this
#foreach($menu->banner as $banners)
{{ $banners->title }}
#endforeach
I'm still not sure how you want to retrieve the correct URL associated to an id but that shouldn't really matter. The basic principle is just to fetch the URL and make a redirect:
Route::get('/{id}', function($id = 1){
if(is_numeric($id))
{
$page = Menu::find($id);
$url = 'foo'; // get correct URL somehow
return Redirect::to($url);
} else {
// ...
}
});

Resources