Hello i have made a function in my controller that generates random number and i want to pass that result number to the view.
This is the code of the controller:
public function create()
{
$randomNumber = random_int(100000, 999999);
$clients = Client::all();
$products = Product::all();
return view('orders/create',compact('clients','products'))
->with($randomNumber,(request()->input('page', 1) - 1) * 5);
}
I have inserted ->with($randomNumber) because i saw that people used this method when they had an array and would call it in the view {{$randomNumber->first}} but as i mentioned above i have a single value only not an array.
This is the view code:
<input id="orderNumber" type="orderNumber" class="form-control #error('name') is-invalid #enderror" name="orderNumber" value="{{ $randomNumber }}" required autocomplete="orderNumber">
But shows me this error:
Undefined variable $randomNumber
If you want send a random number to view you can do this :
public function create()
{
$randomNumber = random_int(100000, 999999);
$clients = Client::all();
$products = Product::all();
return view('orders/create',compact('clients','products'))
->with('randomNumber',$randomNumber);
}
Or you want send (request()->input('page', 1) - 1) * 5 named by randomNumber
->with('randomNumber',(request()->input('page', 1) - 1) * 5);
To set variables not necessary in your case.
Everything can be done in a more compact manner, like:
return view('orders/create',[
'clients' => Client::all(),
'products' => Product::all(),
'randomNumber' => random_int(100000, 999999),
])->with((request()->input('page', 1) - 1) * 5);
In blade/ view get variable like:
{{ $randomNumber }}
Related
Here I have two folder named products and teamname. In team name, I have index.blade.php where I can view name like this
using this code
<td>{{ $teambatter->name }}</td>
Where TeambatterController.php have this code
public function index()
{
$teambatter = Teambatter::latest()->paginate(5);
return view('teambatters.index',compact('teambatter'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
The problem is when I view same name in indexpublic.blade.php in products folder using this code
<h1> {{ $teambatter->name }} </h1>
I got $teambater is undefine in indexpublic.blade.php.
How can I add this name to indexpublic.blade.php in the products folder?
its not working If I pass like this in my productConteroller.php
public function indexpublic()
{
$teambatter = Teambatter::latest()->paginate(20);
$products = Product::latest()->paginate(20);
return view('products.indexpublic',compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
This is my web.php
Route::get('/', [ProductController::class, 'indexpublic']);
In the indexpublic() function you're passing the $products variable to the view, but not the $teambatter variable.
This should work:
public function indexpublic()
{
$teambatter = Teambatter::latest()->paginate(20);
$products = Product::latest()->paginate(20);
return view('products.indexpublic',compact('products', 'teambatter'))->with('i', (request()->input('page', 1) - 1) * 5);
}
This is my blade file where the input type is a radio and I have made name dynamically by giving question id so that I can store in question_id. So How can I store them in Controller.
#foreach($question->answers as $index=> $answer)
<span>
<input type="radio" name="{{$question->id}}" value="{{$answer->id}}" id="answer{{$question->id}}"
class="clickanswer" data-id="{{$answer->id}}">
{{$index+1}}) {!! $answer->answer_body !!}
</span>
#endforeach
Now this is my Controller
public function quizTest(Request $request){
if($request->isMethod('post')){
$data=$request->all();
foreach($data as $question_id => $answer_id){
dd($data);
$result=new Result;
$result->user_id=$data['user_id'];
$result->question_id=$question_id;
$result->answer_id=$answer_id;
$result->save();
}
}
}
Now when I do dd($data); and I want to store data like 1 2 3 4 5 in question_id and 2,6,9,13,18 in answer_id
Change your controller code to this to store it in answer and question format
public function quizTest(Request $request){
if($request->isMethod('post')){
$data=$request->all();
$user_id = $data['user_id'];
unset($data['_token']);
unset($data['user_id']);
foreach($data as $question_id => $answer_id){
$result = new Result;
$result->user_id = $user_id;
$result->question_id = $question_id;
$result->answer_id = $answer_id;
$result->save();
}
}
}
My environment is Laravel 6.0 with PHP 7.3. I want to show the old search value in the text field. However, the old() method is not working. After searching, the old value of the search disappeared. Why isn't the old value displayed? I researched that in most cases, you can use redirect()->withInput() but I don't want to use redirect(). I would prefer to use the view(). method
Controller
class ClientController extends Controller
{
public function index()
{
$clients = Client::orderBy('id', 'asc')->paginate(Client::PAGINATE_NUMBER);
return view('auth.client.index', compact('clients'));
}
public function search()
{
$clientID = $request->input('clientID');
$status = $request->input('status');
$nameKana = $request->input('nameKana');
$registerStartDate = $request->input('registerStartDate');
$registerEndDate = $request->input('registerEndDate');
$query = Client::query();
if (isset($clientID)) {
$query->where('id', $clientID);
}
if ($status != "default") {
$query->where('status', (int) $status);
}
if (isset($nameKana)) {
$query->where('nameKana', 'LIKE', '%'.$nameKana.'%');
}
if (isset($registerStartDate)) {
$query->whereDate('registerDate', '>=', $registerStartDate);
}
if (isset($registerEndDate)) {
$query->whereDate('registerDate', '<=', $registerEndDate);
}
$clients = $query->paginate(Client::PAGINATE_NUMBER);
return view('auth.client.index', compact('clients'));
}
}
Routes
Route::get('/', 'ClientController#index')->name('client.index');
Route::get('/search', 'ClientController#search')->name('client.search');
You just need to pass the variables back to the view:
In Controller:
public function search(Request $request){
$clientID = $request->input('clientID');
$status = $request->input('status');
$nameKana = $request->input('nameKana');
$registerStartDate = $request->input('registerStartDate');
$registerEndDate = $request->input('registerEndDate');
...
return view('auth.client.index', compact('clients', 'clientID', 'status', 'nameKana', 'registerStartDate', 'registerEndDate'));
}
Then, in your index, just do an isset() check on the variables:
In index.blade.php:
<input name="clientID" value="{{ isset($clientID) ? $clientID : '' }}"/>
<input name="status" value="{{ isset($status) ? $status : '' }}"/>
<input name="nameKana" value="{{ isset($nameKana) ? $nameKana : '' }}"/>
...
Since you're returning the same view in both functions, but only passing the variables on one of them, you need to use isset() to ensure the variables exist before trying to use them as the value() attribute on your inputs.
Also, make sure you have Request $request in your method, public function search(Request $request){ ... } (see above) so that $request->input() is accessible.
Change the way you load your view and pass in the array as argument.
// Example:
// Create a newarray with new and old data
$dataSet = array (
'clients' => $query->paginate(Client::PAGINATE_NUMBER),
// OLD DATA
'clientID' => $clientID,
'status' => $status,
'nameKana' => $nameKana,
'registerStartDate' => $registerStartDate,
'registerEndDate' => $registerEndDate
);
// sent dataset
return view('auth.client.index', $dataSet);
Then you can access them in your view as variables $registerStartDate but better to check if it exists first using the isset() method.
example <input type='text' value='#if(isset($registerStartDate)) {{registerStartDate}} #endif />
I'm looking to fetch unique values from a table in my database and pass the company name into my view as options in a drop down list within a form.
The function in my controller looks like this:
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->select('company')->get()->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
And my view looks like this:
<div class="form-group">
#foreach ($companies as $company)
{{ Form::label('Select Company')}}
{{ Form::select('companies', $company->company, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
#endforeach
</div>
I'm getting the following error:
Invalid argument supplied for foreach()
If I try {{dd($companies}} I can see an array is being passed through to the view OK. This array looks like this:
array:353[
0 => {#274 ▼
+"company": "Example company name"
}......
]
Ditching the loop and reverting back to:
<div class="form-group">
{{ Form::label('Select Company')}}
{{ Form::select('companies', $companies, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
</div>
causes another error to be thrown which states:
htmlspecialchars() expects parameter 1 to be string, object given
Where am I going wrong here?
In your controller
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->pluck('company')->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
Fllow as per below
your index function must be like below
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->pluck('company')->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
your view must me like below
<div class="form-group">
{{ Form::label('Select Company')}}
{{ Form::select('companies', $companies, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
</div>
let me know if any
Let me show you what I have.
//Post Controller
public function showPost(Post $post)
{
$albums = $post->album()->with('photos')->get();
$comments = $post->comments()->where('approved', '=', 1)->get();
$this->layout->title = $post->title;
$this->layout->main = View::make('home')->nest('content','posts.single', compact('post', 'comments','albums'));
}
mysql sentence executed correctly
string 'select * from albums where albums.id = '{"id":19,"title":"Post no 19","read_more":"Lorem i'... (length=786)
string 'select * from albums where albums.post_id = '19'' (length=54)
string 'select * from images where images.album_id in ('4')' (length=57)
I want to access the Photo object in blade, everything parses to the template but when I try to get the Photos
#foreach($albums->Photos as $photo)
Undefined property: Illuminate\Database\Eloquent\Collection::$Photos (View:
// post model
public function album()
{
return $this->hasOne('Album');
}
//album model
public function Photos(){
return $this->hasMany('Image');
}
Try
#foreach($albums->Photos()->get() as $photo)
{{$photo->attribute}}
#endforeach
You need to call the function that holds the relationship then use
get()
method to return an Eloquent Collection and then iterate it with a foreach loop.
#foreach($albums as $album)
#foreach($album->Photos as $photo)
<p>{{ $photo->image }}</p>
#endforeach
#endforeach
THats the answer