There are two options to retrieve data in views in Laravel 5.
Transfer from the controller to views
Use #inject() command in views
I need to transfer data from table to combobox, which one might be better?
Thank you!
You can query the data from DB in controller and pass it to view:
$users = Users::all();
return view('index')->with(compact('users'));
OR
return view('index')->with('users', $users);
Here is how you can access in view(blade.php)
#foreach($users as $user)
{!! $user->first_name !!}
#endforeach
Related
i just want to ask how exactly #foreach in laravel works for examply code like this
#foreach ($users as $user)
#endforeach
i know $users value can be obtained from controller like this:
public function show()
{
$users = users:find($id);
return view('user_tab', compact('users'));
}
but i saw some tutorial on site that do #foreach like :
#foreach ($user->posts as $post)
#endforeach
what's posts without $ stand for ?? i just dont know. i do already read laravel documentation about this on here https://laravel.com/docs/8.x/blade but still cant understand it can someone explain it to me? thx for advance
$user->posts
Here 'posts' represents a model relationship. Go to your User model and you will find a function like this:
function posts() {
return $this->hasMany(Posts::class);
}
This represents that a User can have multiple posts. You can directly get all the post of an user by calling this function once you get a user instance.
Hence in your code it is fetching all the posts of the specific user by $user->posts and then iterating over it in #foreach loop.
if in your controller you call user with relation then you can access the related enter code here` like this
new to laravel and just wanted to get some tips on how to do the following.
So the view will show all created assignments (using for each i believe) that are stored in my database on PHPadmin. I want to be able to select one of these assignments and pass the assignment ID (name to the next view).,
Thanks
On your blade file:
#foreach ($users as $user)
<tr onclick="window.location='{{route('user.edit', ['id' => $user->id])}}'">
<td>{{$user->name}}</td>
<td>{{$user->age}}</td>
</tr>
#endforeach
On your route file:
Route::resource('/user', 'UserController', ['as' => 'user']);
I have a table called "logs" with custom column "auth_id" and i have another table called "users" with same column name "auth_id" and column "username".
How can i take it from "logs" with "logs.auth_id" and get username by "users" table?
Now i'm getting results by:
$match->messages = DB::table('logs')->where('match_id', '=', $match->match_id)->get();
Blade:
#foreach ($match->messages as $message)
{{ $message->auth_id }}:
{{ $message->message }}
Example: Now i'm getting AUTH5556 with {{ $message->auth_id }} but i need to get USERNAME from "users" table by {{ $message->auth_id }}
Don't use joins, this is the whole reason for using something like laravel (eloquent). You need to define a relationship between your models.
On your user model you would define a method
public function logs()
{
return $this->hasMany(Log::class);
}
On your log model
public function user()
{
return $this->belongsTo(User::class);
}
Now you need to make sure on your logs table you have a user_id column.
Now you can simply get the log for example
$log = Log::find($id);
And you can retrieve any information about the user who owns that log, for an example in a blade file.
{{ $log->user->username }}
To get reverse information you need to iterate through the results as the user can have many logs. Eg:
$user = User::find($id);
Then you'd do something like this in your blade
#foreach($user->logs as $log)
{{ $log->column_on_log_you_want }}
#endforeach
You also need to look into eager loading.
This is what makes laravel so clean and nice to use.
Read through the documentation
You need to use JOIN in your query to get username from users table. Here is the link to Laravel manual
I have this page support.blade.php in views folder and I'm using Laravel.
I can't echo out $user->id or $user[id] in this page and I get undefined variable error. It's possible to echo it out in other pages, like in tickets.blade.php but in this specific page it's not working.
How can I fix this?
Within your Service Provider you can use
View::share('key', 'value')
You can consult the docs for a more indepth explanation.
You need to pass the data first from controller method:
return view('support', ['user' => $user]);
If you want to display ID of an authenticated user, you can do this from any part of the app without passing data from controller method:
{{ auth()->user()->id }}
Did you pass data in view file??
return view('support', ['user' => $user]);
I can access cookie in controller then pass it to view
//HomeController.php
public function index(Request $request)
{
$name = Cookie::get('name');
return view('index', ['name'=> $name]);
}
But I want to write a small control (widget) that can fetch data from cookie without concern of parent controller. For example, header, footer widgets could fetch its own data without main page controller knowing which data is needed.
I can query the data from database by using View Composer. But, how can I access data from view in the request cookie ?
Using static function with defining namespace and etc is a not safe.
Cuz maybe in next versions of framework this namespace can change.
It's better to use helper functions.
{{ request()->cookie('laravel_session') }}
or
{{ cookie('laravel_session') }}
Tested on working app with Laravel 5.2
You can use {{ Cookie::get('laravel_session') }} to print out the cookie inside your view.
You can use
{{\Illuminate\Support\Facades\Cookie::get('laravel_session')}}
in a blade template
You can use:
$response = new \Illuminate\Http\Response(view('your_view'));
$response->withCookie(cookie('cookieName' , 'cookieValue' , expire));
return $response;
or
\Cookie::queue('cookieName', 'cookieValue', expire);
return view('your_view');
I've used both of them.