This is my controller
public function show($restaurant_id)
{
$data = Restaurant::find($restaurant_id)->waitingtimes();
echo $data->first()->value; exit;
I got Trying to get property of non-object though the waitingtime model is mapped to a database table that has the value column.
Could you help please? Also, could you tell me where can I read the documentation about the returning type of the function find() thanks
Edit 1
The data is not empty; I can see the database, and the restaurant table has the id 20 and the table waitingtimes has values that its restaurant_20 is 20
Actually - what is better is to map the route to a model.
So in your Routes.php file:
Route::model('restaurant', 'Restaurant');
Route::get('/restaurant/{restaurant}', ['as' => 'restaurant.show', 'uses' => RestaurantController#show]);
Then in your controller file:
public function show(Restaurant $restaurant)
{
echo $restaurant->waitingtimes()->first()->value;
}
You can read more about Route Model Binding here.
here is the source of find()
https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L643
and here you have the docs
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Model.html#method_find
for your non-object error,
your $data is empty, you should check that before you use the $data collection
Finally I found the solution myself, which is:
public function show($restaurant_id)
{
$data = new Restaurant(array('id' => $restaurant_id));
$data = $data->waitingtimes();
echo $data->first()->value; exit;
Related
I have the following route in my laravel project
Route::get('/',[
'uses' => 'MakeController#index'
]);
Controller
class MakeController extends Controller{
public function index(){
$makes = MakeType::all();
return View::make('Index', $makes);
// tried this
// return view('Index',compact('makes'));
}
}
index.blade.php
<select>
<option>Select Make</option>
#foreach($makes as $make)
<option>{{$make->name}}</option>
#endforeach
</select>
Problem:
The problem is when I try to load the index page, it shows me the following error
Use of undefined constant makes - assumed 'makes' (this will throw an Error in a future version of PHP) (View: /customers/6/1/4/alle-voertuigen.nl/httpd.www/resources/views/Index.blade.php)
I've visit all the possible links, and tried different ways of doing it, but nothing is working with me.
This is what it is showing when I do dd($makes), in attributes I have the name column
Please help me, thanks
Use with in your controller.
return View::make('Index')->with(compact('makes'));
Should work.
If you use this way to return a view, you must use with to add parameters :
return View::make('Index')->with('makes', $makes);
I will advise to use the view helper, I don't know what version of Laravel you use :
return view('Index', ['makes' => $makes]);
Just only thing sure, it can't work if you don't pass variables as an array (with compact or ['myVariable' => $var])
class MakeController extends Controller {
public function index() {
$makes = MakeType::all();
return View::make('index', $makes);
// tried this
// return view('index',compact('makes'));
}
}
its index not Index
I want to passing a data from Shirts to details, to know a detail of product from Shirts. How to do that? I have some error here
Error's Message
This my web view
web View
FrontController.php
class FrontController extends Controller
{
public function index()
{
$shirts=Product::all();
return view('front.home', compact('shirts'));
}
public function shirts()
{
$shirts=Product::all();
return view('front.shirts', compact('shirts'));
}
public function detail($id)
{
return view('front.shirt', ['detail' => Product::findOrFail($id)]);
}
Route
Web.php
Route::get('/', 'FrontController#index')->name('home');
Route::get('/shirts', 'FrontController#shirts')->name('shirts');
Route::get('/detail', 'FrontController#detail')->name('detail');
Shirts.blade.php
<a href="{{route('detail', $shirt->id)}}">
you missing argument in route('detail')
Edit to:
Route::get('/detail/{id}', 'FrontController#detail')->name('detail');
And Try again.Hope this help :D
there is two three thing that need to change first is route file
it should be like this
Route::get('/detail/{$id}', 'FrontController#detail')->name('detail');
from this you get that parameter $id that you passes in controller .
and for view as per i see in your code there your route is
<a href="{{route('detail', $detail->id)}}">
which has to be like this
<a href="{{route('detail', $shirt->id)}}">
i think it might solve your problem
hope it will help you
I think in your Route you need to give Id parameter also,
Route::get('/detail/{id}', 'FrontController#detail')->name('detail');
I am currently trying to make a function that calls different scopeQueries such as scopeByLocation() or scopeByPublished() on models defined in an array. I've got the basics working through [this link][1]. However, when trying to access custom made query scopes that are defined in the corresponding model, I get the following error: "Call to undefined method Illuminate\Database\Query\Builder::ForLocation($location)->get()".
What I want to achieve is a single method which loops through every model in the array of models and retrieves & calls the right scopeQuery on the model, something like this:
$modelElements = $model::{$queryScope}();
Where for example $model = 'Modules\News\Models\Article'
And $queryScope is a defined queryScope in the model itself. E.g. scopeForLocation($location).
I've tested $queryScope = 'all' and I get a result just fine, however when I try to access a custom queryScope ($queryScope = 'ForLocation($location)->get') that exists within for example the Location model, I get the following error: "Call to undefined method Illuminate\Database\Query\Builder::ForLocation($location)->get()".
So this all happens in a foreach-loop where every model in my models-array gets called and then the corresponding queryScope gets called on the model.
Why does the $queryScope = 'all' method works on my dynamic models, but other scopes throw an error? I really hope someone could help me get into the right direction with this issue.
Thanks in advance,
J. Doe.
Okay, I've finally solved it the following way:
//array of models
public function models()
{
return [
'Modules\Website\Models\Article',
...
];
}
//function that retrieves all elements for a model
public function getAllElementsForModel($model, $param)
{
//instantiate model
$model = new $model;
//call queryScope
//'queryScope' could be any queryScope that is defined within your model(s),
//the parameters are needed for the associated queryScope
$query = call_user_func_array([$model, 'queryScope'], [$param1, $param2]);
$result = $query->get();
//do stuff with your $result
}
//retrieves all
public function all($param)
{
//loop through the array of models
foreach($this->models() as $model){
$this->getAllElementsForModel($model, $param);
//do stuff here...
}
}
Sharing is caring!
I have a one-to-many relationship defined in Laravel:
TheModules has many TheModulesActions and TheModulesActions has one TheModules.
It works well, however I would like to set a custom attribute inside TheModulesActions called url.
To achieve that I need the following code:
protected $appends = array('url');
public function getUrlAttribute() {
$nameURL = SharedMethods::slugify($this->title);
$url = "#/control/{$module}/{$nameURL}/{$this->id}";
return $url;
}
Till now everything works, however $module variable is empty, all I have is $this->moduleID which is linked to TheModules.
I need $this->TheModules()->title but it doesn't work inside my getUrlAttribute method.
Is there any way to access relationship data inside getAttribute?
EDIT
The Relationships in TheModulesActions class are:
public function TheModule() {
return $this->belongsTo('App\TheModules');
}
The Relationships in TheModules class are:
public function TheActions() {
return $this->hasMany('App\TheModulesActions', 'moduleID');
}
Tried $this->TheModules->title:
Gives me this error:
ErrorException in TheModulesActions.php line 15:
Trying to get property of non-object
You can it by dropping the parenthesis () on the relation name as:
$this->TheModules->title
When you use parenthesis with relation name then Laravel returns an instance of query builder.
I'm freshman in CI.
Today I got an error like this when I learned CI tutorial.
You must use the "set" method to update an entry.
Filename:
F:\xampp\htdocs\ISA2013\ISA2013\system\database\DB_active_rec.php
Line Number: 1174
My Model code like this.
class Apply_model extends CI_Model {
public function add_record($data){
$query = $this->db->insert('help_user');
//return;
}
}
And I just coded like that in Control:
public function create(){
$data = array('USER_NUMBER' => $this->input->post('stuNO'),
'USER_EMAIL' => $this->input->post('email'),
'USER_INFO' => $this->input->post('info')
);
$this->apply_model->add_record($data);
$this->index();
}
..when I run class/create, I got the top of error..
Can someone help me?
You need to pass your data as parameter to $this->db->insert function as mentioned by nevermind
As mentioned by nevermind, you have to pass data array to the insert function. Also please note that data array should be an associative array, where keys will be the table fields and values will be values for the fields.
You don't pass the parameter data into $this->db->insert function. You can change the code line :
$query = $this->db->insert('help_user');
to:
$query = $this->db->insert('help_user', $data);
on your model you must like this
public function add_record($data)
{
$this->db->insert('help_user',$data); //it's more be simple
}
if you write a return; you will return a null value to your controller
if you write like
$query = $this->db->insert('help_user');
you just make a variable $query with value $this->db->insert('help_user'); so thats make a error
hey buddy you are missing one parameter and you are not returning the value, you should have
return $this->db->insert('help_user', $data);