Laravel sqlite3 Eloquent searching database table for specific keyword - php

How would I go about searching my database using a keyword that was inputted by the user, and then displaying the most relevant results on another page?
The code I supplied down below just gives my a blank page, I first assumed this was simply because I had nothing in my database but when I checked there was an input with that keyword.
My search function:
public function searchUsers()
{
$searchWord = Input::get('searchBox');
return View::make('user.search')->with('users', User::where('fullname', 'LIKE', '%'. $searchWord .'%'));
}
The user inputs the word in this blade form:
{{ Form::open(array('url' => secure_url('user/searchUsers'), 'class'=>'form-group has-feedback')) }}
<h1 style="font-size:55px; text-align:center;">Search for Friendship!</h1>
<br>
{{ Form::text('searchBox', $value = null, array('placeholder' => 'Search', 'class'=> 'form-control input-lg','autofocus' => 'autofocus')) }}
{{ Form::close() }}
How I display my results:
#foreach ($users as $user)
<div id="commentPanel"class="panel panel-default">
<div class="panel-heading">{{ $user->fullname }}</div>
<div class="panel-body">
<p id="commentP">{{ $user->email }}</p>
</div>
</div>

Well you are not returning the users from database, you just set where condition to the Eloquent object which simply returns a query object. Change your return statement to be like this:
return View::make('user.search')
->with(
'users',
User::where('fullname', 'LIKE', '%'. $searchWord .'%')->get()
);

Related

How to get the index of the field from my array properly in Laravel blade?

This is the "show" function in my Controller :
public function show($nform)
{
$plan = PlanFormation::findOrFail($nform);
$plan_props = PlanFormation::select(
'clients.raisoci',
'clients.nrc_entrp',
'intervenants.nom',
'intervenants.prenom')
->join('clients', 'clients.nrc_entrp', '=', 'plan_formations.nrc_e')
->join('intervenants', 'intervenants.id_interv', '=', 'plan_formations.id_inv')
->where('plan_formations.n_form', '=', $nform)
->get();
return view('planformation.detail', ['plan_props' => $plan_props, 'plan' => $plan]);
}
Here is my Blade :
<div class="card-header">
<a class="btn btn-dark btn-sm bu-lg-ic" href="/planformation"><i class="fa fa-arrow-left"></i></a>
<h3 class="card-title card-h3">Plan N° {{ $plan->n_form }} > {{ $plan_props['raisoci'] }}</h3>
</div>
I got "Undefined index 'raisoci' when I use $plan_props['raisoci'] in the blade
In your example $plan_props is not an array with element ['raisoci'] but a collection of items. So you can get the first element of this collection and call its 'raisoci' property:
{{ $plan_props[0]['raisoci'] }}
or
{{ $plan_props[0]->raisoci }}
You may also call first() method instead of get() in your query. In this case you'll get exactly what you expected in your example:
$plan_props = PlanFormation::select(...)
...
->where('plan_formations.n_form', '=', $nform)
->first();
It worked when I used ->first() and I left {{ $plan_props['raisoci'] }} as it's
The third answer is the correct answer
Thank you Manssor for your help

Laravel how to echo selected a value in a dropdown using blade when updating a specific resource

I want to echo the selected value when I edit a specific resource in my table. When I edit the specific resource it should display the current data in my dropdown but in the real scenario it displays the first one on the list which is wrong. So how can I echo the selected value in the options of the dropdown using blade in laravel?
Here is some sample of my code in the view below
<!-- Shows Field -->
<div class="form-group col-sm-6">
{!! Form::label('show_id', 'Shows:') !!}
{!! Form::select('show_id', $shows, $shows, ['class' => 'form-control input-md','required'])!!}
</div>
{{-- {{ $channelshows->channel->name }} --}}
<!-- Client Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('channel_id', 'Channels:') !!}
{!! Form::select('channel_id', $channel, $channel, ['class' => 'form-control input-md','required'])!!}
</div>
<!-- Submit Field -->
<div class="form-group col-sm-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
Cancel
</div>
and here is the code in my controller below.
public function edit($id)
{
$channelshows = $this->channelshowsRepository->findWithoutFail($id);
$shows = Show::pluck('name', 'id');
$channel = Channel::pluck('name', 'id');
if (empty($channelshows)) {
Flash::error('Assigned show not found');
return redirect(route('admin.channelshows.index'));
}
return view('admin-dashboard.channelshows.edit', compact('shows', $shows), compact('channel', $channel))->with('channelshows', $channelshows);
}
Everything here works fine even if I updated the specific resource. I just want to auto populate or select the current value of the resource that I will update because when I edit the specific resource it shows the first one on the list.
Am I going to use an #if statement in blade? But how can I do it using the blade template in my select form. Can somebody help me?
Appreciate if someone can help.
Thanks in advance.
Here is an example:
Open form:
{{ Form::model($service, array('route' => array('services.update', $service->id))) }}
Select form field:
<div class="form-group">
{{ Form::label('Related Agreement') }}
{{ Form::select('agreement', $agreementsList, null, array('class'=>'form-control', 'placeholder'=>'Please select ...')) }}
</div>
In Controller:
$agreementsList = Agreement::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)->pluck('name', 'id');
(Include this when passing data to your view)

Laravel 4 : Passing data from database to view

I wish to make a select box containing some categories of items. This select box should have the data from database.
so first i created my blade
admin.blade.php :
<h2>Add Category</h2>
{{ HTML::ul($errors->all(), array('class'=>'errors')) }}
{{ Form::open(array( 'url'=>'admin/category/add', $title="Admin Control Panel")) }}
<p>
{{ Form::label('Category Name:') }}
{{ Form::text('category_name', Input::old('category_name')) }}
</p>
<p>
{{ Form::select('parent_category', array('' => 'Select Category', '0 ' => 'Main Category',)) }}
{{ Form::label('Parent Category:') }}
</p>
<p>
{{ Form::submit('Submit', array('name' => 'save')) }}
</p>
{{ Form::close() }}
and then i created my controller:
class CategoryController extends BaseController
{
public function add()
{
$category_list = CategoryModel::select();
return View::make('admin')->with('category_list', $category_list);
}
i need to display this array in my select box. but i don't know how to do that. please suggest a proper way!
thanks!
return View::make('admin')->with('category_list', $category_list);
What this is saying is create a response and return it to the client. The response is going to be generated using the blade template admin, and we're going to pass the contents of the variable $category_list to the view which will be able to reference it internally as $category_list (the first string parameter to the with function).
Therefore, inside your view if you want to access your category list you can do this:
{{ $category_list->title }}
Assuming, of course, that $category_list here is a single model with an attribute title. If $category_list is a collection of models (or an array), you can just use foreach:
#foreach($category_list as $category)
{{ $category->title }}
#endforeach
If what you're doing is trying to build a select list based off of the entries in your database for a given model, you can simply do this:
{{ Form::select('category', CategoryModel::list('title', 'id')) }}
Of course, you can generate that array in your controller and pass it down as you are doing now, too.

Laravel Form Text Not Being Uploaded To Database

I am trying to create a comment system for posts on a blog. I created a form for uploading a comment. Both the user_id and the post_id columns are properly populated in the table, however, the body column is empty when enter text and submit it. I defined a relationship that one Post hasMany Comments. Also, I defined that one User hasMany Comments. Here is my code:
//commentcontroller.php
public function newComment(){
$id=Auth::id();
$comment = New Comment();
$comment->user_id=$id;
$comment->post_id=Input::get('post_id');
$comment->body=Input::get('body');
post->comments()->save($comment);
}
Form:
<div class="col-md-8 col-md-offset-2">
<hr>
#foreach($posts as $post)
<h3>{{ $post->title}}</h3>
<p>by {{ $post->user->name }}</p>
<img src='{{ asset($post->image_path) }}' class="img-responsive" id="blogpic"/>
<p>{{ Str::words($post->body) }}</p>
<p>Read More...</p>
<hr>
//COMMENT AREA
{{ Form::open(array('action' => 'CommentController#newComment', 'files'=>true)) }}
{{ Form::textarea('body', null, array('class'=>'form-control')) }}
{{ Form::hidden('post_id', $post->id) }}
<br>
<br>
{{ Form::submit('Post') }}
#endforeach
</div>
I am not concerned yet about displaying comments, just uploading them to the database. The user_id and post_id upload correctly in the database, the text just doesn't save. Thanks in advance.
You may try this:
public function newComment()
{
$comment = New Comment();
$comment->user_id = Auth::user()->id;
$comment->post_id = $post_id = Input::get('post_id');
$comment->body = Input::get('body');
Post::find($post_id)->comments()->save($comment);
}
Notice Auth::user()->id is used instead of Auth::id().

how to preform a dynamic select dropdown in laravel 4

I've got my relationships setup where my projects are related to clients, what I want to do is to be able to select a select in my create project view via drop down list.
This can be achieved as so:
{{ Form::select('client', array('s' => 's', 'm' => 'm'), 's');}}
However I'm not sure how I would do this dynamically from the data I am retrieving from the database, the code I have so far is:
{{ Form::open(array('action' => 'ProjectController#store', 'id' => 'createproject')) }}
<div class="form-group">
<? $projects = Auth::user()->clients; ?>
#if (Auth::check())
#if (count($projects) > 0)
#foreach ($projects as $client)
{{ Form::select('client', array('client_one' => 'client_one', 'client_two' => 'client_two'), 'client_one');}}
#endforeach
#else
<h3>You have no projects click here to create a project</h3>
#endif
#endif
</div>
<div class="form-group">
{{ Form::label('project_name', 'Project Name') }}
{{ Form::text('project_name', Input::old('project_name'), array('class' => 'form-control')) }}
</div>
Can anyone point me into the direction of how I can populate this select field dynamically?
Thanks in advance!
Doing the data processing in the view is not a good idea. What you should do is prepare the array in the controller and use that array in the view.
In your controller.
$clients = Auth::user()->clients;
$client_selector = array();
foreach($clients as $client) {
$client_selector[$client->name] = $client->name; // I assume name attribute contains client name here
}
return View::make('your.view', array(.., 'client_selector' => $client_selector, ...));
In your view.
#if(count($client_selector)>0)
{{Form::select('client', $client_selector, array_values($client_selector)[0])}}
#endif
I think this is a be better solution:
In your controller:
$clients = Auth::user()->clients->lists('name', 'name');
return View::make('your.view', array(.., 'clients' => $clients, ...));
In your view:
{{ Form::select('client', $clients) }}

Categories