I’m new to Laravel. I’m using Laravel 5.2 and I’ve faced a problem in inserting data in to pivot table which I used to handle a many to many relationship. For passing data into the sever I’m using a jquery ajax post request. It’s code is as follows.
$("#btnSave").click(function(){
var path = JSON.stringify(route);
var token = $('input[name="_token"]').val();
$.post("/tour",
{
tourname: $("#name").val(),
startpoint: $("#select_startpoint").val(),
endpoint : $("#select_endpoint").val(),
waypoints : path,
'_token': token
},function(){
alert("Path has been saved");
window.location.href = "/tour";
}); });
Here route is a JavaScript array with set of strings and I’m using Json to pass the values in server. Here I’m using an RESTful resource controller to handle the request and its store method is as follows.
public function store(Request $request){
$user = Auth::user();
$tour = new Tour;
$tour->name = $request->tourname;
$tour->user_id = $user->id;
$tour->startpoint = $request->startpoint;
$tour->endpoint = $request->endpoint;
$tour->save();
$json = $request->waypoints;
$waypoints = json_decode($json);
foreach($waypoints as $waypoint){
$city = City::where('name', '=', $waypoint)->firstOrFail();
$tour->cities()->attach($city->id);
} }
Here in inserting data into pivot table I want obtain the city_id of a particular city from the database first, as I only have it’s name in the array.
As I execute the code the tour table get updated correctly but the pivot table (city_tour) does’nt. When I was further debugging I noticed when a integer value is custom assigned (As example: $tour->cities()->attach(2); ) the code works fine. It seems there is a problem in assigning the value to the $waypoint variable inside the query. But I can’t figure it out, help is much appreciated.
You could try where('name', 'LIKE', "%$waypoint%" )..... "=" usually doesn't play well with strings unless its an exact match.
LIKE in SQL gets closest match.
Use of % with LIKE:
searching for the City 'Algiers'.
this would find the city
$city = 'Algiers';
City::where('name', 'LIKE', "$city" )->firstOrFail();
if you have a white space then you might get nothing
$city = ' Algiers';
City::where('name', 'LIKE', "$city" )->firstOrFail();
if you use % then the space or character is ignored.
$city = ' Algiers'; //extra space on the end
City::where('name', 'LIKE', "%$city" )->firstOrFail();
or if you want to ignore any deviations from the end of the word:
$city = 'Algier'; //with 's' missing
City::where('name', 'LIKE', "$city%" )->firstOrFail();
or you dont have to use LIKE but you make sure the $city is in the column.
Hope that helps
Related
I'm beginner in laravel and I'm trying to run comparison queries given in the database.
I saved a field date that is implemented by a form together with other fields including the name.
I tried to query the name and it works all regularly with this code below.
I would like to retrieve all the rows that have the name variable as the field name that I pass (and here it seems to work) and then only those with the field date that have the specified month at the number that I pass as variable $month.
what would be the right form to do this?
thanks
Piero
public function filterparamenter(){
$name = request('name');
$month = request('$month');
$query = subagente::all();
$query = $query->where('subagente', $subagente);
$query = $query->whereMonth('data', $month)->get();
Method Illuminate\Database\Eloquent\Collection::whereMonth does not exist.
Using ::all() returns a Collection, which has a ->where() method, but ->whereMonth() is only available on Eloquent's Builder class. Change your code as follows:
$query = subagente::query();
$query = $query->where('subagente', $subagente);
$query = $query->whereMonth('data', $month)->get();
Or, more compact:
$results = subagente::where("subagente", $subagente)
->whereMonth("data", $month)
-get();
Using ::query() or ::where() to start your query will generate a Builder instance, which you can chain addition clauses (->where(), ->whereMonth(), etc) on before calling ->get() to return a Collection of subagente records.
Side note, should "data" be "date"?
I have a table named messages that has the structure as follow:
id | sender_id | receiver_id | message | sent_at
I have made a system that whenever a user selects a particular user the messages that are sent by or received by him are shown in the page in proper manner. For that I have the following code that does the thing:
<script type="text/javascript">
function showMessages(elem){
let id = $(elem).attr('id');
let myId = "<?php echo \Session::get('user_id'); ?>";
console.log(id, myId);
$.get("/get-messages",
{
id_no_1: id,
id_no_2: myId
}, function(data){
alert(data)
});
}
Here, I get the user_id of the associated user and my own user and perform an AJAX request to the route and the route has:
Route::get('/get-messages', 'PagesController#getMessages');
And my major function looks like this:
public function getMessages(){
$id_no_1 = request('id_no_1');
$id_no_2 = request('id_no_2');
$messagesSendByOneToTwo = DB::table('messages')
->where('sender_id', $id_no_1)
->where('receiver_id', $id_no_2)
->get();
// $messagesSendByOneToTwo = json_encode($messagesSendByOneToTwo);
$messagesSendByTwoToOne = DB::table('messages')
->where('sender_id', $id_no_2)
->where('receiver_id', $id_no_1)
->get();
// $messagesSendByOneToTwo = json_encode($messagesSendByTwoToOne);
$allMessages = array_merge($messagesSendByOneToTwo, $messagesSendByTwoToOne);
$allMessages = json_encode($allMessages);
echo $allMessages;
}
There I have two variables and I want to merge the two variable into a single variable. I tried array_merge(); which I knew wouldn't work because the variables aren't arrays. To this problem, I have the following possible solutions:
Merge the two variables into one and perform things with it. (But, I don't know how to do that.
Send two variables as the response to the JavaScript function handling the response (which I have never heard of).
Returning to a new view with the variables (which doesn't seem to be a good idea)
So, what should I do to solve this problem?
The best way would be to merge the variables into a new one so what should I do here?
There's no reason for you to do two queries to begin with:
public function getMessages(){
$id_no_1 = request('id_no_1');
$id_no_2 = request('id_no_2');
$messagesSent = DB::table('messages')
->whereIn('sender_id', [ $id_no_1, $id_no_2 ])
->whereIn('receiver_id', [ $id_no_1, $id_no_2 ])
->get();
return response()->json($messagesSent); //This is better than echoing json
}
If you want an array you can try following code to get array instead of collection.
messagesSendByOneToTwo = DB::table('messages')
->where('sender_id', $id_no_1)
->where('receiver_id', $id_no_2)
->get()->toArray();
I'm using eloquent to build a query but i'm stuck in the final phase.
$city = City::where('name', $event)->first()->event;
This is working and returns all 3 events for this city, as shown in the screenshot
I want to perform another where for the events, so i can select only 1 based on criteria but it's not working.
I have tried the following
$city = City::where('name', $event)->first()->event->where('id', 1);
and it's giving me the following error
Call to undefined method Illuminate\Database\Eloquent\Collection::where()
Your very example makes no sense, since all you need is:
$eventId = 1;
Event::find($eventId);
In case you wanted to check if that even is related to the city:
Event::where('city_id', $cityId)->find($eventId); // Event model or null
or a bit complicated in this case, but generic solution for any relation type:
Event::whereHas('city', function ($q) use ($cityId) {
$q->where('id', $cityId);
})->find($eventId); // model or null
And finally, if you used find instead of where:
City::where('name', $event)->first()->event->find(1);
City::where('name', $event)->first()->event()->find(1);
It would return the same Model.
This is because find it does basically the same in the context of a query builder:
City::where('name', $event)->first()->event()->where('id', 1)->first();
// equals to:
City::where('name', $event)->first()->event()->find(1);
but it is also a method of the collection:
$someCollection->find($someId); // returns single Model
If you already have the id (1), you can do something like:
$city = City::where('name', $event)->where('id', 1)->first();
But I think you'd rather want the city_id:
$city = City::where('name', $event)->where('city_id', 3)->first();
Solution
The solution is to use event()-> instead of event->
This query is returning the 1st record of the relationship, Which is what i needed.
Thanks everyone!
$city = City::where('name', $event)->first()->event()->where('id', 1)->get();
I have tried various methods to resolve this issue, but none worked for me.
1st method:
$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle');
$title = $title->where('title', '=', 'City');
$title = $title->get();
2nd method:
$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle')->where('title', '=', 'City')->get();
3rd method:
$title = DB::select(DB::raw("select * from titles where titles.char_id = 5 and title = 'Castle' and title = 'City'"));
None of the above methods work. If I take only one where clause it works perfectly. Example:
$title = Character::find($selected_char->id)->title()->where('title', '=', 'City')->get();
$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle')->get();
I even tried to take another column than title, but it doesn't work with a second where function. I want to retreive the rows from titles table where the title is City AND Castle I have used multiple where clauses before in a single select statement and it worked. Not now. Any suggestions? Thanks in advance.
You said:
I want to retreive the rows from titles table where the title is City AND Castle
You may try this:
$rowCOllection = DB::table('titles')
->whereIn('title', array('City', 'Castle'))->get();
Using multiple where:
$rowCOllection = DB::table('titles')
->where('title', 'City')
->where('title', 'Castle')->get();
If you want to add another where clause for titles.char_id then you may use it like:
$rowCOllection = DB::table('titles')
->where('title', 'City')
->where('title', 'Castle')
->where('char_id', 5)->get();
You may chain as much where as you need before you call get() method. You can add the where('char_id', 5) after the whereIn like whereIn(...)->where('char_id', 5) and then call get().
If you have a Title model then you may do the same thing using:
Title::where(...)->where(...)->get();
Same as using DB, only replace the DB::table('titles') with Title, for example:
$rowCOllection = Title::where('title', 'City')
->where('title', 'Castle')
->where('char_id', 5)->get();
What about Character here ?
I don't really know how work your double ->where( in php, but in sql here is the mistake :
When you say where title = 'a' and title = 'b', it's like you say : ok give me something where 0=1 it returns nothing.
You can do :
select * from titles where titles.char_id = 5 and (title = 'Castle' or title = 'City')
Retrieve all data where title equals castle or city
Or
select * from titles where titles.char_id = 5 and title IN ('Castle','City')
Retrieve all data where title equals castle or city using IN
I'm pretty sure you will find a way to do that in PHP too.
Assuming you are using Laravel 4
And Character is your model extended from Eloquent
don't mix FIND and WHERE.
Find is for single usage find AND sorting afterward (so order by, and etc)
So if you want to chain up your query
Character::where()->where()->where()-get() (don't forget the get or else you wont get a result)
this way you respect eloquent's features.
Note your first method with ->title() is flawed because your calling a function that you custom created inside your model - thats why it wouldn't have worked.
Note: WereWolf Alpha's method will also work IF you don't want to use Eloquent because the code that he presented will work but thats Fluent notation...so take your pick.
I am currently building an API, I want to be able to search the API via the query string. So for example http://api.dev/?q=12
I can get the value entered but how do I then use this to search the database?
if ($q = Input::get('q'))
{
return( ModelName::where('id', '=', $q));
}
This doesn't seem to change the search and the same data is just returned.
ID's are unique so it doesn't surprise me you're getting the same result.
For Laravel I find the query builder to be better at handling search queries instead of Eloquent.
E.g.
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
Hope this helps.
To search from the db with Eloquent you can use both this ways:
ModelName::where('id', '=', $q)->get();
or
ModelName::find($q);
In the second example Laravel assumes that your Primary Key is id, so in case you have another field as your primary you have to overwrite it, by setting the property in the model.
class ModelName
{
...your props
private $primaryKey = 'yourPrimaryKey'
...
}