I am trying to get data from the database and simply display it in the view
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests', compact($messages));
}
The view
#foreach ($messages as $message)
<h1>{{ $message->first_name }}</h1>
#endforeach
But I am getting this error
compact(): Argument #1 must be string or array of strings, Illuminate\Database\Eloquent\Collection given
The PHP method compact() is a little tricky with its syntax. I make this same mistake all the time.
Change:
return view('dashboard/projects-interests', compact($messages));
to:
return view('dashboard/projects-interests', compact('messages'));
compact() looks for a string representation of the variable.
There are multiple ways you could write this.
using compact where you pass strings representing the variable
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests', compact('messages'));
}
using ->with
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests')->with(['messages' => $messages]);
}
With using laravel magic
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests')->withMessages($messages);
}
Personally, I prefer this form as it avoids pointless variables
public function index()
{
return view('dashboard/projects-interests')
->withMessages(ProjectInterestedMessages::get());
}
You don't need $
return view('dashboard/projects-interests', compact('messages'));
Related
so here is my model codes:
public function GetId()
{
$this->db->select('count(*) as total');
$this->db->from($this->table);
$this->db->where('dibaca', null);
$query = $this->db->get();
return $query->result_array();
}
and this is my code in html:
<?= $DataId['total']; ?>
i already call the function to my controller as DataId,
and i get error that Undefined array key "total"
can you guys tell me what is wrong in it?
Some untested advice:
Your model can be refined to:
public function countNullDibaca(): int
{
return $this->db
->where("dibaca", null)
->count_all_results($this->table);
}
Your controller should call for the model data and pass it to the view.
public function myController(): void
{
$this->load->model('my_model', 'MyModel');
$this->load->view(
'my_view',
['total' => $this->MyModel->countNullDibaca()]
);
}
Finally, your view can access the variable that associates with the first-level key in the passed in array.
<?= $total; ?>
Here is a related post which speaks on passing data from the controller to the view.
Replace the result_array() at the model with
num_rows()
and you can delete ['total'] from your code in html, or like this:
<?= $DataId; ?>
I actually managed to get data from HasManyThrough relations, but when I want to reach the relational data, it throws the error:
Property [publishings] does not exist on this collection instance.
(View: C:\Xampp\htdocs\wave\resources\views\pages\category.blade.php)
As can be seen in the picture down below, I received "publishings" data.
Where I got the error from is this Blade file:
category.blade.php
#foreach ($data->publishings as $item)
<div></div>
#endforeach
Since I have the data, the codes down below can be unnecessary for the solution, but they are there, just in case.
CategoryController.php
public function index($category)
{
$data = Category::where('slug', $category)
->with(['publishings' => function ($query) {
$query->where('slug', '!=', 'placeholder')->latest()->paginate(10);
}])->get();
return view('pages.category')->with('data', $data);
}
Category.php
public function assets()
{
return $this->hasMany('App\Models\Asset');
}
public function publishings()
{
return $this->hasManyThrough('App\Models\Publishing', 'App\Models\Asset');
}
Asset.php
public function category()
{
return $this->belongsTo('App\Models\Category');
}
public function publishings()
{
return $this->hasMany('App\Models\Publishing');
}
Publishing.php
public function asset()
{
return $this->belongsTo('App\Models\Asset');
}
Thanks for your help.
You are passing an Eloquent Collection to the view. If you want to query only the first result, you can use the first() method of the Query Builder instead of get():
public function index($category)
{
$data = Category::where('slug', $category)
->with(['publishings' => function ($query) {
$query->where('slug', '!=', 'placeholder')->latest()->paginate(10);
}])->first();
return view('pages.category')->with('data', $data);
}
If anyone has the same problem and comes across this message, the solution, in my case was actually quite easy. The 3rd line in the picture says:
0 => App\Models\Category
And I was trying to reach the relational data as if the $data is an object. That's why
$data->publishings
thrown the error. Solution is
$data[0]->publishings
Or return only that data from the controller:
return view('pages.category')->with('data', $data[0]);
i tried to show some data from database to my view. But i get undefined variable error.
Route::get('pages/tab-content-games', 'Admin\TournamentCrudController#get_data');
and this is my function
public function get_data(){
$data['data'] = \DB::table('tournament')->get();
return view('pages.tab-content-games', ['data' => $data]);
}
in tab-content-games.blade.php
i just print the variable
{{ print_r($data) }}
can someone helped me, what part i'm doing wrong. thankyou
Please try with this return view('pages.tab-content-games', compact('data'));
Try this:
i think you have to use rural name as your database name tournaments instead of tournament. also you can use compact.
public function get_data(){
$data = \DB::table('tournaments')->get();
return view('pages.tab-content-games', compact('data');
}
PS: consider using model in your controller instead of using DB for simple functions.
use App\Tournament;
public function get_data(){
$data = Tournament::all();
return view('pages.tab-content-games', compact('data')
}
Try this:
return view('pages.tab-content-games')->with('data', $data);
I have trouble with showing related variable in laravel blade
public function GetAll()
{
$news=DB::table('news')->get();
return View('index',['news'=>$news]);
}
In View:
#foreach($news as $new)
...
{{$new->comments()->count()}} Comments
...
#endforeach
Its also doesnt work for any variables of object but working good for first item:
public function Count()
{
$news=News::find(1);
echo $news->comments()->count();
}
public function GetAll()
{
$news = News::with('comments')->get();
return View('index',['news'=>$news]);
}
Use ORM instead of DB.
I have following functions in Controller.
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
$this->SaveChanges($request);
}
private function SaveChanges($request) {
if($request['CountryID'] == 0) {
$Country = new \App\Models\CountryModel();
}
else {
$Country = \App\Models\CountryModel
::where('CountryID', $request['CountryID'])->first();
}
$Country->Country = $request['Country'];
$Country->CountryCode = $request['CountryCode'];
$Country->save();
return redirect()->route('AllCountries');
}
public function AllCountries() {
$Countries = \App\Models\CountryModel::all();
return view('Country.List', array('Countries' => $Countries));
}
Issue is in below line: When I call function SaveChanges, I am not able to see List of countries page and when I write the code directly in UpdateCountry function, it redirect route successfully.
return redirect()->route('AllCountries');
Anybody faced this issue before ?
Your route is being handled by the UpdateCountry function. Laravel will take action based on the returned value from this function. However, you're not returning anything from this function.
You call SaveChanges, which returns a Redirect object, but then you don't return anything from your UpdateCountry function. You need that Redirect object from the UpdateCountry function in order for Laravel to actually return the redirect to the client.
Update your UpdateCountry function to this:
// added the return statement, so this function will return the redirect
// to the route handler.
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
return $this->SaveChanges($request);
}
Maybe you missed a return in $this->SaveChanges($request). It has to be:
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
return $this->SaveChanges($request);
}
I hope it works fine for you.