I tried, but don't work: i don't know why...
Page.php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class takeData extends Controller
{
public function selectdata()
{
$data_users = DB::select('select * from persons where id = :id', ['id' => 1]);
return view('page', ['persons' => $data_users]);
}
}
....
page.blade.php
....
#foreach ($persons as $person)
<p>$person</p>
#endforeach
routes.php
Route::get('takeData', 'Page#selectdata');
Error:
Whoops, looks like something went wrong.
If I remove '#foreach ($ persons as $ person) $ person #endforeach' in page.blade.php everything works correctly, why is it wrong?
Thanks a lot!!!
On your view, change <p>$person</p> to this:
<p>{{ $person }}</p>
The {{ }} is used to output data as specified in the Blade Documentation.
Related
Hello I need some help I am learning Laravel 9 and I have a problem, in the UserController I declare a public function show with a parameter $id and return it in user.blade.php file and call using template literals, my problem is I cannot get the data inside my function show.
This is the codein UserController file
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function show($id){
$data = array([
"id" => $id,
"name" => "John Doe",
"age" => 22,
"email" => "jhondoe#gmail.com"
]);
return view("user", $data);
}
}
web.php file
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
Route::get('/show/{id}', [UserController::class, 'show']);
user.blade.php file
{{ $data }}
it says
"ErrorException Undefined variable $data"
enter image description here
The view function expects an associative array as second parameter (see docs). Each key of the array will have an equivalent variable in your blade view.
So you should respond either be :
return view("user", ['data' => $data]);
Or use PHP's compact helper
return view("user", compact('data'));
Or change your view so that it uses
{{ $id }}
{{ $name }}
{{ $age }}
{{ $email }}
Do whatever makes more sense to your use case
I do have a problem with the out of this code
this is the models used
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Navigation extends Model
{
protected $table = "navigation" ;
protected $primaryKey = "navi_id";
public function PrimaryNavigation()
{
return $this->hasMany(PrimaryNavigation::class);
}
}
and this how the query goes
<?php
namespace App\Http\Controllers;
use App\Navigation;
use App\PrimaryNavigation;
use Illuminate\Http\Request;
class NavigationController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$mainNavi = Navigation::with('PrimaryNavigation')->get() ;
return view('dump')->with('data',$mainNavi);
}
}
?>
Whenever i try to print out the result coming from this query it never showing the correct output from the related table .
when i print the output it shows correct result but when i tried to print out the related table data only it shows NULL value
#foreach($data as $single)
{{dd($single)}}</br>
#endforeach
this code shows the correct output
{"navi_id":8,"navi_name":"fouad","navi_route":"\/fouad","navi_sort":0,"navi_created_at":"2019-09-23 02:21:24.000000","navi_last_update":"2019-09-23 02:21:29.000000","primary_navigation":[{"primary_navi_id":5,"navigation_navi_id":8,"primary_navi_name":"tamer","primary_navi_route":"\/tamer","primary_navi_created_at":"2019-09-23 02:28:38.000000","primary_navi_last_update":"2019-09-23 02:28:40.000000","primary_navi_sort":0},{"primary_navi_id":13,"navigation_navi_id":8,"primary_navi_name":"momo","primary_navi_route":"\/momo","primary_navi_created_at":"2019-09-23 14:44:13.000000","primary_navi_last_update":"2019-09-23 14:44:15.000000","primary_navi_sort":0}]}
{"navi_id":9,"navi_name":"mohamed","navi_route":"\/mohamed","navi_sort":1,"navi_created_at":"2019-09-23 14:09:30.000000","navi_last_update":"2019-09-23 14:09:33.000000","primary_navigation":[{"primary_navi_id":6,"navigation_navi_id":9,"primary_navi_name":"soso","primary_navi_route":"\/SOSO","primary_navi_created_at":"2019-09-23 14:10:33.000000","primary_navi_last_update":"2019-09-23 14:10:36.000000","primary_navi_sort":0},{"primary_navi_id":14,"navigation_navi_id":9,"primary_navi_name":"yoyo","primary_navi_route":"\/yoyo","primary_navi_created_at":"2019-09-23 14:44:34.000000","primary_navi_last_update":"2019-09-23 14:44:37.000000","primary_navi_sort":0}]}
{"navi_id":10,"navi_name":"yasser","navi_route":"\/yasser","navi_sort":2,"navi_created_at":"2019-09-23 14:09:43.000000","navi_last_update":"2019-09-23 14:09:45.000000","primary_navigation":[{"primary_navi_id":7,"navigation_navi_id":10,"primary_navi_name":"mathy","primary_navi_route":"\/math","primary_navi_created_at":"2019-09-05 14:10:57.000000","primary_navi_last_update":"2019-10-09 14:10:59.000000","primary_navi_sort":0}]}
{"navi_id":11,"navi_name":"mahy","navi_route":"\/mahy","navi_sort":3,"navi_created_at":"2019-09-23 14:10:00.000000","navi_last_update":"2019-09-23 14:10:02.000000","primary_navigation":[{"primary_navi_id":8,"navigation_navi_id":11,"primary_navi_name":"toty","primary_navi_route":"\/toyt","primary_navi_created_at":"2019-09-23 14:11:20.000000","primary_navi_last_update":"2019-09-23 14:11:23.000000","primary_navi_sort":0},{"primary_navi_id":12,"navigation_navi_id":11,"primary_navi_name":"JAJA","primary_navi_route":"\/JAJ","primary_navi_created_at":"2019-09-23 14:43:54.000000","primary_navi_last_update":"2019-09-23 14:43:57.000000","primary_navi_sort":0}]}
but when i tried to show the related table only it gives null
#foreach($data as $single)
{{dd($single->primary_navigation)}}</br>
#endforeach
you must be instead of primary_navigation use ->PrimaryNavigation(as defined in your relationship)
#foreach($data as $single)
{{dd($single->PrimaryNavigation)}}</br>
#endforeach
or change your relation method name
public function primary_navigations()
{
return $this->hasMany(PrimaryNavigation::class);
}
and use
#foreach($data as $single)
{{dd($single->primary_navigations)}}</br>
#endforeach
I am trying to create an adverts section on my landing page of a laravel 5.4 project using data from a database but it is not working.
in my welcome, I have added this lines of code
foreach($adverts as $advert) {
$advert;
}
I have then created a WelcomeController with the following code
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class WelcomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$adverts = DB::table('adverts')->get();
return view('welcome', ['adverts' => $adverts]);
}
}
This gives me an error $adverts is not defined
What could I be doing wrong?
$adverts is not defined error is coming because you are missing echo or {{ $adverts->item }} in case of Laravel.
You need to echo $adverts data in foreach in your blade file in the below way:
#foreach($adverts as $advert)
{{ $advert->item }}
#endforeach
Or you can simply use PHP as well like below:
foreach($adverts as $advert){
echo $advert->item;
}
You can also convert your data to array and then display in foreach like below:
$adverts = DB::table('adverts')->get();
$adverts = json_decode( json_encode($adverts), true);
#foreach($adverts as $advert)
{{ $advert['item'] }}
#endforeach
You can first check what's coming in $advert array in the below way:
echo "<pre>"; print_r($adverts); die;
Thanks For Reading.
I'm new in Laravel, i want to try to change the output of Database with #foreach blade, there is the example :
This is Route :
Route::get('/home', 'warnajati#index');
This is Controller :
public function index()
{
$post = DB::table('posts')->get();
return view('warnajati', ['posts'=>$post]);
}
This is Views :
#foreach ($posts as $post)
<div class="title"><h3>{{$post->title}}</h3></div>
#endforeach
with Output of $post->title is "This is The Looonger Title you ever know" ,
and i want to make the title is shorter with Wordlimit() function i have made :
function wordlimit($text, $limit=10)
{
if (strlen($text)>$limit) {
# code...
$word = mb_substr($text,0,$limit-3)."...";
}else{
$word =$text;
}
};
How and Where i must place that function in laravel Project ?? please help me..
Your function has no return value... Laravel already has this function: http://laravel.com/docs/5.3/helpers#method-str-limit
#foreach ($posts as $post)
<div class="title"><h3>{{ str_limit($post->title, 10) }}</h3></div>
#endforeach
You can use Laravel's Accessor for doing that like this inside a Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function getShortTitleAttribute($value)
{
// return shortened title here ...
}
}
and then you can use it in blade like this:
{{ $post->short_title }}
Hope this helps!
You can put your function in helpers.php file from libraries folder.
Just make sure that you have helpers.php file autoloaded in composer.json file:
"autoload": {
"files": [
"libraries/helpers.php"
],
},
If you had to add this to your composer.json you will also have to run composer dump-autoload command from terminal.
For more info check out Best practices for custom helpers on Laravel 5.
Laravel version 5.2
My controller file:
<?php
namespace App\Http\Controllers;
use App\questions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
class Dashboard extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function fetchQuestions() {
//
}
public function index(){
$listQuestions=Auth::user()->questions()->pluck('question')->all();
//dd($listQuestions);
return view('forms.question',compact('listQuestions'));
}
}
My views file
<ul>
#foreach ($listQuestions as $q)
<li>{{ $q }}</li>
#endforeach
</ul>
$listQuestions isnt getting passed to the view.
But on dd($listQuestions) from view & controller file, I get this
array:2 [▼
0 => "some question"
1 => "another question"
]
It shows empty view page, no errors or output
What am I missing here?
pluck() method retrieves values by keys. You want to get a collection:
$listQuestions = Auth::user()->questions()->get();
$q is an object, so you need to get a property, i.e. question:
#foreach ($listQuestions as $q)
<li>{{ $q->question }}</li>
#endforeach
You can also pass a variable to a view like this
return view('forms.question')
->with('listQuestions' , $listQuestions)
Now i believe you're just passing the string 'listQuestions' over to the view, so it can't iterate over it. Hence the empty view.
Change your
return view('forms.question',compact('listQuestions'));
To
return view('forms.question',['listQuestions'=> $listQuestions]);
That should work